Debian Codegraph @

external/rx/Ix/NET/Tests/AsyncTests.Bugs.cs

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