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]
|
|
110 public void ComplexCase1Test() {
|
|
111 var flags = new bool[3];
|
|
112
|
|
113 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
|
|
114
|
|
115 var p = PromiseHelper
|
|
116 .Sleep(200, "Alan")
|
|
117 .Cancelled(() => flags[0] = true)
|
|
118 .Chain(x =>
|
|
119 PromiseHelper
|
|
120 .Sleep(200, "Hi, " + x)
|
|
121 .Map( y => y )
|
|
122 .Cancelled(() => flags[1] = true)
|
|
123 )
|
|
124 .Cancelled(() => flags[2] = true);
|
|
125 Thread.Sleep(300);
|
|
126 p.Cancel();
|
|
127 try {
|
|
128 Assert.AreEqual(p.Join(), "Hi, Alan");
|
|
129 Assert.Fail("Shouldn't get here");
|
|
130 } catch(OperationCanceledException) {
|
|
131 }
|
|
132
|
|
133 Assert.IsFalse(flags[0]);
|
|
134 Assert.IsTrue(flags[1]);
|
|
135 Assert.IsTrue(flags[2]);
|
|
136 }
|
4
|
137 }
|
|
138 }
|
|
139
|