SynchronizatonContextTests.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Alias Console to MockConsole so we don't accidentally use Console
  2. namespace Terminal.Gui.ApplicationTests;
  3. public class SyncrhonizationContextTests
  4. {
  5. [Fact]
  6. public void SynchronizationContext_CreateCopy ()
  7. {
  8. Application.Init ();
  9. SynchronizationContext context = SynchronizationContext.Current;
  10. Assert.NotNull (context);
  11. SynchronizationContext contextCopy = context.CreateCopy ();
  12. Assert.NotNull (contextCopy);
  13. Assert.NotEqual (context, contextCopy);
  14. Application.Shutdown ();
  15. }
  16. [Fact]
  17. public void SynchronizationContext_Post ()
  18. {
  19. Application.Init ();
  20. SynchronizationContext context = SynchronizationContext.Current;
  21. var success = false;
  22. Task.Run (
  23. () =>
  24. {
  25. Thread.Sleep (1_000);
  26. // non blocking
  27. context.Post (
  28. delegate
  29. {
  30. success = true;
  31. // then tell the application to quit
  32. Application.Invoke (() => Application.RequestStop ());
  33. },
  34. null
  35. );
  36. Assert.False (success);
  37. }
  38. );
  39. // blocks here until the RequestStop is processed at the end of the test
  40. Application.Run ().Dispose ();
  41. Assert.True (success);
  42. Application.Shutdown ();
  43. }
  44. [Fact]
  45. [AutoInitShutdown]
  46. public void SynchronizationContext_Send ()
  47. {
  48. Application.Init ();
  49. SynchronizationContext context = SynchronizationContext.Current;
  50. var success = false;
  51. Task.Run (
  52. () =>
  53. {
  54. Thread.Sleep (1_000);
  55. // blocking
  56. context.Send (
  57. delegate
  58. {
  59. success = true;
  60. // then tell the application to quit
  61. Application.Invoke (() => Application.RequestStop ());
  62. },
  63. null
  64. );
  65. Assert.True (success);
  66. }
  67. );
  68. // blocks here until the RequestStop is processed at the end of the test
  69. Application.Run ().Dispose ();
  70. Assert.True (success);
  71. Application.Shutdown ();
  72. }
  73. }