annotate Implab.Test/AsyncTests.cs @ 13:b0feb5b9ad1c promises

small fixes, WorkerPool still incomplete
author cin
date Wed, 06 Nov 2013 01:07:55 +0400
parents 6ec82bf68c8e
children e943453e5039
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]
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
110 public void WorkerPoolSizeTest() {
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
111 var pool = new WorkerPool(5,10);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
112
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
113 Assert.AreEqual(5, pool.ThreadCount);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
114
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
115 pool.Invoke(() => { Thread.Sleep(1000); return 10; });
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
116 pool.Invoke(() => { Thread.Sleep(1000); return 10; });
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
117 pool.Invoke(() => { Thread.Sleep(1000); return 10; });
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
118
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
119 Assert.AreEqual(5, pool.ThreadCount);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
120
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
121 for (int i = 0; i < 100; i++)
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
122 pool.Invoke(() => { Thread.Sleep(1000); return 10; });
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
123 Assert.AreEqual(10, pool.ThreadCount);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
124 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
125
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
126 [TestMethod]
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
127 public void WorkerPoolCorrectTest() {
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
128 var pool = new WorkerPool(5, 20);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
129
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
130 var count = 0;
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
131 for (int i = 0; i < 1000; i++)
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
132 pool
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
133 .Invoke(() => 1)
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
134 .Then(x => Interlocked.Add(ref count, x));
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
135
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
136 Assert.AreEqual(1000, count);
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
137 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
138
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
139 [TestMethod]
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
140 public void ComplexCase1Test() {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
141 var flags = new bool[3];
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
142
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
143 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
144
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
145 var p = PromiseHelper
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
146 .Sleep(200, "Alan")
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
147 .Cancelled(() => flags[0] = true)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
148 .Chain(x =>
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
149 PromiseHelper
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
150 .Sleep(200, "Hi, " + x)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
151 .Map( y => y )
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
152 .Cancelled(() => flags[1] = true)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
153 )
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
154 .Cancelled(() => flags[2] = true);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
155 Thread.Sleep(300);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
156 p.Cancel();
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
157 try {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
158 Assert.AreEqual(p.Join(), "Hi, Alan");
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
159 Assert.Fail("Shouldn't get here");
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
160 } catch(OperationCanceledException) {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
161 }
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
162
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
163 Assert.IsFalse(flags[0]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
164 Assert.IsTrue(flags[1]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
165 Assert.IsTrue(flags[2]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
166 }
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
167 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
168 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
169