using ZB.MOM.WW.MxGateway.Server.Workers;
namespace ZB.MOM.WW.MxGateway.Tests.TestSupport;
///
/// Lightweight in-process stand-in for used by fake worker
/// launchers and lifecycle tests.
///
///
///
/// awaits a that is
/// completed only by or , so a caller observing
/// completion can trust that exit actually happened rather than passing spuriously.
///
///
/// The disposal and kill bookkeeping is exposed under several aliases
/// (//;
/// /) so the various lifecycle tests can
/// keep their existing assertion vocabulary while sharing one definition.
///
///
public sealed class FakeWorkerProcess(int processId) : IWorkerProcess
{
private readonly TaskCompletionSource _exited = new(TaskCreationOptions.RunContinuationsAsynchronously);
/// Gets the process identifier.
public int Id { get; } = processId;
/// Gets or sets a value indicating whether the process has exited.
public bool HasExited { get; set; }
/// Gets or sets the exit code of the process, or if it has not exited.
public int? ExitCode { get; set; }
/// Gets the number of times was called.
public int KillCount { get; private set; }
/// Gets a value indicating whether was called at least once.
public bool KillCalled => KillCount > 0;
/// Gets the entireProcessTree flag from the most recent call.
public bool KillEntireProcessTree { get; private set; }
/// Gets a value indicating whether was called.
public bool IsDisposed { get; private set; }
/// Gets a value indicating whether was called.
public bool DisposeCalled => IsDisposed;
/// Gets a value indicating whether was called.
public bool Disposed => IsDisposed;
///
public ValueTask WaitForExitAsync(CancellationToken cancellationToken) =>
new(_exited.Task.WaitAsync(cancellationToken));
///
public void Kill(bool entireProcessTree)
{
KillCount++;
KillEntireProcessTree = entireProcessTree;
MarkExited(-1);
}
///
public void Dispose() => IsDisposed = true;
///
/// Marks the process as exited with the specified exit code and unblocks
/// any callers of .
///
/// The process exit code.
public void MarkExited(int exitCode)
{
HasExited = true;
ExitCode = exitCode;
_exited.TrySetResult();
}
}