Mercurial > pub > ImplabNet
annotate Implab.Test/AsyncTests.cs @ 76:c761fc982e1d v2
Refactoring of the IPromise<T> interface
Added tests
author | cin |
---|---|
date | Wed, 10 Sep 2014 17:53:05 +0400 |
parents | 3b8393be3441 |
children | 91362ffbecf8 |
rev | line source |
---|---|
4 | 1 using System; |
2 using Microsoft.VisualStudio.TestTools.UnitTesting; | |
3 using System.Reflection; | |
4 using System.Threading; | |
11 | 5 using Implab.Parallels; |
4 | 6 |
15 | 7 namespace Implab.Test { |
8 [TestClass] | |
9 public class AsyncTests { | |
10 [TestMethod] | |
11 public void ResolveTest() { | |
12 int res = -1; | |
13 var p = new Promise<int>(); | |
14 p.Then(x => res = x); | |
15 p.Resolve(100); | |
4 | 16 |
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
17
diff
changeset
|
17 Assert.AreEqual(100, res); |
15 | 18 } |
0 | 19 |
4 | 20 [TestMethod] |
15 | 21 public void RejectTest() { |
22 int res = -1; | |
23 Exception err = null; | |
4 | 24 |
15 | 25 var p = new Promise<int>(); |
73 | 26 p.Then( |
27 x => res = x, | |
28 e => { | |
29 err = e; | |
30 return -2; | |
31 } | |
32 ); | |
15 | 33 p.Reject(new ApplicationException("error")); |
4 | 34 |
15 | 35 Assert.AreEqual(res, -1); |
36 Assert.AreEqual(err.Message, "error"); | |
4 | 37 |
15 | 38 } |
0 | 39 |
4 | 40 [TestMethod] |
76 | 41 public void CancelExceptionTest() { |
42 var p = new Promise<bool>(); | |
43 p.Cancel(); | |
44 | |
45 var p2 = p.Cancelled(() => { | |
46 throw new ApplicationException("CANCELLED"); | |
47 }); | |
48 | |
49 try { | |
50 p2.Join(); | |
51 Assert.Fail(); | |
52 } catch (ApplicationException err) { | |
53 Assert.AreEqual("CANCELLED", err.InnerException.Message); | |
54 } | |
55 | |
56 } | |
57 | |
58 [TestMethod] | |
59 public void ContinueOnCancelTest() { | |
60 var p = new Promise<bool>(); | |
61 p.Cancel(); | |
62 | |
63 var p2 = p | |
64 .Cancelled(() => { | |
65 throw new ApplicationException("CANCELLED"); | |
66 }) | |
67 .Error(e => true); | |
68 | |
69 Assert.AreEqual(true, p2.Join()); | |
70 } | |
71 | |
72 [TestMethod] | |
15 | 73 public void JoinSuccessTest() { |
74 var p = new Promise<int>(); | |
75 p.Resolve(100); | |
76 Assert.AreEqual(p.Join(), 100); | |
77 } | |
0 | 78 |
4 | 79 [TestMethod] |
15 | 80 public void JoinFailTest() { |
81 var p = new Promise<int>(); | |
82 p.Reject(new ApplicationException("failed")); | |
4 | 83 |
15 | 84 try { |
85 p.Join(); | |
86 throw new ApplicationException("WRONG!"); | |
87 } catch (TargetInvocationException err) { | |
88 Assert.AreEqual(err.InnerException.Message, "failed"); | |
89 } catch { | |
90 Assert.Fail("Got wrong excaption"); | |
91 } | |
92 } | |
0 | 93 |
4 | 94 [TestMethod] |
15 | 95 public void MapTest() { |
96 var p = new Promise<int>(); | |
4 | 97 |
76 | 98 var p2 = p.Then(x => x.ToString()); |
15 | 99 p.Resolve(100); |
4 | 100 |
15 | 101 Assert.AreEqual(p2.Join(), "100"); |
102 } | |
0 | 103 |
4 | 104 [TestMethod] |
11 | 105 public void FixErrorTest() { |
106 var p = new Promise<int>(); | |
107 | |
108 var p2 = p.Error(e => 101); | |
109 | |
110 p.Reject(new Exception()); | |
111 | |
112 Assert.AreEqual(p2.Join(), 101); | |
113 } | |
114 | |
115 [TestMethod] | |
15 | 116 public void ChainTest() { |
117 var p1 = new Promise<int>(); | |
4 | 118 |
15 | 119 var p3 = p1.Chain(x => { |
120 var p2 = new Promise<string>(); | |
121 p2.Resolve(x.ToString()); | |
122 return p2; | |
123 }); | |
4 | 124 |
15 | 125 p1.Resolve(100); |
4 | 126 |
15 | 127 Assert.AreEqual(p3.Join(), "100"); |
128 } | |
0 | 129 |
4 | 130 [TestMethod] |
15 | 131 public void PoolTest() { |
132 var pid = Thread.CurrentThread.ManagedThreadId; | |
133 var p = AsyncPool.Invoke(() => Thread.CurrentThread.ManagedThreadId); | |
4 | 134 |
15 | 135 Assert.AreNotEqual(pid, p.Join()); |
136 } | |
10 | 137 |
138 [TestMethod] | |
13 | 139 public void WorkerPoolSizeTest() { |
17 | 140 var pool = new WorkerPool(5, 10, 0); |
13 | 141 |
20 | 142 Assert.AreEqual(5, pool.PoolSize); |
13 | 143 |
22 | 144 pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); |
145 pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); | |
146 pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); | |
13 | 147 |
20 | 148 Assert.AreEqual(5, pool.PoolSize); |
13 | 149 |
150 for (int i = 0; i < 100; i++) | |
22 | 151 pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); |
152 Thread.Sleep(200); | |
20 | 153 Assert.AreEqual(10, pool.PoolSize); |
15 | 154 |
155 pool.Dispose(); | |
13 | 156 } |
157 | |
158 [TestMethod] | |
159 public void WorkerPoolCorrectTest() { | |
17 | 160 var pool = new WorkerPool(0,1000,100); |
15 | 161 |
162 int iterations = 1000; | |
163 int pending = iterations; | |
164 var stop = new ManualResetEvent(false); | |
13 | 165 |
166 var count = 0; | |
15 | 167 for (int i = 0; i < iterations; i++) { |
13 | 168 pool |
169 .Invoke(() => 1) | |
15 | 170 .Then(x => Interlocked.Add(ref count, x)) |
171 .Then(x => Math.Log10(x)) | |
172 .Anyway(() => { | |
173 Interlocked.Decrement(ref pending); | |
174 if (pending == 0) | |
175 stop.Set(); | |
176 }); | |
177 } | |
178 | |
179 stop.WaitOne(); | |
13 | 180 |
15 | 181 Assert.AreEqual(iterations, count); |
182 Console.WriteLine("Max threads: {0}", pool.MaxRunningThreads); | |
183 pool.Dispose(); | |
184 | |
185 } | |
186 | |
187 [TestMethod] | |
188 public void WorkerPoolDisposeTest() { | |
189 var pool = new WorkerPool(5, 20); | |
20 | 190 Assert.AreEqual(5, pool.PoolSize); |
15 | 191 pool.Dispose(); |
21
6a56df4ec59e
DispatchPool works again, but performance is poor in some cases
cin
parents:
20
diff
changeset
|
192 Thread.Sleep(500); |
20 | 193 Assert.AreEqual(0, pool.PoolSize); |
15 | 194 pool.Dispose(); |
13 | 195 } |
196 | |
197 [TestMethod] | |
14 | 198 public void MTQueueTest() { |
199 var queue = new MTQueue<int>(); | |
200 int res; | |
201 | |
202 queue.Enqueue(10); | |
203 Assert.IsTrue(queue.TryDequeue(out res)); | |
204 Assert.AreEqual(10, res); | |
205 Assert.IsFalse(queue.TryDequeue(out res)); | |
206 | |
207 for (int i = 0; i < 1000; i++) | |
208 queue.Enqueue(i); | |
209 | |
210 for (int i = 0; i < 1000; i++) { | |
211 queue.TryDequeue(out res); | |
212 Assert.AreEqual(i, res); | |
213 } | |
214 | |
215 int writers = 0; | |
216 int readers = 0; | |
217 var stop = new ManualResetEvent(false); | |
218 int total = 0; | |
219 | |
76 | 220 int itemsPerWriter = 10000; |
221 int writersCount = 10; | |
14 | 222 |
223 for (int i = 0; i < writersCount; i++) { | |
224 Interlocked.Increment(ref writers); | |
225 var wn = i; | |
226 AsyncPool | |
227 .InvokeNewThread(() => { | |
228 for (int ii = 0; ii < itemsPerWriter; ii++) { | |
229 queue.Enqueue(1); | |
230 } | |
231 return 1; | |
232 }) | |
15 | 233 .Anyway(() => Interlocked.Decrement(ref writers)); |
14 | 234 } |
15 | 235 |
14 | 236 for (int i = 0; i < 10; i++) { |
237 Interlocked.Increment(ref readers); | |
238 var wn = i; | |
239 AsyncPool | |
240 .InvokeNewThread(() => { | |
241 int t; | |
242 do { | |
243 while (queue.TryDequeue(out t)) | |
244 Interlocked.Add(ref total, t); | |
245 } while (writers > 0); | |
246 return 1; | |
247 }) | |
15 | 248 .Anyway(() => { |
14 | 249 Interlocked.Decrement(ref readers); |
250 if (readers == 0) | |
251 stop.Set(); | |
252 }); | |
253 } | |
254 | |
255 stop.WaitOne(); | |
256 | |
257 Assert.AreEqual(itemsPerWriter * writersCount, total); | |
258 } | |
259 | |
260 [TestMethod] | |
15 | 261 public void ParallelMapTest() { |
262 | |
263 int count = 100000; | |
264 | |
265 double[] args = new double[count]; | |
266 var rand = new Random(); | |
267 | |
268 for (int i = 0; i < count; i++) | |
269 args[i] = rand.NextDouble(); | |
270 | |
271 var t = Environment.TickCount; | |
272 var res = args.ParallelMap(x => Math.Sin(x*x), 4).Join(); | |
273 | |
274 Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); | |
275 | |
276 t = Environment.TickCount; | |
277 for (int i = 0; i < count; i++) | |
278 Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); | |
279 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); | |
280 } | |
281 | |
282 [TestMethod] | |
16 | 283 public void ChainedMapTest() { |
284 | |
34 | 285 using (var pool = new WorkerPool(0,100,100)) { |
16 | 286 int count = 10000; |
287 | |
288 double[] args = new double[count]; | |
289 var rand = new Random(); | |
290 | |
291 for (int i = 0; i < count; i++) | |
292 args[i] = rand.NextDouble(); | |
293 | |
294 var t = Environment.TickCount; | |
295 var res = args | |
30 | 296 .ChainedMap( |
16 | 297 x => pool.Invoke( |
298 () => Math.Sin(x * x) | |
299 ), | |
300 4 | |
301 ) | |
302 .Join(); | |
303 | |
304 Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); | |
305 | |
306 t = Environment.TickCount; | |
307 for (int i = 0; i < count; i++) | |
308 Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); | |
309 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); | |
310 Console.WriteLine("Max workers: {0}", pool.MaxRunningThreads); | |
311 } | |
312 } | |
313 | |
314 [TestMethod] | |
15 | 315 public void ParallelForEachTest() { |
316 | |
317 int count = 100000; | |
318 | |
319 int[] args = new int[count]; | |
320 var rand = new Random(); | |
321 | |
322 for (int i = 0; i < count; i++) | |
323 args[i] = (int)(rand.NextDouble() * 100); | |
324 | |
325 int result = 0; | |
326 | |
327 var t = Environment.TickCount; | |
328 args.ParallelForEach(x => Interlocked.Add(ref result, x), 4).Join(); | |
329 | |
330 Console.WriteLine("Iteration complete in {0} ms, result: {1}", Environment.TickCount - t, result); | |
331 | |
332 int result2 = 0; | |
333 | |
334 t = Environment.TickCount; | |
335 for (int i = 0; i < count; i++) | |
336 result2 += args[i]; | |
337 Assert.AreEqual(result2, result); | |
338 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); | |
339 } | |
340 | |
341 [TestMethod] | |
10 | 342 public void ComplexCase1Test() { |
343 var flags = new bool[3]; | |
344 | |
345 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map) | |
346 | |
347 var p = PromiseHelper | |
348 .Sleep(200, "Alan") | |
349 .Cancelled(() => flags[0] = true) | |
350 .Chain(x => | |
351 PromiseHelper | |
352 .Sleep(200, "Hi, " + x) | |
76 | 353 .Then(y => y) |
10 | 354 .Cancelled(() => flags[1] = true) |
355 ) | |
356 .Cancelled(() => flags[2] = true); | |
357 Thread.Sleep(300); | |
358 p.Cancel(); | |
359 try { | |
360 Assert.AreEqual(p.Join(), "Hi, Alan"); | |
361 Assert.Fail("Shouldn't get here"); | |
15 | 362 } catch (OperationCanceledException) { |
10 | 363 } |
364 | |
365 Assert.IsFalse(flags[0]); | |
366 Assert.IsTrue(flags[1]); | |
367 Assert.IsTrue(flags[2]); | |
368 } | |
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
369 |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
370 [TestMethod] |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
371 public void ChainedCancel1Test() { |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
372 // при отмене сцепленной асинхронной операции все обещание должно |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
373 // завершаться ошибкой OperationCanceledException |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
374 var p = PromiseHelper |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
375 .Sleep(1, "Hi, HAL!") |
76 | 376 .Then(x => { |
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
377 // запускаем две асинхронные операции |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
378 var result = PromiseHelper.Sleep(1000, "HEM ENABLED!!!"); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
379 // вторая операция отменяет первую до завершения |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
380 PromiseHelper |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
381 .Sleep(100, "HAL, STOP!") |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
382 .Then(() => result.Cancel()); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
383 return result; |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
384 }); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
385 try { |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
386 p.Join(); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
387 } catch (TargetInvocationException err) { |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
388 Assert.IsTrue(err.InnerException is OperationCanceledException); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
389 } |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
390 } |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
391 |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
392 [TestMethod] |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
393 public void ChainedCancel2Test() { |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
394 // при отмене цепочки обещаний, вложенные операции также должны отменяться |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
395 var pSurvive = new Promise<bool>(); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
396 var hemStarted = new ManualResetEvent(false); |
76 | 397 var p = PromiseHelper |
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
398 .Sleep(1, "Hi, HAL!") |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
399 .Chain(x => { |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
400 hemStarted.Set(); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
401 // запускаем две асинхронные операции |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
402 var result = PromiseHelper |
76 | 403 .Sleep(10000, "HEM ENABLED!!!") |
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
404 .Then(s => pSurvive.Resolve(false)); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
405 |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
406 result |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
407 .Cancelled(() => pSurvive.Resolve(true)); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
408 |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
409 return result; |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
410 }); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
411 |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
412 hemStarted.WaitOne(); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
413 p.Cancel(); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
414 |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
415 try { |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
416 p.Join(); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
417 } catch (OperationCanceledException) { |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
418 Assert.IsTrue(pSurvive.Join()); |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
419 } |
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
30
diff
changeset
|
420 } |
15 | 421 } |
4 | 422 } |
423 |