annotate Implab.Test/AsyncTests.cs @ 35:2880242f987a diagnostics

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