annotate Implab.Test/AsyncTests.cs @ 14:e943453e5039 promises

Implemented interllocked queue fixed promise syncronization
author cin
date Wed, 06 Nov 2013 17:49:12 +0400
parents b0feb5b9ad1c
children 0f982f9b7d4d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
1 using System;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
2 using Microsoft.VisualStudio.TestTools.UnitTesting;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
3 using System.Reflection;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
4 using System.Threading;
11
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
5 using Implab.Parallels;
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
6
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
7 namespace Implab.Test
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
8 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
9 [TestClass]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
10 public class AsyncTests
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
11 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
12 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
13 public void ResolveTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
14 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
15 int res = -1;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
16 var p = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
17 p.Then (x => res = x);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
18 p.Resolve (100);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
19
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
20 Assert.AreEqual (res, 100);
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
21 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
22
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
23 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
24 public void RejectTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
25 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
26 int res = -1;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
27 Exception err = null;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
28
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
29 var p = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
30 p.Then (x => res = x, e => err = e);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
31 p.Reject (new ApplicationException ("error"));
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
32
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
33 Assert.AreEqual (res, -1);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
34 Assert.AreEqual (err.Message, "error");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
35
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
36 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
37
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
38 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
39 public void JoinSuccessTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
40 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
41 var p = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
42 p.Resolve (100);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
43 Assert.AreEqual (p.Join (), 100);
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
44 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
45
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
46 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
47 public void JoinFailTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
48 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
49 var p = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
50 p.Reject (new ApplicationException ("failed"));
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
51
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
52 try {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
53 p.Join ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
54 throw new ApplicationException ("WRONG!");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
55 } catch (TargetInvocationException err) {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
56 Assert.AreEqual (err.InnerException.Message, "failed");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
57 } catch {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
58 Assert.Fail ("Got wrong excaption");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
59 }
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
60 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
61
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
62 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
63 public void MapTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
64 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
65 var p = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
66
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
67 var p2 = p.Map (x => x.ToString ());
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
68 p.Resolve (100);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
69
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
70 Assert.AreEqual (p2.Join (), "100");
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
71 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
72
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
73 [TestMethod]
11
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
74 public void FixErrorTest() {
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
75 var p = new Promise<int>();
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
76
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
77 var p2 = p.Error(e => 101);
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
78
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
79 p.Reject(new Exception());
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
80
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
81 Assert.AreEqual(p2.Join(), 101);
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
82 }
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
83
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
84 [TestMethod]
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
85 public void ChainTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
86 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
87 var p1 = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
88
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
89 var p3 = p1.Chain (x => {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
90 var p2 = new Promise<string> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
91 p2.Resolve (x.ToString ());
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
92 return p2;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
93 });
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
94
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
95 p1.Resolve (100);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
96
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
97 Assert.AreEqual (p3.Join (), "100");
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
98 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
99
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
100 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
101 public void PoolTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
102 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
103 var pid = Thread.CurrentThread.ManagedThreadId;
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
104 var p = AsyncPool.Invoke (() => Thread.CurrentThread.ManagedThreadId);
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
105
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
106 Assert.AreNotEqual (pid, p.Join ());
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
107 }
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
108
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
109 [TestMethod]
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
110 public void WorkerPoolSizeTest() {
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
111 var pool = new WorkerPool(5,10);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
112
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
113 Assert.AreEqual(5, pool.ThreadCount);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
114
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
115 pool.Invoke(() => { Thread.Sleep(1000); return 10; });
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
116 pool.Invoke(() => { Thread.Sleep(1000); return 10; });
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
117 pool.Invoke(() => { Thread.Sleep(1000); return 10; });
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
118
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
119 Assert.AreEqual(5, pool.ThreadCount);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
120
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
121 for (int i = 0; i < 100; i++)
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
122 pool.Invoke(() => { Thread.Sleep(1000); return 10; });
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
123 Assert.AreEqual(10, pool.ThreadCount);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
124 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
125
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
126 [TestMethod]
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
127 public void WorkerPoolCorrectTest() {
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
128 var pool = new WorkerPool(5, 20);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
129
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
130 var count = 0;
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
131 for (int i = 0; i < 1000; i++)
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
132 pool
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
133 .Invoke(() => 1)
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
134 .Then(x => Interlocked.Add(ref count, x));
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
135
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
136 Assert.AreEqual(1000, count);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
137 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
138
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
139 [TestMethod]
14
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
140 public void MTQueueTest() {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
141 var queue = new MTQueue<int>();
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
142 var pool = new WorkerPool(5, 20);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
143
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
144 int res;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
145
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
146 queue.Enqueue(10);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
147 Assert.IsTrue(queue.TryDequeue(out res));
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
148 Assert.AreEqual(10, res);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
149 Assert.IsFalse(queue.TryDequeue(out res));
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
150
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
151 for (int i = 0; i < 1000; i++)
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
152 queue.Enqueue(i);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
153
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
154 for (int i = 0; i < 1000; i++) {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
155 queue.TryDequeue(out res);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
156 Assert.AreEqual(i, res);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
157 }
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
158
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
159 int writers = 0;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
160 int readers = 0;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
161 var stop = new ManualResetEvent(false);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
162 int total = 0;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
163
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
164 int itemsPerWriter = 1000;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
165 int writersCount = 3;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
166
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
167 for (int i = 0; i < writersCount; i++) {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
168 Interlocked.Increment(ref writers);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
169 var wn = i;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
170 AsyncPool
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
171 .InvokeNewThread(() => {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
172 Console.WriteLine("Started writer: {0}", wn);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
173 for (int ii = 0; ii < itemsPerWriter; ii++) {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
174 queue.Enqueue(1);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
175 Thread.Sleep(1);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
176 }
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
177 Console.WriteLine("Stopped writer: {0}", wn);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
178 return 1;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
179 })
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
180 .Then(x => Interlocked.Decrement(ref writers) );
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
181 }
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
182
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
183 for (int i = 0; i < 10; i++) {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
184 Interlocked.Increment(ref readers);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
185 var wn = i;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
186 AsyncPool
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
187 .InvokeNewThread(() => {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
188 int t;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
189 Console.WriteLine("Started reader: {0}", wn);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
190 do {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
191 while (queue.TryDequeue(out t))
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
192 Interlocked.Add(ref total, t);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
193 Thread.Sleep(0);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
194 } while (writers > 0);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
195 Console.WriteLine("Stopped reader: {0}", wn);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
196 return 1;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
197 })
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
198 .Then(x => {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
199 Interlocked.Decrement(ref readers);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
200 if (readers == 0)
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
201 stop.Set();
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
202 });
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
203 }
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
204
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
205 stop.WaitOne();
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
206
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
207 Assert.AreEqual(itemsPerWriter * writersCount, total);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
208 }
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
209
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
210 [TestMethod]
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
211 public void ComplexCase1Test() {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
212 var flags = new bool[3];
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
213
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
214 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
215
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
216 var p = PromiseHelper
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
217 .Sleep(200, "Alan")
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
218 .Cancelled(() => flags[0] = true)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
219 .Chain(x =>
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
220 PromiseHelper
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
221 .Sleep(200, "Hi, " + x)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
222 .Map( y => y )
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
223 .Cancelled(() => flags[1] = true)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
224 )
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
225 .Cancelled(() => flags[2] = true);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
226 Thread.Sleep(300);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
227 p.Cancel();
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
228 try {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
229 Assert.AreEqual(p.Join(), "Hi, Alan");
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
230 Assert.Fail("Shouldn't get here");
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
231 } catch(OperationCanceledException) {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
232 }
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
233
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
234 Assert.IsFalse(flags[0]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
235 Assert.IsTrue(flags[1]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
236 Assert.IsTrue(flags[2]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
237 }
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
238 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
239 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
240