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]
|
10
|
140 public void ComplexCase1Test() {
|
|
141 var flags = new bool[3];
|
|
142
|
|
143 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
|
|
144
|
|
145 var p = PromiseHelper
|
|
146 .Sleep(200, "Alan")
|
|
147 .Cancelled(() => flags[0] = true)
|
|
148 .Chain(x =>
|
|
149 PromiseHelper
|
|
150 .Sleep(200, "Hi, " + x)
|
|
151 .Map( y => y )
|
|
152 .Cancelled(() => flags[1] = true)
|
|
153 )
|
|
154 .Cancelled(() => flags[2] = true);
|
|
155 Thread.Sleep(300);
|
|
156 p.Cancel();
|
|
157 try {
|
|
158 Assert.AreEqual(p.Join(), "Hi, Alan");
|
|
159 Assert.Fail("Shouldn't get here");
|
|
160 } catch(OperationCanceledException) {
|
|
161 }
|
|
162
|
|
163 Assert.IsFalse(flags[0]);
|
|
164 Assert.IsTrue(flags[1]);
|
|
165 Assert.IsTrue(flags[2]);
|
|
166 }
|
4
|
167 }
|
|
168 }
|
|
169
|