annotate Implab.Test/AsyncTests.cs @ 12:eb418ba8275b promises

refactoring, added WorkerPool
author cin
date Tue, 05 Nov 2013 19:55:34 +0400
parents 6ec82bf68c8e
children b0feb5b9ad1c
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]
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
110 public void ComplexCase1Test() {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
111 var flags = new bool[3];
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
112
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
113 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
114
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
115 var p = PromiseHelper
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
116 .Sleep(200, "Alan")
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
117 .Cancelled(() => flags[0] = true)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
118 .Chain(x =>
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
119 PromiseHelper
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
120 .Sleep(200, "Hi, " + x)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
121 .Map( y => y )
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
122 .Cancelled(() => flags[1] = true)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
123 )
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
124 .Cancelled(() => flags[2] = true);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
125 Thread.Sleep(300);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
126 p.Cancel();
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
127 try {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
128 Assert.AreEqual(p.Join(), "Hi, Alan");
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
129 Assert.Fail("Shouldn't get here");
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
130 } catch(OperationCanceledException) {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
131 }
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
132
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
133 Assert.IsFalse(flags[0]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
134 Assert.IsTrue(flags[1]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
135 Assert.IsTrue(flags[2]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
136 }
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
137 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
138 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
139