Mercurial > pub > ImplabNet
comparison Implab.Test/AsyncTests.cs @ 77:91362ffbecf8 v2
ported tests to mono
author | cin |
---|---|
date | Thu, 11 Sep 2014 10:56:14 +0400 |
parents | c761fc982e1d |
children | 4f20870d0816 |
comparison
equal
deleted
inserted
replaced
76:c761fc982e1d | 77:91362ffbecf8 |
---|---|
1 using System; | 1 using System; |
2 using Microsoft.VisualStudio.TestTools.UnitTesting; | |
3 using System.Reflection; | 2 using System.Reflection; |
4 using System.Threading; | 3 using System.Threading; |
5 using Implab.Parallels; | 4 using Implab.Parallels; |
5 | |
6 #if MONO | |
7 | |
8 using NUnit.Framework; | |
9 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; | |
10 using TestMethod = NUnit.Framework.TestAttribute; | |
11 | |
12 #else | |
13 | |
14 using Microsoft.VisualStudio.TestTools.UnitTesting; | |
15 | |
16 #endif | |
6 | 17 |
7 namespace Implab.Test { | 18 namespace Implab.Test { |
8 [TestClass] | 19 [TestClass] |
9 public class AsyncTests { | 20 public class AsyncTests { |
10 [TestMethod] | 21 [TestMethod] |
157 | 168 |
158 [TestMethod] | 169 [TestMethod] |
159 public void WorkerPoolCorrectTest() { | 170 public void WorkerPoolCorrectTest() { |
160 var pool = new WorkerPool(0,1000,100); | 171 var pool = new WorkerPool(0,1000,100); |
161 | 172 |
162 int iterations = 1000; | 173 const int iterations = 1000; |
163 int pending = iterations; | 174 int pending = iterations; |
164 var stop = new ManualResetEvent(false); | 175 var stop = new ManualResetEvent(false); |
165 | 176 |
166 var count = 0; | 177 var count = 0; |
167 for (int i = 0; i < iterations; i++) { | 178 for (int i = 0; i < iterations; i++) { |
215 int writers = 0; | 226 int writers = 0; |
216 int readers = 0; | 227 int readers = 0; |
217 var stop = new ManualResetEvent(false); | 228 var stop = new ManualResetEvent(false); |
218 int total = 0; | 229 int total = 0; |
219 | 230 |
220 int itemsPerWriter = 10000; | 231 const int itemsPerWriter = 10000; |
221 int writersCount = 10; | 232 const int writersCount = 10; |
222 | 233 |
223 for (int i = 0; i < writersCount; i++) { | 234 for (int i = 0; i < writersCount; i++) { |
224 Interlocked.Increment(ref writers); | 235 Interlocked.Increment(ref writers); |
225 var wn = i; | |
226 AsyncPool | 236 AsyncPool |
227 .InvokeNewThread(() => { | 237 .InvokeNewThread(() => { |
228 for (int ii = 0; ii < itemsPerWriter; ii++) { | 238 for (int ii = 0; ii < itemsPerWriter; ii++) { |
229 queue.Enqueue(1); | 239 queue.Enqueue(1); |
230 } | 240 } |
233 .Anyway(() => Interlocked.Decrement(ref writers)); | 243 .Anyway(() => Interlocked.Decrement(ref writers)); |
234 } | 244 } |
235 | 245 |
236 for (int i = 0; i < 10; i++) { | 246 for (int i = 0; i < 10; i++) { |
237 Interlocked.Increment(ref readers); | 247 Interlocked.Increment(ref readers); |
238 var wn = i; | |
239 AsyncPool | 248 AsyncPool |
240 .InvokeNewThread(() => { | 249 .InvokeNewThread(() => { |
241 int t; | 250 int t; |
242 do { | 251 do { |
243 while (queue.TryDequeue(out t)) | 252 while (queue.TryDequeue(out t)) |
258 } | 267 } |
259 | 268 |
260 [TestMethod] | 269 [TestMethod] |
261 public void ParallelMapTest() { | 270 public void ParallelMapTest() { |
262 | 271 |
263 int count = 100000; | 272 const int count = 100000; |
264 | 273 |
265 double[] args = new double[count]; | 274 var args = new double[count]; |
266 var rand = new Random(); | 275 var rand = new Random(); |
267 | 276 |
268 for (int i = 0; i < count; i++) | 277 for (int i = 0; i < count; i++) |
269 args[i] = rand.NextDouble(); | 278 args[i] = rand.NextDouble(); |
270 | 279 |
281 | 290 |
282 [TestMethod] | 291 [TestMethod] |
283 public void ChainedMapTest() { | 292 public void ChainedMapTest() { |
284 | 293 |
285 using (var pool = new WorkerPool(0,100,100)) { | 294 using (var pool = new WorkerPool(0,100,100)) { |
286 int count = 10000; | 295 const int count = 10000; |
287 | 296 |
288 double[] args = new double[count]; | 297 var args = new double[count]; |
289 var rand = new Random(); | 298 var rand = new Random(); |
290 | 299 |
291 for (int i = 0; i < count; i++) | 300 for (int i = 0; i < count; i++) |
292 args[i] = rand.NextDouble(); | 301 args[i] = rand.NextDouble(); |
293 | 302 |
294 var t = Environment.TickCount; | 303 var t = Environment.TickCount; |
295 var res = args | 304 var res = args |
296 .ChainedMap( | 305 .ChainedMap( |
306 // Analysis disable once AccessToDisposedClosure | |
297 x => pool.Invoke( | 307 x => pool.Invoke( |
298 () => Math.Sin(x * x) | 308 () => Math.Sin(x * x) |
299 ), | 309 ), |
300 4 | 310 4 |
301 ) | 311 ) |
312 } | 322 } |
313 | 323 |
314 [TestMethod] | 324 [TestMethod] |
315 public void ParallelForEachTest() { | 325 public void ParallelForEachTest() { |
316 | 326 |
317 int count = 100000; | 327 const int count = 100000; |
318 | 328 |
319 int[] args = new int[count]; | 329 var args = new int[count]; |
320 var rand = new Random(); | 330 var rand = new Random(); |
321 | 331 |
322 for (int i = 0; i < count; i++) | 332 for (int i = 0; i < count; i++) |
323 args[i] = (int)(rand.NextDouble() * 100); | 333 args[i] = (int)(rand.NextDouble() * 100); |
324 | 334 |
342 public void ComplexCase1Test() { | 352 public void ComplexCase1Test() { |
343 var flags = new bool[3]; | 353 var flags = new bool[3]; |
344 | 354 |
345 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map) | 355 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map) |
346 | 356 |
347 var p = PromiseHelper | 357 var step1 = PromiseHelper |
348 .Sleep(200, "Alan") | 358 .Sleep(200, "Alan") |
349 .Cancelled(() => flags[0] = true) | 359 .Cancelled(() => flags[0] = true); |
360 var p = step1 | |
350 .Chain(x => | 361 .Chain(x => |
351 PromiseHelper | 362 PromiseHelper |
352 .Sleep(200, "Hi, " + x) | 363 .Sleep(200, "Hi, " + x) |
353 .Then(y => y) | 364 .Then(y => y) |
354 .Cancelled(() => flags[1] = true) | 365 .Cancelled(() => flags[1] = true) |
355 ) | 366 ) |
356 .Cancelled(() => flags[2] = true); | 367 .Cancelled(() => flags[2] = true); |
357 Thread.Sleep(300); | 368 step1.Join(); |
358 p.Cancel(); | 369 p.Cancel(); |
359 try { | 370 try { |
360 Assert.AreEqual(p.Join(), "Hi, Alan"); | 371 Assert.AreEqual(p.Join(), "Hi, Alan"); |
361 Assert.Fail("Shouldn't get here"); | 372 Assert.Fail("Shouldn't get here"); |
362 } catch (OperationCanceledException) { | 373 } catch (OperationCanceledException) { |
367 Assert.IsTrue(flags[2]); | 378 Assert.IsTrue(flags[2]); |
368 } | 379 } |
369 | 380 |
370 [TestMethod] | 381 [TestMethod] |
371 public void ChainedCancel1Test() { | 382 public void ChainedCancel1Test() { |
372 // при отмене сцепленной асинхронной операции все обещание должно | 383 // РїСЂРё отмене сцепленной асинхронной операции РІСЃРµ обещание должно |
373 // завершаться ошибкой OperationCanceledException | 384 // завершаться ошибкой OperationCanceledException |
374 var p = PromiseHelper | 385 var p = PromiseHelper |
375 .Sleep(1, "Hi, HAL!") | 386 .Sleep(1, "Hi, HAL!") |
376 .Then(x => { | 387 .Then(x => { |
377 // запускаем две асинхронные операции | 388 // запускаем РґРІРµ асинхронные операции |
378 var result = PromiseHelper.Sleep(1000, "HEM ENABLED!!!"); | 389 var result = PromiseHelper.Sleep(1000, "HEM ENABLED!!!"); |
379 // вторая операция отменяет первую до завершения | 390 // вторая операция отменяет первую РґРѕ завершения |
380 PromiseHelper | 391 PromiseHelper |
381 .Sleep(100, "HAL, STOP!") | 392 .Sleep(100, "HAL, STOP!") |
382 .Then(() => result.Cancel()); | 393 .Then(result.Cancel); |
383 return result; | 394 return result; |
384 }); | 395 }); |
385 try { | 396 try { |
386 p.Join(); | 397 p.Join(); |
387 } catch (TargetInvocationException err) { | 398 } catch (TargetInvocationException err) { |
389 } | 400 } |
390 } | 401 } |
391 | 402 |
392 [TestMethod] | 403 [TestMethod] |
393 public void ChainedCancel2Test() { | 404 public void ChainedCancel2Test() { |
394 // при отмене цепочки обещаний, вложенные операции также должны отменяться | 405 // РїСЂРё отмене цепочки обещаний, вложенные операции также должны отменяться |
395 var pSurvive = new Promise<bool>(); | 406 var pSurvive = new Promise<bool>(); |
396 var hemStarted = new ManualResetEvent(false); | 407 var hemStarted = new ManualResetEvent(false); |
397 var p = PromiseHelper | 408 var p = PromiseHelper |
398 .Sleep(1, "Hi, HAL!") | 409 .Sleep(1, "Hi, HAL!") |
399 .Chain(x => { | 410 .Chain(x => { |
400 hemStarted.Set(); | 411 hemStarted.Set(); |
401 // запускаем две асинхронные операции | 412 // запускаем РґРІРµ асинхронные операции |
402 var result = PromiseHelper | 413 var result = PromiseHelper |
403 .Sleep(10000, "HEM ENABLED!!!") | 414 .Sleep(10000, "HEM ENABLED!!!") |
404 .Then(s => pSurvive.Resolve(false)); | 415 .Then(s => pSurvive.Resolve(false)); |
405 | 416 |
406 result | 417 result |