1
0

UsingAsyncTest.cs 4.8 KB

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