AsyncTests.Bugs.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_TPL
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using System.Collections;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Diagnostics;
  12. //using System.Reactive.Linq;
  13. //using System.Reactive.Concurrency;
  14. namespace Tests
  15. {
  16. public partial class AsyncTests
  17. {
  18. [TestInitialize]
  19. public void InitTests()
  20. {
  21. TaskScheduler.UnobservedTaskException += (o, e) =>
  22. {
  23. };
  24. }
  25. /*
  26. [TestMethod]
  27. public void TestPushPopAsync()
  28. {
  29. var stack = new Stack<int>();
  30. var count = 10;
  31. var observable = Observable.Generate(
  32. 0,
  33. i => i < count,
  34. i => i + 1,
  35. i => i,
  36. i => TimeSpan.FromMilliseconds(1), // change this to 0 to avoid the problem [1]
  37. Scheduler.ThreadPool);
  38. var task = DoSomethingAsync(observable, stack);
  39. // we give it a timeout so the test can fail instead of hang
  40. task.Wait(TimeSpan.FromSeconds(2));
  41. Assert.AreEqual(10, stack.Count);
  42. }
  43. private Task DoSomethingAsync(IObservable<int> observable, Stack<int> stack)
  44. {
  45. var ae = observable
  46. .ToAsyncEnumerable()
  47. //.Do(i => Debug.WriteLine("Bug-fixing side effect: " + i)) // [2]
  48. .GetEnumerator();
  49. var tcs = new TaskCompletionSource<object>();
  50. var a = default(Action);
  51. a = new Action(() =>
  52. {
  53. ae.MoveNext().ContinueWith(t =>
  54. {
  55. if (t.Result)
  56. {
  57. var i = ae.Current;
  58. Debug.WriteLine("Doing something with " + i);
  59. Thread.Sleep(50);
  60. stack.Push(i);
  61. a();
  62. }
  63. else
  64. tcs.TrySetResult(null);
  65. });
  66. });
  67. a();
  68. return tcs.Task;
  69. }
  70. */
  71. static IEnumerable<int> Xs(Action a)
  72. {
  73. try
  74. {
  75. var rnd = new Random();
  76. while (true)
  77. {
  78. yield return rnd.Next(0, 43);
  79. Thread.Sleep(rnd.Next(0, 500));
  80. }
  81. }
  82. finally
  83. {
  84. a();
  85. }
  86. }
  87. [TestMethod]
  88. public void CorrectDispose()
  89. {
  90. var disposed = false;
  91. var xs = new[] { 1, 2, 3 }.WithDispose(() =>
  92. {
  93. disposed = true;
  94. }).ToAsyncEnumerable();
  95. var ys = xs.Select(x => x + 1);
  96. var e = ys.GetEnumerator();
  97. e.Dispose();
  98. Assert.IsTrue(disposed);
  99. Assert.IsFalse(e.MoveNext().Result);
  100. }
  101. [TestMethod]
  102. public void DisposesUponError()
  103. {
  104. var disposed = false;
  105. var xs = new[] { 1, 2, 3 }.WithDispose(() =>
  106. {
  107. disposed = true;
  108. }).ToAsyncEnumerable();
  109. var ex = new Exception("Bang!");
  110. var ys = xs.Select(x => { if (x == 1) throw ex; return x; });
  111. var e = ys.GetEnumerator();
  112. AssertThrows<Exception>(() => e.MoveNext());
  113. Assert.IsTrue(disposed);
  114. }
  115. [TestMethod]
  116. public void CorrectCancel()
  117. {
  118. var disposed = false;
  119. var xs = new[] { 1, 2, 3 }.WithDispose(() =>
  120. {
  121. disposed = true;
  122. }).ToAsyncEnumerable();
  123. var ys = xs.Select(x => x + 1).Where(x => true);
  124. var e = ys.GetEnumerator();
  125. var cts = new CancellationTokenSource();
  126. var t = e.MoveNext(cts.Token);
  127. cts.Cancel();
  128. try
  129. {
  130. t.Wait();
  131. }
  132. catch
  133. {
  134. // Don't care about the outcome; we could have made it to element 1
  135. // but we could also have cancelled the MoveNext-calling task. Either
  136. // way, we want to wait for the task to be completed and check that
  137. }
  138. finally
  139. {
  140. // the cancellation bubbled all the way up to the source to dispose
  141. // it. This design is chosen because cancelling a MoveNext call leaves
  142. // the enumerator in an indeterminate state. Further interactions with
  143. // it should be forbidden.
  144. Assert.IsTrue(disposed);
  145. }
  146. Assert.IsFalse(e.MoveNext().Result);
  147. }
  148. [TestMethod]
  149. public void CanCancelMoveNext()
  150. {
  151. var evt = new ManualResetEvent(false);
  152. var xs = Blocking(evt).ToAsyncEnumerable().Select(x => x).Where(x => true);
  153. var e = xs.GetEnumerator();
  154. var cts = new CancellationTokenSource();
  155. var t = e.MoveNext(cts.Token);
  156. cts.Cancel();
  157. try
  158. {
  159. t.Wait();
  160. Assert.Fail();
  161. }
  162. catch
  163. {
  164. Assert.IsTrue(t.IsCanceled);
  165. }
  166. evt.Set();
  167. }
  168. static IEnumerable<int> Blocking(ManualResetEvent evt)
  169. {
  170. evt.WaitOne();
  171. yield return 42;
  172. }
  173. }
  174. static class MyExt
  175. {
  176. public static IEnumerable<T> WithDispose<T>(this IEnumerable<T> source, Action a)
  177. {
  178. return EnumerableEx.Create(() =>
  179. {
  180. var e = source.GetEnumerator();
  181. return new Enumerator<T>(e.MoveNext, () => e.Current, () => { e.Dispose(); a(); });
  182. });
  183. }
  184. class Enumerator<T> : IEnumerator<T>
  185. {
  186. private readonly Func<bool> _moveNext;
  187. private readonly Func<T> _current;
  188. private readonly Action _dispose;
  189. public Enumerator(Func<bool> moveNext, Func<T> current, Action dispose)
  190. {
  191. _moveNext = moveNext;
  192. _current = current;
  193. _dispose = dispose;
  194. }
  195. public T Current
  196. {
  197. get { return _current(); }
  198. }
  199. public void Dispose()
  200. {
  201. _dispose();
  202. }
  203. object IEnumerator.Current
  204. {
  205. get { return Current; }
  206. }
  207. public bool MoveNext()
  208. {
  209. return _moveNext();
  210. }
  211. public void Reset()
  212. {
  213. throw new NotImplementedException();
  214. }
  215. }
  216. }
  217. }
  218. #endif