FakeInput.cs 1004 B

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