SynchronizatonContextTests.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. [AutoInitShutdown]
  7. public void SynchronizationContext_CreateCopy ()
  8. {
  9. SynchronizationContext context = SynchronizationContext.Current;
  10. Assert.NotNull (context);
  11. SynchronizationContext contextCopy = context.CreateCopy ();
  12. Assert.NotNull (contextCopy);
  13. Assert.NotEqual (context, contextCopy);
  14. }
  15. [Fact]
  16. [AutoInitShutdown]
  17. public void SynchronizationContext_Post ()
  18. {
  19. SynchronizationContext context = SynchronizationContext.Current;
  20. var success = false;
  21. Task.Run (
  22. () =>
  23. {
  24. Thread.Sleep (1_000);
  25. // non blocking
  26. context.Post (
  27. delegate
  28. {
  29. success = true;
  30. // then tell the application to quit
  31. Application.Invoke (() => Application.RequestStop ());
  32. },
  33. null
  34. );
  35. Assert.False (success);
  36. }
  37. );
  38. // blocks here until the RequestStop is processed at the end of the test
  39. Application.Run ().Dispose ();
  40. Assert.True (success);
  41. }
  42. [Fact]
  43. [AutoInitShutdown]
  44. public void SynchronizationContext_Send ()
  45. {
  46. SynchronizationContext context = SynchronizationContext.Current;
  47. var success = false;
  48. Task.Run (
  49. () =>
  50. {
  51. Thread.Sleep (1_000);
  52. // blocking
  53. context.Send (
  54. delegate
  55. {
  56. success = true;
  57. // then tell the application to quit
  58. Application.Invoke (() => Application.RequestStop ());
  59. },
  60. null
  61. );
  62. Assert.True (success);
  63. }
  64. );
  65. // blocks here until the RequestStop is processed at the end of the test
  66. Application.Run ().Dispose ();
  67. Assert.True (success);
  68. }
  69. }