SynchronizatonContextTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. [InlineData (typeof (ConsoleDriverFacade<char>), "v2unix")]
  27. public void SynchronizationContext_Post (Type driverType, string driverName = null)
  28. {
  29. lock (_lockPost)
  30. {
  31. ConsoleDriver.RunningUnitTests = true;
  32. if (driverType.Name.Contains ("ConsoleDriverFacade"))
  33. {
  34. Application.Init (driverName: driverName);
  35. }
  36. else
  37. {
  38. Application.Init (driverName: driverType.Name);
  39. }
  40. SynchronizationContext context = SynchronizationContext.Current;
  41. var success = false;
  42. Task.Run (() =>
  43. {
  44. while (Application.Top is null || Application.Top is { Running: false })
  45. {
  46. Thread.Sleep (500);
  47. }
  48. // non blocking
  49. context.Post (
  50. delegate
  51. {
  52. success = true;
  53. // then tell the application to quit
  54. Application.Invoke (() => Application.RequestStop ());
  55. },
  56. null
  57. );
  58. if (Application.Top is { Running: true })
  59. {
  60. Assert.False (success);
  61. }
  62. }
  63. );
  64. // blocks here until the RequestStop is processed at the end of the test
  65. Application.Run ().Dispose ();
  66. Assert.True (success);
  67. if (ApplicationImpl.Instance is ApplicationV2)
  68. {
  69. ApplicationImpl.Instance.Shutdown ();
  70. }
  71. else
  72. {
  73. Application.Shutdown ();
  74. }
  75. }
  76. }
  77. [Fact]
  78. [AutoInitShutdown]
  79. public void SynchronizationContext_Send ()
  80. {
  81. ConsoleDriver.RunningUnitTests = true;
  82. Application.Init ();
  83. SynchronizationContext context = SynchronizationContext.Current;
  84. var success = false;
  85. Task.Run (
  86. () =>
  87. {
  88. Thread.Sleep (500);
  89. // blocking
  90. context.Send (
  91. delegate
  92. {
  93. success = true;
  94. // then tell the application to quit
  95. Application.Invoke (() => Application.RequestStop ());
  96. },
  97. null
  98. );
  99. Assert.True (success);
  100. }
  101. );
  102. // blocks here until the RequestStop is processed at the end of the test
  103. Application.Run ().Dispose ();
  104. Assert.True (success);
  105. Application.Shutdown ();
  106. }
  107. }