SynchronizatonContextTests.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. [Theory]
  17. [InlineData (typeof (FakeDriver))]
  18. [InlineData (typeof (NetDriver))]
  19. [InlineData (typeof (WindowsDriver))]
  20. [InlineData (typeof (CursesDriver))]
  21. public void SynchronizationContext_Post (Type driverType)
  22. {
  23. ConsoleDriver.RunningUnitTests = true;
  24. Application.Init (driverName: driverType.Name);
  25. SynchronizationContext context = SynchronizationContext.Current;
  26. var success = false;
  27. Task.Run (
  28. () =>
  29. {
  30. Thread.Sleep (1_000);
  31. // non blocking
  32. context.Post (
  33. delegate
  34. {
  35. success = true;
  36. // then tell the application to quit
  37. Application.Invoke (() => Application.RequestStop ());
  38. },
  39. null
  40. );
  41. Assert.False (success);
  42. }
  43. );
  44. // blocks here until the RequestStop is processed at the end of the test
  45. Application.Run ().Dispose ();
  46. Assert.True (success);
  47. Application.Shutdown ();
  48. }
  49. [Fact]
  50. [AutoInitShutdown]
  51. public void SynchronizationContext_Send ()
  52. {
  53. Application.Init ();
  54. SynchronizationContext context = SynchronizationContext.Current;
  55. var success = false;
  56. Task.Run (
  57. () =>
  58. {
  59. Thread.Sleep (1_000);
  60. // blocking
  61. context.Send (
  62. delegate
  63. {
  64. success = true;
  65. // then tell the application to quit
  66. Application.Invoke (() => Application.RequestStop ());
  67. },
  68. null
  69. );
  70. Assert.True (success);
  71. }
  72. );
  73. // blocks here until the RequestStop is processed at the end of the test
  74. Application.Run ().Dispose ();
  75. Assert.True (success);
  76. Application.Shutdown ();
  77. }
  78. }