annotate Implab.Test/AsyncTests.cs @ 77:91362ffbecf8 v2

ported tests to mono
author cin
date Thu, 11 Sep 2014 10:56:14 +0400
parents c761fc982e1d
children 4f20870d0816
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
1 using System;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
2 using System.Reflection;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
3 using System.Threading;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
4 using Implab.Parallels;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
5
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
6 #if MONO
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
7
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
8 using NUnit.Framework;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
9 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
10 using TestMethod = NUnit.Framework.TestAttribute;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
11
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
12 #else
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
13
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
14 using Microsoft.VisualStudio.TestTools.UnitTesting;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
15
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
16 #endif
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
17
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
18 namespace Implab.Test {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
19 [TestClass]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
20 public class AsyncTests {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
21 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
22 public void ResolveTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
23 int res = -1;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
24 var p = new Promise<int>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
25 p.Then(x => res = x);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
26 p.Resolve(100);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
27
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
28 Assert.AreEqual(100, res);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
29 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
30
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
31 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
32 public void RejectTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
33 int res = -1;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
34 Exception err = null;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
35
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
36 var p = new Promise<int>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
37 p.Then(
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
38 x => res = x,
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
39 e => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
40 err = e;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
41 return -2;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
42 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
43 );
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
44 p.Reject(new ApplicationException("error"));
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
45
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
46 Assert.AreEqual(res, -1);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
47 Assert.AreEqual(err.Message, "error");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
48
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
49 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
50
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
51 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
52 public void CancelExceptionTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
53 var p = new Promise<bool>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
54 p.Cancel();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
55
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
56 var p2 = p.Cancelled(() => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
57 throw new ApplicationException("CANCELLED");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
58 });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
59
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
60 try {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
61 p2.Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
62 Assert.Fail();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
63 } catch (ApplicationException err) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
64 Assert.AreEqual("CANCELLED", err.InnerException.Message);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
65 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
66
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
67 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
68
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
69 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
70 public void ContinueOnCancelTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
71 var p = new Promise<bool>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
72 p.Cancel();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
73
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
74 var p2 = p
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
75 .Cancelled(() => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
76 throw new ApplicationException("CANCELLED");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
77 })
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
78 .Error(e => true);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
79
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
80 Assert.AreEqual(true, p2.Join());
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
81 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
82
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
83 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
84 public void JoinSuccessTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
85 var p = new Promise<int>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
86 p.Resolve(100);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
87 Assert.AreEqual(p.Join(), 100);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
88 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
89
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
90 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
91 public void JoinFailTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
92 var p = new Promise<int>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
93 p.Reject(new ApplicationException("failed"));
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
94
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
95 try {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
96 p.Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
97 throw new ApplicationException("WRONG!");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
98 } catch (TargetInvocationException err) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
99 Assert.AreEqual(err.InnerException.Message, "failed");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
100 } catch {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
101 Assert.Fail("Got wrong excaption");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
102 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
103 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
104
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
105 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
106 public void MapTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
107 var p = new Promise<int>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
108
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
109 var p2 = p.Then(x => x.ToString());
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
110 p.Resolve(100);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
111
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
112 Assert.AreEqual(p2.Join(), "100");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
113 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
114
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
115 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
116 public void FixErrorTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
117 var p = new Promise<int>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
118
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
119 var p2 = p.Error(e => 101);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
120
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
121 p.Reject(new Exception());
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
122
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
123 Assert.AreEqual(p2.Join(), 101);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
124 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
125
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
126 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
127 public void ChainTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
128 var p1 = new Promise<int>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
129
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
130 var p3 = p1.Chain(x => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
131 var p2 = new Promise<string>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
132 p2.Resolve(x.ToString());
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
133 return p2;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
134 });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
135
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
136 p1.Resolve(100);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
137
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
138 Assert.AreEqual(p3.Join(), "100");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
139 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
140
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
141 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
142 public void PoolTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
143 var pid = Thread.CurrentThread.ManagedThreadId;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
144 var p = AsyncPool.Invoke(() => Thread.CurrentThread.ManagedThreadId);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
145
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
146 Assert.AreNotEqual(pid, p.Join());
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
147 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
148
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
149 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
150 public void WorkerPoolSizeTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
151 var pool = new WorkerPool(5, 10, 0);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
152
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
153 Assert.AreEqual(5, pool.PoolSize);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
154
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
155 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
156 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
157 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
158
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
159 Assert.AreEqual(5, pool.PoolSize);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
160
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
161 for (int i = 0; i < 100; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
162 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
163 Thread.Sleep(200);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
164 Assert.AreEqual(10, pool.PoolSize);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
165
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
166 pool.Dispose();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
167 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
168
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
169 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
170 public void WorkerPoolCorrectTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
171 var pool = new WorkerPool(0,1000,100);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
172
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
173 const int iterations = 1000;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
174 int pending = iterations;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
175 var stop = new ManualResetEvent(false);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
176
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
177 var count = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
178 for (int i = 0; i < iterations; i++) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
179 pool
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
180 .Invoke(() => 1)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
181 .Then(x => Interlocked.Add(ref count, x))
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
182 .Then(x => Math.Log10(x))
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
183 .Anyway(() => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
184 Interlocked.Decrement(ref pending);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
185 if (pending == 0)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
186 stop.Set();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
187 });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
188 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
189
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
190 stop.WaitOne();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
191
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
192 Assert.AreEqual(iterations, count);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
193 Console.WriteLine("Max threads: {0}", pool.MaxRunningThreads);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
194 pool.Dispose();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
195
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
196 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
197
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
198 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
199 public void WorkerPoolDisposeTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
200 var pool = new WorkerPool(5, 20);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
201 Assert.AreEqual(5, pool.PoolSize);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
202 pool.Dispose();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
203 Thread.Sleep(500);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
204 Assert.AreEqual(0, pool.PoolSize);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
205 pool.Dispose();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
206 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
207
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
208 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
209 public void MTQueueTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
210 var queue = new MTQueue<int>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
211 int res;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
212
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
213 queue.Enqueue(10);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
214 Assert.IsTrue(queue.TryDequeue(out res));
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
215 Assert.AreEqual(10, res);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
216 Assert.IsFalse(queue.TryDequeue(out res));
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
217
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
218 for (int i = 0; i < 1000; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
219 queue.Enqueue(i);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
220
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
221 for (int i = 0; i < 1000; i++) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
222 queue.TryDequeue(out res);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
223 Assert.AreEqual(i, res);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
224 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
225
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
226 int writers = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
227 int readers = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
228 var stop = new ManualResetEvent(false);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
229 int total = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
230
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
231 const int itemsPerWriter = 10000;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
232 const int writersCount = 10;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
233
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
234 for (int i = 0; i < writersCount; i++) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
235 Interlocked.Increment(ref writers);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
236 AsyncPool
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
237 .InvokeNewThread(() => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
238 for (int ii = 0; ii < itemsPerWriter; ii++) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
239 queue.Enqueue(1);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
240 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
241 return 1;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
242 })
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
243 .Anyway(() => Interlocked.Decrement(ref writers));
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
244 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
245
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
246 for (int i = 0; i < 10; i++) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
247 Interlocked.Increment(ref readers);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
248 AsyncPool
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
249 .InvokeNewThread(() => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
250 int t;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
251 do {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
252 while (queue.TryDequeue(out t))
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
253 Interlocked.Add(ref total, t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
254 } while (writers > 0);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
255 return 1;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
256 })
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
257 .Anyway(() => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
258 Interlocked.Decrement(ref readers);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
259 if (readers == 0)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
260 stop.Set();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
261 });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
262 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
263
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
264 stop.WaitOne();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
265
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
266 Assert.AreEqual(itemsPerWriter * writersCount, total);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
267 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
268
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
269 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
270 public void ParallelMapTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
271
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
272 const int count = 100000;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
273
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
274 var args = new double[count];
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
275 var rand = new Random();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
276
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
277 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
278 args[i] = rand.NextDouble();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
279
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
280 var t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
281 var res = args.ParallelMap(x => Math.Sin(x*x), 4).Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
282
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
283 Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
284
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
285 t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
286 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
287 Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
288 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
289 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
290
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
291 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
292 public void ChainedMapTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
293
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
294 using (var pool = new WorkerPool(0,100,100)) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
295 const int count = 10000;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
296
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
297 var args = new double[count];
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
298 var rand = new Random();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
299
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
300 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
301 args[i] = rand.NextDouble();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
302
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
303 var t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
304 var res = args
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
305 .ChainedMap(
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
306 // Analysis disable once AccessToDisposedClosure
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
307 x => pool.Invoke(
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
308 () => Math.Sin(x * x)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
309 ),
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
310 4
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
311 )
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
312 .Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
313
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
314 Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
315
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
316 t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
317 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
318 Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
319 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
320 Console.WriteLine("Max workers: {0}", pool.MaxRunningThreads);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
321 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
322 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
323
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
324 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
325 public void ParallelForEachTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
326
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
327 const int count = 100000;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
328
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
329 var args = new int[count];
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
330 var rand = new Random();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
331
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
332 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
333 args[i] = (int)(rand.NextDouble() * 100);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
334
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
335 int result = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
336
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
337 var t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
338 args.ParallelForEach(x => Interlocked.Add(ref result, x), 4).Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
339
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
340 Console.WriteLine("Iteration complete in {0} ms, result: {1}", Environment.TickCount - t, result);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
341
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
342 int result2 = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
343
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
344 t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
345 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
346 result2 += args[i];
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
347 Assert.AreEqual(result2, result);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
348 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
349 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
350
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
351 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
352 public void ComplexCase1Test() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
353 var flags = new bool[3];
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
354
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
355 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
356
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
357 var step1 = PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
358 .Sleep(200, "Alan")
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
359 .Cancelled(() => flags[0] = true);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
360 var p = step1
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
361 .Chain(x =>
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
362 PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
363 .Sleep(200, "Hi, " + x)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
364 .Then(y => y)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
365 .Cancelled(() => flags[1] = true)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
366 )
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
367 .Cancelled(() => flags[2] = true);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
368 step1.Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
369 p.Cancel();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
370 try {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
371 Assert.AreEqual(p.Join(), "Hi, Alan");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
372 Assert.Fail("Shouldn't get here");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
373 } catch (OperationCanceledException) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
374 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
375
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
376 Assert.IsFalse(flags[0]);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
377 Assert.IsTrue(flags[1]);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
378 Assert.IsTrue(flags[2]);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
379 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
380
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
381 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
382 public void ChainedCancel1Test() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
383 // при отмене сцепленной асинхронной операции все обещание должно
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
384 // завершаться ошибкой OperationCanceledException
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
385 var p = PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
386 .Sleep(1, "Hi, HAL!")
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
387 .Then(x => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
388 // запускаем две асинхронные операции
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
389 var result = PromiseHelper.Sleep(1000, "HEM ENABLED!!!");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
390 // вторая операция отменяет первую до завершения
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
391 PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
392 .Sleep(100, "HAL, STOP!")
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
393 .Then(result.Cancel);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
394 return result;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
395 });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
396 try {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
397 p.Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
398 } catch (TargetInvocationException err) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
399 Assert.IsTrue(err.InnerException is OperationCanceledException);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
400 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
401 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
402
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
403 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
404 public void ChainedCancel2Test() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
405 // при отмене цепочки обещаний, вложенные операции также должны отменяться
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
406 var pSurvive = new Promise<bool>();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
407 var hemStarted = new ManualResetEvent(false);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
408 var p = PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
409 .Sleep(1, "Hi, HAL!")
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
410 .Chain(x => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
411 hemStarted.Set();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
412 // запускаем две асинхронные операции
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
413 var result = PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
414 .Sleep(10000, "HEM ENABLED!!!")
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
415 .Then(s => pSurvive.Resolve(false));
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
416
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
417 result
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
418 .Cancelled(() => pSurvive.Resolve(true));
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
419
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
420 return result;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
421 });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
422
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
423 hemStarted.WaitOne();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
424 p.Cancel();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
425
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
426 try {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
427 p.Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
428 } catch (OperationCanceledException) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
429 Assert.IsTrue(pSurvive.Join());
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
430 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
431 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
432 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
433 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
434