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