SynchronizatonContextTests.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.ComTypes;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Terminal.Gui;
  10. using Xunit;
  11. using Xunit.Sdk;
  12. // Alias Console to MockConsole so we don't accidentally use Console
  13. using Console = Terminal.Gui.FakeConsole;
  14. namespace Terminal.Gui.ApplicationTests {
  15. public class SyncrhonizationContextTests {
  16. [Fact, AutoInitShutdown]
  17. public void SynchronizationContext_Post ()
  18. {
  19. var context = SynchronizationContext.Current;
  20. var success = false;
  21. Task.Run (() => {
  22. Thread.Sleep (1_000);
  23. // non blocking
  24. context.Post (
  25. delegate (object o) {
  26. success = true;
  27. // then tell the application to quit
  28. Application.Invoke (() => Application.RequestStop ());
  29. }, null);
  30. Assert.False (success);
  31. });
  32. // blocks here until the RequestStop is processed at the end of the test
  33. Application.Run ();
  34. Assert.True (success);
  35. }
  36. [Fact, AutoInitShutdown]
  37. public void SynchronizationContext_Send ()
  38. {
  39. var context = SynchronizationContext.Current;
  40. var success = false;
  41. Task.Run (() => {
  42. Thread.Sleep (1_000);
  43. // blocking
  44. context.Send (
  45. delegate (object o) {
  46. success = true;
  47. // then tell the application to quit
  48. Application.Invoke (() => Application.RequestStop ());
  49. }, null);
  50. Assert.True (success);
  51. });
  52. // blocks here until the RequestStop is processed at the end of the test
  53. Application.Run ();
  54. Assert.True (success);
  55. }
  56. [Fact, AutoInitShutdown]
  57. public void SynchronizationContext_CreateCopy ()
  58. {
  59. var context = SynchronizationContext.Current;
  60. Assert.NotNull (context);
  61. var contextCopy = context.CreateCopy ();
  62. Assert.NotNull (contextCopy);
  63. Assert.NotEqual (context, contextCopy);
  64. }
  65. }
  66. }