TaskLikeSupportTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Reactive;
  6. using System.Reactive.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Microsoft.VisualStudio.TestTools.UnitTesting;
  10. using Assert = Xunit.Assert;
  11. namespace Tests.System.Reactive.Tests
  12. {
  13. [TestClass]
  14. public class TaskLikeSupportTest
  15. {
  16. [TestMethod]
  17. public async Task Return()
  18. {
  19. Assert.Equal(42, await ManOrBoy_Return());
  20. }
  21. #pragma warning disable 1998
  22. private async ITaskObservable<int> ManOrBoy_Return()
  23. {
  24. return 42;
  25. }
  26. #pragma warning restore 1998
  27. [TestMethod]
  28. public async Task Throw()
  29. {
  30. await Assert.ThrowsAsync<DivideByZeroException>(async () => await ManOrBoy_Throw(42, 0));
  31. }
  32. #pragma warning disable 1998
  33. private async ITaskObservable<int> ManOrBoy_Throw(int n, int d)
  34. {
  35. return n / d;
  36. }
  37. #pragma warning restore 1998
  38. // We execute the ManOrBoy_Basics tests twice, once without a SynchronizationContext, and
  39. // once with one. When we were on xUnit, SynchronizationContext.Current was never null
  40. // because xUnit populates it with their AsyncTestSyncContext, apparently to ensure that
  41. // async void tests work. MSTest takes the more strict view that async void tests should
  42. // not be encouraged. (It has an analyzer to detect these and warn you about them). So
  43. // tests in MSTest get the default behaviour (i.e. SynchronizationContext.Current will be
  44. // null) unless the test sets one up explicitly.
  45. //
  46. // The ManOrBoy_Basics tests exercise different code paths depending on the availability of
  47. // a SynchronizationContext. AsyncSubject<T>.AwaitObserver.InvokeOnOriginalContext will go
  48. // via the context if there is one, and invokes its callback synchronously if not. This is
  49. // a significant difference, which is why, now that we can test both ways, we do.
  50. //
  51. // When we switched to MSTest, and before we had added the tests to run both with and
  52. // without the SynchronizationContext (meaning we only tested without one) this test
  53. // started failing intermittently. It eventually became apparent that this was because
  54. // some test somewhere in the system is setting SynchronizationContext.Current to contain
  55. // a WindowsFormsSynchronizationContext. That's why this test explicitly sets it to
  56. // null - if a UI-based context is present, the test will hang because it will attempt
  57. // to use that context to handle completion but nothing will be running a message loop,
  58. // so completion never occurs. (It's intermittent because the order in which tests run
  59. // is not deterministic, so sometimes when this test runs, the SynchronizationContext
  60. // was already null.)
  61. [TestMethod]
  62. public async Task BasicsNoSynchronizationContext()
  63. {
  64. var ctx = SynchronizationContext.Current;
  65. try
  66. {
  67. SynchronizationContext.SetSynchronizationContext(null);
  68. Assert.Equal(45, await ManOrBoy_Basics());
  69. }
  70. finally
  71. {
  72. SynchronizationContext.SetSynchronizationContext(ctx);
  73. }
  74. }
  75. [TestMethod]
  76. public async Task BasicsWithSynchronizationContext()
  77. {
  78. var ctx = SynchronizationContext.Current;
  79. try
  80. {
  81. SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
  82. Assert.Equal(45, await ManOrBoy_Basics());
  83. }
  84. finally
  85. {
  86. SynchronizationContext.SetSynchronizationContext(ctx);
  87. }
  88. }
  89. private async ITaskObservable<int> ManOrBoy_Basics()
  90. {
  91. var res = 0;
  92. for (var i = 0; i < 10; i++)
  93. {
  94. switch (i % 4)
  95. {
  96. case 0:
  97. res += await Observable.Return(i);
  98. break;
  99. case 1:
  100. res += await Observable.Return(i).Delay(TimeSpan.FromMilliseconds(50));
  101. break;
  102. case 2:
  103. res += await Task.FromResult(i);
  104. break;
  105. case 3:
  106. res += await Task.Run(() => { Task.Delay(50).Wait(); return i; });
  107. break;
  108. }
  109. }
  110. return res;
  111. }
  112. }
  113. }