4
|
1 using System;
|
|
2 using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
3 using System.Reflection;
|
|
4 using System.Threading;
|
|
5
|
10
|
6 namespace Implab.Test
|
4
|
7 {
|
|
8 [TestClass]
|
|
9 public class AsyncTests
|
|
10 {
|
|
11 [TestMethod]
|
|
12 public void ResolveTest ()
|
|
13 {
|
|
14 int res = -1;
|
|
15 var p = new Promise<int> ();
|
|
16 p.Then (x => res = x);
|
|
17 p.Resolve (100);
|
|
18
|
|
19 Assert.AreEqual (res, 100);
|
0
|
20 }
|
|
21
|
4
|
22 [TestMethod]
|
|
23 public void RejectTest ()
|
|
24 {
|
|
25 int res = -1;
|
|
26 Exception err = null;
|
|
27
|
|
28 var p = new Promise<int> ();
|
|
29 p.Then (x => res = x, e => err = e);
|
|
30 p.Reject (new ApplicationException ("error"));
|
|
31
|
|
32 Assert.AreEqual (res, -1);
|
|
33 Assert.AreEqual (err.Message, "error");
|
|
34
|
0
|
35 }
|
|
36
|
4
|
37 [TestMethod]
|
|
38 public void JoinSuccessTest ()
|
|
39 {
|
|
40 var p = new Promise<int> ();
|
|
41 p.Resolve (100);
|
|
42 Assert.AreEqual (p.Join (), 100);
|
0
|
43 }
|
|
44
|
4
|
45 [TestMethod]
|
|
46 public void JoinFailTest ()
|
|
47 {
|
|
48 var p = new Promise<int> ();
|
|
49 p.Reject (new ApplicationException ("failed"));
|
|
50
|
|
51 try {
|
|
52 p.Join ();
|
|
53 throw new ApplicationException ("WRONG!");
|
|
54 } catch (TargetInvocationException err) {
|
|
55 Assert.AreEqual (err.InnerException.Message, "failed");
|
|
56 } catch {
|
|
57 Assert.Fail ("Got wrong excaption");
|
|
58 }
|
0
|
59 }
|
|
60
|
4
|
61 [TestMethod]
|
|
62 public void MapTest ()
|
|
63 {
|
|
64 var p = new Promise<int> ();
|
|
65
|
|
66 var p2 = p.Map (x => x.ToString ());
|
|
67 p.Resolve (100);
|
|
68
|
|
69 Assert.AreEqual (p2.Join (), "100");
|
0
|
70 }
|
|
71
|
4
|
72 [TestMethod]
|
|
73 public void ChainTest ()
|
|
74 {
|
|
75 var p1 = new Promise<int> ();
|
|
76
|
|
77 var p3 = p1.Chain (x => {
|
|
78 var p2 = new Promise<string> ();
|
|
79 p2.Resolve (x.ToString ());
|
|
80 return p2;
|
|
81 });
|
|
82
|
|
83 p1.Resolve (100);
|
|
84
|
|
85 Assert.AreEqual (p3.Join (), "100");
|
0
|
86 }
|
|
87
|
4
|
88 [TestMethod]
|
|
89 public void PoolTest ()
|
|
90 {
|
|
91 var pid = Thread.CurrentThread.ManagedThreadId;
|
10
|
92 var p = AsyncPool.Invoke (() => Thread.CurrentThread.ManagedThreadId);
|
4
|
93
|
|
94 Assert.AreNotEqual (pid, p.Join ());
|
|
95 }
|
10
|
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 }
|
4
|
125 }
|
|
126 }
|
|
127
|