1
0

TaskLikeSupportTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. This seems likely to be indicative of a subtle bug in
  54. // Rx, because there doesn't seem to be any obvious reason why this should be expected to
  55. // deadlock in the absence of a synchronization context. It doesn't fail if you run the
  56. // test in isolation. It only happens when running all the tests, and even then it often
  57. // doesn't. Since we modified the build to apply a default timeout to all tests with the
  58. // aim of trying to work out which tests were occasionally locking up, the failures have
  59. // not yet recurred, suggesting that there's some sort of race condition here that's finely
  60. // balanced enough to be affected by test settings. Maybe there's some subtle reason why
  61. // you should never attempt to do what this test is doing without a
  62. // SynchronizationContext, but if so, it's unclear what that might be.
  63. // Issue https://github.com/dotnet/reactive/issues/1885 is tracking this until we
  64. // resolve the root cause of the occasional failures.
  65. [TestMethod]
  66. public async Task BasicsNoSynchronizationContext()
  67. {
  68. Assert.Equal(45, await ManOrBoy_Basics());
  69. }
  70. [TestMethod]
  71. public async Task BasicsWithSynchronizationContext()
  72. {
  73. SynchronizationContext ctx = SynchronizationContext.Current;
  74. try
  75. {
  76. SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
  77. Assert.Equal(45, await ManOrBoy_Basics());
  78. }
  79. finally
  80. {
  81. SynchronizationContext.SetSynchronizationContext(ctx);
  82. }
  83. }
  84. #pragma warning disable 1998
  85. private async ITaskObservable<int> ManOrBoy_Basics()
  86. {
  87. var res = 0;
  88. for (var i = 0; i < 10; i++)
  89. {
  90. switch (i % 4)
  91. {
  92. case 0:
  93. res += await Observable.Return(i);
  94. break;
  95. case 1:
  96. res += await Observable.Return(i).Delay(TimeSpan.FromMilliseconds(50));
  97. break;
  98. case 2:
  99. res += await Task.FromResult(i);
  100. break;
  101. case 3:
  102. res += await Task.Run(() => { Task.Delay(50).Wait(); return i; });
  103. break;
  104. }
  105. }
  106. return res;
  107. }
  108. #pragma warning restore 1998
  109. }
  110. }