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