comparison Implab.Test/AsyncTests.cs @ 10:aa33d0bb8c0c promises

implemeted new cancellable promises concept
author cin
date Sun, 03 Nov 2013 18:07:38 +0400
parents 381095ad0a69
children 6ec82bf68c8e
comparison
equal deleted inserted replaced
9:c82e0dfbb4dd 10:aa33d0bb8c0c
1 using System; 1 using System;
2 using Microsoft.VisualStudio.TestTools.UnitTesting; 2 using Microsoft.VisualStudio.TestTools.UnitTesting;
3 using Implab;
4 using System.Reflection; 3 using System.Reflection;
5 using System.Threading; 4 using System.Threading;
6 5
7 namespace Implab.Tests 6 namespace Implab.Test
8 { 7 {
9 [TestClass] 8 [TestClass]
10 public class AsyncTests 9 public class AsyncTests
11 { 10 {
12 [TestMethod] 11 [TestMethod]
88 87
89 [TestMethod] 88 [TestMethod]
90 public void PoolTest () 89 public void PoolTest ()
91 { 90 {
92 var pid = Thread.CurrentThread.ManagedThreadId; 91 var pid = Thread.CurrentThread.ManagedThreadId;
93 var p = AsyncPool.Invoke (() => { 92 var p = AsyncPool.Invoke (() => Thread.CurrentThread.ManagedThreadId);
94 return Thread.CurrentThread.ManagedThreadId;
95 });
96 93
97 Assert.AreNotEqual (pid, p.Join ()); 94 Assert.AreNotEqual (pid, p.Join ());
98 } 95 }
96
97 [TestMethod]
98 public void ComplexCase1Test() {
99 var flags = new bool[3];
100
101 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
102
103 var p = PromiseHelper
104 .Sleep(200, "Alan")
105 .Cancelled(() => flags[0] = true)
106 .Chain(x =>
107 PromiseHelper
108 .Sleep(200, "Hi, " + x)
109 .Map( y => y )
110 .Cancelled(() => flags[1] = true)
111 )
112 .Cancelled(() => flags[2] = true);
113 Thread.Sleep(300);
114 p.Cancel();
115 try {
116 Assert.AreEqual(p.Join(), "Hi, Alan");
117 Assert.Fail("Shouldn't get here");
118 } catch(OperationCanceledException) {
119 }
120
121 Assert.IsFalse(flags[0]);
122 Assert.IsTrue(flags[1]);
123 Assert.IsTrue(flags[2]);
124 }
99 } 125 }
100 } 126 }
101 127