SynchronizatonContextTests.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Alias Console to MockConsole so we don't accidentally use Console
  2. namespace Terminal.Gui.ApplicationTests;
  3. public class SyncrhonizationContextTests
  4. {
  5. [Fact(Skip = "Causes ubuntu to crash in github action.")]
  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. Application.Init (driverName: driverType.Name);
  24. SynchronizationContext context = SynchronizationContext.Current;
  25. var success = false;
  26. Task.Run (
  27. () =>
  28. {
  29. Thread.Sleep (1_000);
  30. // non blocking
  31. context.Post (
  32. delegate
  33. {
  34. success = true;
  35. // then tell the application to quit
  36. Application.Invoke (() => Application.RequestStop ());
  37. },
  38. null
  39. );
  40. Assert.False (success);
  41. }
  42. );
  43. // blocks here until the RequestStop is processed at the end of the test
  44. Application.Run ().Dispose ();
  45. Assert.True (success);
  46. Application.Shutdown ();
  47. }
  48. [Fact]
  49. [AutoInitShutdown]
  50. public void SynchronizationContext_Send ()
  51. {
  52. Application.Init ();
  53. SynchronizationContext context = SynchronizationContext.Current;
  54. var success = false;
  55. Task.Run (
  56. () =>
  57. {
  58. Thread.Sleep (1_000);
  59. // blocking
  60. context.Send (
  61. delegate
  62. {
  63. success = true;
  64. // then tell the application to quit
  65. Application.Invoke (() => Application.RequestStop ());
  66. },
  67. null
  68. );
  69. Assert.True (success);
  70. }
  71. );
  72. // blocks here until the RequestStop is processed at the end of the test
  73. Application.Run ().Dispose ();
  74. Assert.True (success);
  75. Application.Shutdown ();
  76. }
  77. }