UsingAsyncTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.Collections.Generic;
  6. using System.Linq;
  7. using System.Reactive.Disposables;
  8. using System.Reactive.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Microsoft.Reactive.Testing;
  12. using Microsoft.VisualStudio.TestTools.UnitTesting;
  13. using Assert = Xunit.Assert;
  14. namespace ReactiveTests.Tests
  15. {
  16. [TestClass]
  17. public class UsingAsyncTest : ReactiveTest
  18. {
  19. [TestMethod]
  20. public void UsingAsync_ArgumentChecking()
  21. {
  22. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Using<int, IDisposable>(null, (res, ct) => null));
  23. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Using<int, IDisposable>(ct => null, null));
  24. }
  25. [TestMethod]
  26. public void UsingAsync_Simple()
  27. {
  28. var done = new CountdownEvent(1);
  29. var xs = Observable.Using(
  30. ct => Task.Factory.StartNew(() => Disposable.Create(() => done.Signal())),
  31. (_, ct) => Task.Factory.StartNew(() => Observable.Return(42))
  32. );
  33. var res = xs.ToEnumerable().ToList();
  34. Assert.Equal([42], res);
  35. Assert.True(done.Wait(5000), "done.Wait(5000)");
  36. }
  37. [TestMethod]
  38. public void UsingAsync_CancelResource()
  39. {
  40. var N = 10;// 0000;
  41. for (var i = 0; i < N; i++)
  42. {
  43. var called = false;
  44. var s = new ManualResetEvent(false);
  45. var e = new ManualResetEvent(false);
  46. var x = new ManualResetEvent(false);
  47. var xs = Observable.Using(
  48. ct => Task.Factory.StartNew(() =>
  49. {
  50. s.Set();
  51. e.WaitOne();
  52. while (!ct.IsCancellationRequested)
  53. {
  54. ;
  55. }
  56. x.Set();
  57. return Disposable.Empty;
  58. }),
  59. (_, ct) =>
  60. {
  61. called = true;
  62. return Task.Factory.StartNew(() =>
  63. Observable.Return(42)
  64. );
  65. }
  66. );
  67. var d = xs.Subscribe(_ => { });
  68. s.WaitOne();
  69. d.Dispose();
  70. e.Set();
  71. x.WaitOne();
  72. Assert.False(called);
  73. }
  74. }
  75. [TestMethod]
  76. public void UsingAsync_CancelFactory()
  77. {
  78. var N = 10;// 0000;
  79. for (var i = 0; i < N; i++)
  80. {
  81. var gate = new object();
  82. var disposed = false;
  83. var called = false;
  84. var s = new ManualResetEvent(false);
  85. var e = new ManualResetEvent(false);
  86. var x = new ManualResetEvent(false);
  87. var xs = Observable.Using(
  88. ct => Task.Factory.StartNew(() =>
  89. Disposable.Create(() =>
  90. {
  91. lock (gate)
  92. {
  93. disposed = true;
  94. }
  95. })
  96. ),
  97. (_, ct) => Task.Factory.StartNew(() =>
  98. {
  99. s.Set();
  100. e.WaitOne();
  101. while (!ct.IsCancellationRequested)
  102. {
  103. ;
  104. }
  105. x.Set();
  106. return Observable.Defer(() =>
  107. {
  108. called = true;
  109. return Observable.Return(42);
  110. });
  111. })
  112. );
  113. var d = xs.Subscribe(_ => { });
  114. s.WaitOne();
  115. //
  116. // This will *eventually* set the CancellationToken. There's a fundamental race between observing the CancellationToken
  117. // and returning the IDisposable that will set the CancellationTokenSource. Notice this is reflected in the code above,
  118. // by looping until the CancellationToken is set.
  119. //
  120. d.Dispose();
  121. e.Set();
  122. x.WaitOne();
  123. while (true)
  124. {
  125. lock (gate)
  126. {
  127. if (disposed)
  128. {
  129. break;
  130. }
  131. }
  132. }
  133. Assert.False(called, i.ToString());
  134. }
  135. }
  136. }
  137. }