SynchronizatonContextTests.cs 4.0 KB

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