using System.Runtime.CompilerServices; using System.Threading.Channels; using ZB.MOM.WW.MxGateway.Contracts.Proto; using ZB.MOM.WW.MxGateway.Server.Workers; namespace ZB.MOM.WW.MxGateway.Tests.TestSupport; /// /// Always- whose event /// stream is a channel the test writes to. Lets a test attach a real /// GatewaySession to a worker it drives by hand — the session can be marked Ready, /// its SessionEventDistributor pump then drains this channel, and the test controls /// exactly when each event is fanned. /// /// /// Commands are not scripted here: returns an empty reply, because /// the consumers of this fake route commands through their own ISessionManager double /// rather than through the worker client. Use a purpose-built worker client instead when a /// test needs command behavior. /// /// Session identifier the client reports. /// Channel whose events yields, in order. public sealed class ChannelWorkerClient(string sessionId, ChannelReader events) : IWorkerClient { /// public string SessionId { get; } = sessionId; /// public int? ProcessId { get; } = 4321; /// public WorkerClientState State { get; } = WorkerClientState.Ready; /// public DateTimeOffset LastHeartbeatAt { get; } = DateTimeOffset.UtcNow; /// public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask; /// public Task InvokeAsync( WorkerCommand command, TimeSpan timeout, CancellationToken cancellationToken) => Task.FromResult(new WorkerCommandReply()); /// public async IAsyncEnumerable ReadEventsAsync( [EnumeratorCancellation] CancellationToken cancellationToken) { await foreach (WorkerEvent workerEvent in events.ReadAllAsync(cancellationToken).ConfigureAwait(false)) { yield return workerEvent; } } /// public Task ShutdownAsync(TimeSpan timeout, CancellationToken cancellationToken) => Task.CompletedTask; /// public void Kill(string reason) { } /// Completes immediately because the fake holds no resources. /// A completed value task. public ValueTask DisposeAsync() => ValueTask.CompletedTask; }