SynchronizatonContextTests.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Alias Console to MockConsole so we don't accidentally use Console
  2. using UnitTests;
  3. namespace Terminal.Gui.ApplicationTests;
  4. public class SyncrhonizationContextTests
  5. {
  6. [Fact]
  7. public void SynchronizationContext_CreateCopy ()
  8. {
  9. ConsoleDriver.RunningUnitTests = true;
  10. Application.Init ();
  11. SynchronizationContext context = SynchronizationContext.Current;
  12. Assert.NotNull (context);
  13. SynchronizationContext contextCopy = context.CreateCopy ();
  14. Assert.NotNull (contextCopy);
  15. Assert.NotEqual (context, contextCopy);
  16. Application.Shutdown ();
  17. }
  18. private object _lockPost = new ();
  19. [Theory]
  20. [InlineData (typeof (FakeDriver))]
  21. [InlineData (typeof (NetDriver))]
  22. [InlineData (typeof (WindowsDriver))]
  23. [InlineData (typeof (CursesDriver))]
  24. [InlineData (typeof (ConsoleDriverFacade<WindowsConsole.InputRecord>), "v2win")]
  25. [InlineData (typeof (ConsoleDriverFacade<ConsoleKeyInfo>), "v2net")]
  26. public void SynchronizationContext_Post (Type driverType, string driverName = null)
  27. {
  28. lock (_lockPost)
  29. {
  30. ConsoleDriver.RunningUnitTests = true;
  31. if (driverType.Name.Contains ("ConsoleDriverFacade"))
  32. {
  33. Application.Init (driverName: driverName);
  34. }
  35. else
  36. {
  37. Application.Init (driverName: driverType.Name);
  38. }
  39. SynchronizationContext context = SynchronizationContext.Current;
  40. var success = false;
  41. Task.Run (() =>
  42. {
  43. while (Application.Top is null || Application.Top is { Running: false })
  44. {
  45. Thread.Sleep (500);
  46. }
  47. // non blocking
  48. context.Post (
  49. delegate
  50. {
  51. success = true;
  52. // then tell the application to quit
  53. Application.Invoke (() => Application.RequestStop ());
  54. },
  55. null
  56. );
  57. if (Application.Top is { Running: true })
  58. {
  59. Assert.False (success);
  60. }
  61. }
  62. );
  63. // blocks here until the RequestStop is processed at the end of the test
  64. Application.Run ().Dispose ();
  65. Assert.True (success);
  66. if (ApplicationImpl.Instance is ApplicationV2)
  67. {
  68. ApplicationImpl.Instance.Shutdown ();
  69. }
  70. else
  71. {
  72. Application.Shutdown ();
  73. }
  74. }
  75. }
  76. [Fact]
  77. [AutoInitShutdown]
  78. public void SynchronizationContext_Send ()
  79. {
  80. ConsoleDriver.RunningUnitTests = true;
  81. Application.Init ();
  82. SynchronizationContext context = SynchronizationContext.Current;
  83. var success = false;
  84. Task.Run (
  85. () =>
  86. {
  87. Thread.Sleep (500);
  88. // blocking
  89. context.Send (
  90. delegate
  91. {
  92. success = true;
  93. // then tell the application to quit
  94. Application.Invoke (() => Application.RequestStop ());
  95. },
  96. null
  97. );
  98. Assert.True (success);
  99. }
  100. );
  101. // blocks here until the RequestStop is processed at the end of the test
  102. Application.Run ().Dispose ();
  103. Assert.True (success);
  104. Application.Shutdown ();
  105. }
  106. }