2
0

FakeInput.cs 976 B

123456789101112131415161718192021222324252627282930313233
  1. using System.Collections.Concurrent;
  2. namespace TerminalGuiFluentTesting;
  3. internal class FakeInput<T> : IConsoleInput<T>
  4. {
  5. private readonly CancellationToken _hardStopToken;
  6. private readonly CancellationTokenSource _timeoutCts;
  7. public FakeInput (CancellationToken hardStopToken)
  8. {
  9. _hardStopToken = hardStopToken;
  10. // Create a timeout-based cancellation token too to prevent tests ever fully hanging
  11. _timeoutCts = new (With.Timeout);
  12. }
  13. /// <inheritdoc/>
  14. public void Dispose () { }
  15. /// <inheritdoc/>
  16. public void Initialize (ConcurrentQueue<T> inputBuffer) { InputBuffer = inputBuffer; }
  17. public ConcurrentQueue<T>? InputBuffer { get; set; }
  18. /// <inheritdoc/>
  19. public void Run (CancellationToken token)
  20. {
  21. // Blocks until either the token or the hardStopToken is cancelled.
  22. WaitHandle.WaitAny ([token.WaitHandle, _hardStopToken.WaitHandle, _timeoutCts.Token.WaitHandle]);
  23. }
  24. }