TimeoutTests.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #nullable enable
  2. using Xunit.Abstractions;
  3. namespace ApplicationTests.Timeout;
  4. /// <summary>
  5. /// Tests for timeout behavior with nested Application.Run() calls.
  6. /// These tests verify that timeouts scheduled in a parent run loop continue to fire
  7. /// correctly when a nested modal dialog is shown via Application.Run().
  8. /// </summary>
  9. public class TimeoutTests (ITestOutputHelper output)
  10. {
  11. [Fact]
  12. public void AddTimeout_Fires ()
  13. {
  14. IApplication app = Application.Create ();
  15. app.Init ("fake");
  16. uint timeoutTime = 100;
  17. var timeoutFired = false;
  18. // Setup a timeout that will fire
  19. app.AddTimeout (
  20. TimeSpan.FromMilliseconds (timeoutTime),
  21. () =>
  22. {
  23. timeoutFired = true;
  24. // Return false so the timer does not repeat
  25. return false;
  26. }
  27. );
  28. // The timeout has not fired yet
  29. Assert.False (timeoutFired);
  30. // Block the thread to prove the timeout does not fire on a background thread
  31. Thread.Sleep ((int)timeoutTime * 2);
  32. Assert.False (timeoutFired);
  33. app.StopAfterFirstIteration = true;
  34. app.Run<Runnable> ();
  35. // The timeout should have fired
  36. Assert.True (timeoutFired);
  37. app.Dispose ();
  38. }
  39. }