UsingAsyncTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.Text;
  8. using System.Threading.Tasks;
  9. using System.Reactive;
  10. using System.Reactive.Concurrency;
  11. using System.Reactive.Linq;
  12. using Microsoft.Reactive.Testing;
  13. using Xunit;
  14. using ReactiveTests.Dummies;
  15. using System.Reflection;
  16. using System.Threading;
  17. using System.Reactive.Disposables;
  18. using System.Reactive.Subjects;
  19. namespace ReactiveTests.Tests
  20. {
  21. public class UsingAsyncTest : ReactiveTest
  22. {
  23. [Fact]
  24. public void UsingAsync_ArgumentChecking()
  25. {
  26. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Using<int, IDisposable>(null, (res, ct) => null));
  27. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.Using<int, IDisposable>(ct => null, null));
  28. }
  29. [Fact]
  30. public void UsingAsync_Simple()
  31. {
  32. var done = new CountdownEvent(1);
  33. var xs = Observable.Using<int, IDisposable>(
  34. ct => Task.Factory.StartNew<IDisposable>(() => Disposable.Create(() => done.Signal())),
  35. (_, ct) => Task.Factory.StartNew<IObservable<int>>(() => Observable.Return(42))
  36. );
  37. var res = xs.ToEnumerable().ToList();
  38. Assert.Equal(new List<int> { 42 }, res);
  39. Assert.True(done.Wait(5000), "done.Wait(5000)");
  40. }
  41. [Fact]
  42. public void UsingAsync_CancelResource()
  43. {
  44. var N = 10;// 0000;
  45. for (int i = 0; i < N; i++)
  46. {
  47. var called = false;
  48. var s = new ManualResetEvent(false);
  49. var e = new ManualResetEvent(false);
  50. var x = new ManualResetEvent(false);
  51. var xs = Observable.Using<int, IDisposable>(
  52. ct => Task.Factory.StartNew<IDisposable>(() =>
  53. {
  54. s.Set();
  55. e.WaitOne();
  56. while (!ct.IsCancellationRequested)
  57. ;
  58. x.Set();
  59. return Disposable.Empty;
  60. }),
  61. (_, ct) =>
  62. {
  63. called = true;
  64. return Task.Factory.StartNew<IObservable<int>>(() =>
  65. Observable.Return(42)
  66. );
  67. }
  68. );
  69. var d = xs.Subscribe(_ => { });
  70. s.WaitOne();
  71. d.Dispose();
  72. e.Set();
  73. x.WaitOne();
  74. Assert.False(called);
  75. }
  76. }
  77. [Fact]
  78. public void UsingAsync_CancelFactory()
  79. {
  80. var N = 10;// 0000;
  81. for (int i = 0; i < N; i++)
  82. {
  83. var gate = new object();
  84. var disposed = false;
  85. var called = false;
  86. var s = new ManualResetEvent(false);
  87. var e = new ManualResetEvent(false);
  88. var x = new ManualResetEvent(false);
  89. var xs = Observable.Using<int, IDisposable>(
  90. ct => Task.Factory.StartNew<IDisposable>(() =>
  91. Disposable.Create(() =>
  92. {
  93. lock (gate)
  94. disposed = true;
  95. })
  96. ),
  97. (_, ct) => Task.Factory.StartNew<IObservable<int>>(() =>
  98. {
  99. s.Set();
  100. e.WaitOne();
  101. while (!ct.IsCancellationRequested)
  102. ;
  103. x.Set();
  104. return Observable.Defer<int>(() =>
  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. if (disposed)
  125. break;
  126. }
  127. Assert.False(called, i.ToString());
  128. }
  129. }
  130. }
  131. }