annotate Implab.Test/AsyncTests.cs @ 73:3b8393be3441 v2

fixed tests
author cin
date Fri, 05 Sep 2014 00:27:14 +0400
parents dabf79fde388
children c761fc982e1d
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
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
7 namespace Implab.Test {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
8 [TestClass]
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
9 public class AsyncTests {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
10 [TestMethod]
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
11 public void ResolveTest() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
12 int res = -1;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
13 var p = new Promise<int>();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
14 p.Then(x => res = x);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
15 p.Resolve(100);
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
16
19
e3935fdf59a2 Promise is rewritten to use interlocked operations instead of locks
cin
parents: 17
diff changeset
17 Assert.AreEqual(100, res);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
18 }
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
19
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
20 [TestMethod]
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
21 public void RejectTest() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
22 int res = -1;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
23 Exception err = null;
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
24
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
25 var p = new Promise<int>();
73
3b8393be3441 fixed tests
cin
parents: 34
diff changeset
26 p.Then(
3b8393be3441 fixed tests
cin
parents: 34
diff changeset
27 x => res = x,
3b8393be3441 fixed tests
cin
parents: 34
diff changeset
28 e => {
3b8393be3441 fixed tests
cin
parents: 34
diff changeset
29 err = e;
3b8393be3441 fixed tests
cin
parents: 34
diff changeset
30 return -2;
3b8393be3441 fixed tests
cin
parents: 34
diff changeset
31 }
3b8393be3441 fixed tests
cin
parents: 34
diff changeset
32 );
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
33 p.Reject(new ApplicationException("error"));
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
34
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
35 Assert.AreEqual(res, -1);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
36 Assert.AreEqual(err.Message, "error");
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
37
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
38 }
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
39
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
40 [TestMethod]
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
41 public void JoinSuccessTest() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
42 var p = new Promise<int>();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
43 p.Resolve(100);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
44 Assert.AreEqual(p.Join(), 100);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
45 }
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
46
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
47 [TestMethod]
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
48 public void JoinFailTest() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
49 var p = new Promise<int>();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
50 p.Reject(new ApplicationException("failed"));
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
51
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
52 try {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
53 p.Join();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
54 throw new ApplicationException("WRONG!");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
55 } catch (TargetInvocationException err) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
56 Assert.AreEqual(err.InnerException.Message, "failed");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
57 } catch {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
58 Assert.Fail("Got wrong excaption");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
59 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
60 }
0
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]
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
63 public void MapTest() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
64 var p = new Promise<int>();
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
65
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
66 var p2 = p.Map(x => x.ToString());
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
67 p.Resolve(100);
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
68
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
69 Assert.AreEqual(p2.Join(), "100");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
70 }
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
71
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
72 [TestMethod]
11
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
73 public void FixErrorTest() {
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
74 var p = new Promise<int>();
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
75
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
76 var p2 = p.Error(e => 101);
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
77
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
78 p.Reject(new Exception());
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
79
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
80 Assert.AreEqual(p2.Join(), 101);
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
81 }
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
82
6ec82bf68c8e refactoring
cin
parents: 10
diff changeset
83 [TestMethod]
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
84 public void ChainTest() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
85 var p1 = new Promise<int>();
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
86
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
87 var p3 = p1.Chain(x => {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
88 var p2 = new Promise<string>();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
89 p2.Resolve(x.ToString());
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
90 return p2;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
91 });
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
92
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
93 p1.Resolve(100);
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
94
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
95 Assert.AreEqual(p3.Join(), "100");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
96 }
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
97
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
98 [TestMethod]
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
99 public void PoolTest() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
100 var pid = Thread.CurrentThread.ManagedThreadId;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
101 var p = AsyncPool.Invoke(() => Thread.CurrentThread.ManagedThreadId);
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
102
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
103 Assert.AreNotEqual(pid, p.Join());
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
104 }
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
105
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
106 [TestMethod]
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
107 public void WorkerPoolSizeTest() {
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
108 var pool = new WorkerPool(5, 10, 0);
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
109
20
1c3b3d518480 refactoring, sync
cin
parents: 19
diff changeset
110 Assert.AreEqual(5, pool.PoolSize);
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
111
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
112 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
113 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
114 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
115
20
1c3b3d518480 refactoring, sync
cin
parents: 19
diff changeset
116 Assert.AreEqual(5, pool.PoolSize);
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
117
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
118 for (int i = 0; i < 100; i++)
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
119 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
120 Thread.Sleep(200);
20
1c3b3d518480 refactoring, sync
cin
parents: 19
diff changeset
121 Assert.AreEqual(10, pool.PoolSize);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
122
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
123 pool.Dispose();
13
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() {
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
128 var pool = new WorkerPool(0,1000,100);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
129
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
130 int iterations = 1000;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
131 int pending = iterations;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
132 var stop = new ManualResetEvent(false);
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
133
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
134 var count = 0;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
135 for (int i = 0; i < iterations; i++) {
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
136 pool
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
137 .Invoke(() => 1)
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
138 .Then(x => Interlocked.Add(ref count, x))
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
139 .Then(x => Math.Log10(x))
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
140 .Anyway(() => {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
141 Interlocked.Decrement(ref pending);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
142 if (pending == 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
143 stop.Set();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
144 });
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
145 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
146
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
147 stop.WaitOne();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
148
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
149 Assert.AreEqual(iterations, count);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
150 Console.WriteLine("Max threads: {0}", pool.MaxRunningThreads);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
151 pool.Dispose();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
152
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
153 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
154
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
155 [TestMethod]
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
156 public void WorkerPoolDisposeTest() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
157 var pool = new WorkerPool(5, 20);
20
1c3b3d518480 refactoring, sync
cin
parents: 19
diff changeset
158 Assert.AreEqual(5, pool.PoolSize);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
159 pool.Dispose();
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
160 Thread.Sleep(500);
20
1c3b3d518480 refactoring, sync
cin
parents: 19
diff changeset
161 Assert.AreEqual(0, pool.PoolSize);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
162 pool.Dispose();
13
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
163 }
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
164
b0feb5b9ad1c small fixes, WorkerPool still incomplete
cin
parents: 11
diff changeset
165 [TestMethod]
14
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
166 public void MTQueueTest() {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
167 var queue = new MTQueue<int>();
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
168 int res;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
169
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
170 queue.Enqueue(10);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
171 Assert.IsTrue(queue.TryDequeue(out res));
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
172 Assert.AreEqual(10, res);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
173 Assert.IsFalse(queue.TryDequeue(out res));
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
174
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
175 for (int i = 0; i < 1000; i++)
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
176 queue.Enqueue(i);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
177
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
178 for (int i = 0; i < 1000; i++) {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
179 queue.TryDequeue(out res);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
180 Assert.AreEqual(i, res);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
181 }
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
182
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
183 int writers = 0;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
184 int readers = 0;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
185 var stop = new ManualResetEvent(false);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
186 int total = 0;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
187
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
188 int itemsPerWriter = 1000;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
189 int writersCount = 3;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
190
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
191 for (int i = 0; i < writersCount; i++) {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
192 Interlocked.Increment(ref writers);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
193 var wn = i;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
194 AsyncPool
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
195 .InvokeNewThread(() => {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
196 for (int ii = 0; ii < itemsPerWriter; ii++) {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
197 queue.Enqueue(1);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
198 }
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
199 return 1;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
200 })
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
201 .Anyway(() => Interlocked.Decrement(ref writers));
14
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
202 }
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
203
14
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
204 for (int i = 0; i < 10; i++) {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
205 Interlocked.Increment(ref readers);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
206 var wn = i;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
207 AsyncPool
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
208 .InvokeNewThread(() => {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
209 int t;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
210 do {
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
211 while (queue.TryDequeue(out t))
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
212 Interlocked.Add(ref total, t);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
213 } while (writers > 0);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
214 return 1;
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
215 })
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
216 .Anyway(() => {
14
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
217 Interlocked.Decrement(ref readers);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
218 if (readers == 0)
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
219 stop.Set();
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
220 });
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
221 }
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
222
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
223 stop.WaitOne();
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
224
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
225 Assert.AreEqual(itemsPerWriter * writersCount, total);
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
226 }
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
227
e943453e5039 Implemented interllocked queue
cin
parents: 13
diff changeset
228 [TestMethod]
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
229 public void ParallelMapTest() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
230
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
231 int count = 100000;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
232
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
233 double[] args = new double[count];
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
234 var rand = new Random();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
235
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
236 for (int i = 0; i < count; i++)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
237 args[i] = rand.NextDouble();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
238
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
239 var t = Environment.TickCount;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
240 var res = args.ParallelMap(x => Math.Sin(x*x), 4).Join();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
241
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
242 Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
243
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
244 t = Environment.TickCount;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
245 for (int i = 0; i < count; i++)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
246 Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
247 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
248 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
249
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
250 [TestMethod]
16
cin
parents: 15
diff changeset
251 public void ChainedMapTest() {
cin
parents: 15
diff changeset
252
34
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 33
diff changeset
253 using (var pool = new WorkerPool(0,100,100)) {
16
cin
parents: 15
diff changeset
254 int count = 10000;
cin
parents: 15
diff changeset
255
cin
parents: 15
diff changeset
256 double[] args = new double[count];
cin
parents: 15
diff changeset
257 var rand = new Random();
cin
parents: 15
diff changeset
258
cin
parents: 15
diff changeset
259 for (int i = 0; i < count; i++)
cin
parents: 15
diff changeset
260 args[i] = rand.NextDouble();
cin
parents: 15
diff changeset
261
cin
parents: 15
diff changeset
262 var t = Environment.TickCount;
cin
parents: 15
diff changeset
263 var res = args
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
264 .ChainedMap(
16
cin
parents: 15
diff changeset
265 x => pool.Invoke(
cin
parents: 15
diff changeset
266 () => Math.Sin(x * x)
cin
parents: 15
diff changeset
267 ),
cin
parents: 15
diff changeset
268 4
cin
parents: 15
diff changeset
269 )
cin
parents: 15
diff changeset
270 .Join();
cin
parents: 15
diff changeset
271
cin
parents: 15
diff changeset
272 Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t);
cin
parents: 15
diff changeset
273
cin
parents: 15
diff changeset
274 t = Environment.TickCount;
cin
parents: 15
diff changeset
275 for (int i = 0; i < count; i++)
cin
parents: 15
diff changeset
276 Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]);
cin
parents: 15
diff changeset
277 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t);
cin
parents: 15
diff changeset
278 Console.WriteLine("Max workers: {0}", pool.MaxRunningThreads);
cin
parents: 15
diff changeset
279 }
cin
parents: 15
diff changeset
280 }
cin
parents: 15
diff changeset
281
cin
parents: 15
diff changeset
282 [TestMethod]
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
283 public void ParallelForEachTest() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
284
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
285 int count = 100000;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
286
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
287 int[] args = new int[count];
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
288 var rand = new Random();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
289
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
290 for (int i = 0; i < count; i++)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
291 args[i] = (int)(rand.NextDouble() * 100);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
292
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
293 int result = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
294
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
295 var t = Environment.TickCount;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
296 args.ParallelForEach(x => Interlocked.Add(ref result, x), 4).Join();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
297
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
298 Console.WriteLine("Iteration complete in {0} ms, result: {1}", Environment.TickCount - t, result);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
299
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
300 int result2 = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
301
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
302 t = Environment.TickCount;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
303 for (int i = 0; i < count; i++)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
304 result2 += args[i];
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
305 Assert.AreEqual(result2, result);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
306 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
307 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
308
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
309 [TestMethod]
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
310 public void ComplexCase1Test() {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
311 var flags = new bool[3];
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
312
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
313 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
314
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
315 var p = PromiseHelper
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
316 .Sleep(200, "Alan")
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
317 .Cancelled(() => flags[0] = true)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
318 .Chain(x =>
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
319 PromiseHelper
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
320 .Sleep(200, "Hi, " + x)
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
321 .Map(y => y)
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
322 .Cancelled(() => flags[1] = true)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
323 )
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
324 .Cancelled(() => flags[2] = true);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
325 Thread.Sleep(300);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
326 p.Cancel();
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
327 try {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
328 Assert.AreEqual(p.Join(), "Hi, Alan");
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
329 Assert.Fail("Shouldn't get here");
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
330 } catch (OperationCanceledException) {
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
331 }
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
332
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
333 Assert.IsFalse(flags[0]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
334 Assert.IsTrue(flags[1]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
335 Assert.IsTrue(flags[2]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
336 }
33
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
337
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
338 [TestMethod]
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
339 public void ChainedCancel1Test() {
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
340 // при отмене сцепленной асинхронной операции все обещание должно
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
341 // завершаться ошибкой OperationCanceledException
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
342 var p = PromiseHelper
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
343 .Sleep(1, "Hi, HAL!")
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
344 .Chain(x => {
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
345 // запускаем две асинхронные операции
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
346 var result = PromiseHelper.Sleep(1000, "HEM ENABLED!!!");
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
347 // вторая операция отменяет первую до завершения
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
348 PromiseHelper
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
349 .Sleep(100, "HAL, STOP!")
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
350 .Then(() => result.Cancel());
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
351 return result;
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
352 });
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
353 try {
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
354 p.Join();
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
355 } catch (TargetInvocationException err) {
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
356 Assert.IsTrue(err.InnerException is OperationCanceledException);
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
357 }
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
358 }
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
359
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
360 [TestMethod]
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
361 public void ChainedCancel2Test() {
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
362 // при отмене цепочки обещаний, вложенные операции также должны отменяться
73
3b8393be3441 fixed tests
cin
parents: 34
diff changeset
363 IPromise p = null;
33
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
364 var pSurvive = new Promise<bool>();
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
365 var hemStarted = new ManualResetEvent(false);
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
366 p = PromiseHelper
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
367 .Sleep(1, "Hi, HAL!")
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
368 .Chain(x => {
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
369 hemStarted.Set();
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
370 // запускаем две асинхронные операции
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
371 var result = PromiseHelper
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
372 .Sleep(1000, "HEM ENABLED!!!")
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
373 .Then(s => pSurvive.Resolve(false));
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
374
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
375 result
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
376 .Cancelled(() => pSurvive.Resolve(true));
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
377
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
378 return result;
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
379 });
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
380
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
381 hemStarted.WaitOne();
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
382 p.Cancel();
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
383
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
384 try {
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
385 p.Join();
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
386 } catch (OperationCanceledException) {
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
387 Assert.IsTrue(pSurvive.Join());
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
388 }
b255e4aeef17 removed the reference to the parent from the promise object this allows
cin
parents: 30
diff changeset
389 }
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents: 14
diff changeset
390 }
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
391 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
392