annotate Implab.Test/AsyncTests.cs @ 235:b49969a7043c v2

Слияние
author cin
date Thu, 05 Oct 2017 09:24:49 +0300
parents d6fe09f5592c
children
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;
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
10 using TestMethodAttribute = NUnit.Framework.TestAttribute;
77
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>();
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
54 p.CancelOperation(null);
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
55
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
56 var p2 = p.Then(x => x, null, reason => {
77
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>();
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
72 p.CancelOperation(null);
77
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
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
75 .Then(x => x, null, reason => {
77
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 })
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
78 .Then(x => x, e => true);
77
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
119 var p2 = p.Then(x => x, e => 101);
77
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]
105
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
142 public void ChainFailTest() {
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
143 var p1 = new Promise<int>();
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
144
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
145 var p3 = p1.Chain(x => {
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
146 var p2 = new Promise<string>();
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
147 p2.Reject(new Exception("DIE!!!"));
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
148 return p2;
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
149 });
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
150
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
151 p1.Resolve(100);
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
152
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
153 Assert.IsTrue(p3.IsResolved);
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
154 }
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
155
4d308952fd5e minor fixes
cin
parents: 81
diff changeset
156 [TestMethod]
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
157 public void PoolTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
158 var pid = Thread.CurrentThread.ManagedThreadId;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
159 var p = AsyncPool.Invoke(() => Thread.CurrentThread.ManagedThreadId);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
160
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
161 Assert.AreNotEqual(pid, p.Join());
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
162 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
163
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
164 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
165 public void WorkerPoolSizeTest() {
81
2c5631b43c7d dispatch pool rewritten
cin
parents: 80
diff changeset
166 var pool = new WorkerPool(5, 10, 1);
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
167
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
168 Assert.AreEqual(5, pool.PoolSize);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
169
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
170 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
171 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
172 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
173
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
174 Assert.AreEqual(5, pool.PoolSize);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
175
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
176 for (int i = 0; i < 100; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
177 pool.Invoke(() => { Thread.Sleep(100000000); return 10; });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
178 Thread.Sleep(200);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
179 Assert.AreEqual(10, pool.PoolSize);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
180
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
181 pool.Dispose();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
182 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
183
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
184 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
185 public void WorkerPoolCorrectTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
186 var pool = new WorkerPool(0,1000,100);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
187
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
188 const int iterations = 1000;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
189 int pending = iterations;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
190 var stop = new ManualResetEvent(false);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
191
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
192 var count = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
193 for (int i = 0; i < iterations; i++) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
194 pool
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
195 .Invoke(() => 1)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
196 .Then(x => Interlocked.Add(ref count, x))
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
197 .Then(x => Math.Log10(x))
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
198 .On(() => {
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
199 Interlocked.Decrement(ref pending);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
200 if (pending == 0)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
201 stop.Set();
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
202 }, PromiseEventType.All);
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
203 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
204
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
205 stop.WaitOne();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
206
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
207 Assert.AreEqual(iterations, count);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
208 Console.WriteLine("Max threads: {0}", pool.MaxRunningThreads);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
209 pool.Dispose();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
210
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
211 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
212
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
213 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
214 public void WorkerPoolDisposeTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
215 var pool = new WorkerPool(5, 20);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
216 Assert.AreEqual(5, pool.PoolSize);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
217 pool.Dispose();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
218 Thread.Sleep(500);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
219 Assert.AreEqual(0, pool.PoolSize);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
220 pool.Dispose();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
221 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
222
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
223 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
224 public void MTQueueTest() {
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
225 var queue = new SimpleAsyncQueue<int>();
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
226 int res;
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
227
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
228 queue.Enqueue(10);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
229 Assert.IsTrue(queue.TryDequeue(out res));
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
230 Assert.AreEqual(10, res);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
231 Assert.IsFalse(queue.TryDequeue(out res));
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
232
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
233 for (int i = 0; i < 1000; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
234 queue.Enqueue(i);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
235
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
236 for (int i = 0; i < 1000; i++) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
237 queue.TryDequeue(out res);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
238 Assert.AreEqual(i, res);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
239 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
240
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
241 int writers = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
242 int readers = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
243 var stop = new ManualResetEvent(false);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
244 int total = 0;
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
245 var ticks = Environment.TickCount;
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
246
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
247 const int itemsPerWriter = 1000000;
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
248 const int writersCount = 10;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
249
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
250 for (int i = 0; i < writersCount; i++) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
251 Interlocked.Increment(ref writers);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
252 AsyncPool
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
253 .RunThread(() => {
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
254 for (int ii = 0; ii < itemsPerWriter; ii++) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
255 queue.Enqueue(1);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
256 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
257 return 1;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
258 })
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
259 .On(() => Interlocked.Decrement(ref writers), PromiseEventType.All);
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
260 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
261
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
262 for (int i = 0; i < 10; i++) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
263 Interlocked.Increment(ref readers);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
264 AsyncPool
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
265 .RunThread(() => {
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
266 int t;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
267 do {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
268 while (queue.TryDequeue(out t))
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
269 Interlocked.Add(ref total, t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
270 } while (writers > 0);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
271 return 1;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
272 })
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
273 .On(() => {
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
274 Interlocked.Decrement(ref readers);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
275 if (readers == 0)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
276 stop.Set();
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
277 }, PromiseEventType.All);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
278 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
279
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
280 stop.WaitOne();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
281
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
282 Console.WriteLine("{0} in {1}ms", total, Environment.TickCount - ticks);
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
283
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
284 Assert.AreEqual(itemsPerWriter * writersCount, total);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
285 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
286
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
287 [TestMethod]
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
288 public void AsyncQueueTest() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
289 var queue = new AsyncQueue<int>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
290 int res;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
291
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
292 queue.Enqueue(10);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
293 Assert.IsTrue(queue.TryDequeue(out res));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
294 Assert.AreEqual(10, res);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
295 Assert.IsFalse(queue.TryDequeue(out res));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
296
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
297 for (int i = 0; i < 1000; i++)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
298 queue.Enqueue(i);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
299
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
300 for (int i = 0; i < 1000; i++) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
301 queue.TryDequeue(out res);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
302 Assert.AreEqual(i, res);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
303 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
304
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
305 const int count = 10000000;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
306
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
307 int res1 = 0, res2 = 0;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
308 var t1 = Environment.TickCount;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
309
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
310 AsyncPool.RunThread(
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
311 () => {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
312 for (var i = 0; i < count; i++)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
313 queue.Enqueue(1);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
314 Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
315 },
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
316 () => {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
317 for (var i = 0; i < count; i++)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
318 queue.Enqueue(2);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
319 Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
320 },
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
321 () => {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
322 int temp;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
323 int i = 0;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
324 while (i < count)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
325 if (queue.TryDequeue(out temp)) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
326 i++;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
327 res1 += temp;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
328 }
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
329 Console.WriteLine("done reader #1: {0} ms", Environment.TickCount - t1);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
330 },
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
331 () => {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
332 int temp;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
333 int i = 0;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
334 while (i < count)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
335 if (queue.TryDequeue(out temp)) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
336 i++;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
337 res2 += temp;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
338 }
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
339 Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
340 }
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
341 )
205
8200ab154c8a Added ResetState to RunnableComponent to reset in case of failure
cin
parents: 151
diff changeset
342 .PromiseAll()
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
343 .Join();
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
344
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
345 Assert.AreEqual(count * 3, res1 + res2);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
346
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
347 Console.WriteLine(
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
348 "done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}",
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
349 Environment.TickCount - t1,
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
350 res1,
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
351 res2,
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
352 res1 + res2,
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
353 count
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
354 );
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
355 }
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
356
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
357 [TestMethod]
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
358 public void AsyncQueueBatchTest() {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
359 var queue = new AsyncQueue<int>();
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
360
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
361 const int wBatch = 29;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
362 const int wCount = 400000;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
363 const int total = wBatch * wCount * 2;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
364 const int summ = wBatch * wCount * 3;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
365
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
366 int r1 = 0, r2 = 0;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
367 const int rBatch = 111;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
368 int read = 0;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
369
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
370 var t1 = Environment.TickCount;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
371
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
372 AsyncPool.RunThread(
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
373 () => {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
374 var buffer = new int[wBatch];
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
375 for(int i = 0; i<wBatch; i++)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
376 buffer[i] = 1;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
377
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
378 for(int i =0; i < wCount; i++)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
379 queue.EnqueueRange(buffer,0,wBatch);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
380 Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
381 },
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
382 () => {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
383 var buffer = new int[wBatch];
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
384 for(int i = 0; i<wBatch; i++)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
385 buffer[i] = 2;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
386
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
387 for(int i =0; i < wCount; i++)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
388 queue.EnqueueRange(buffer,0,wBatch);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
389 Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
390 },
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
391 () => {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
392 var buffer = new int[rBatch];
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
393
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
394 while(read < total) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
395 int actual;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
396 if (queue.TryDequeueRange(buffer,0,rBatch,out actual)) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
397 for(int i=0; i< actual; i++)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
398 r1 += buffer[i];
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
399 Interlocked.Add(ref read, actual);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
400 }
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
401 }
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
402
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
403 Console.WriteLine("done reader #1: {0} ms", Environment.TickCount - t1);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
404 },
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
405 () => {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
406 var buffer = new int[rBatch];
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
407
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
408 while(read < total) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
409 int actual;
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
410 if (queue.TryDequeueRange(buffer,0,rBatch,out actual)) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
411 for(int i=0; i< actual; i++)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
412 r2 += buffer[i];
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
413 Interlocked.Add(ref read, actual);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
414 }
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
415 }
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
416
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
417 Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
418 }
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
419 )
205
8200ab154c8a Added ResetState to RunnableComponent to reset in case of failure
cin
parents: 151
diff changeset
420 .PromiseAll()
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
421 .Join();
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
422
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
423 Assert.AreEqual(summ , r1 + r2);
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
424
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
425 Console.WriteLine(
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
426 "done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}",
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
427 Environment.TickCount - t1,
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
428 r1,
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
429 r2,
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
430 r1 + r2,
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
431 total
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
432 );
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
433 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
434
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
435 [TestMethod]
122
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
436 public void AsyncQueueChunkDequeueTest() {
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
437 var queue = new AsyncQueue<int>();
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
438
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
439 const int wBatch = 31;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
440 const int wCount = 200000;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
441 const int total = wBatch * wCount * 3;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
442 const int summ = wBatch * wCount * 6;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
443
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
444 int r1 = 0, r2 = 0;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
445 const int rBatch = 1024;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
446 int read = 0;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
447
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
448 var t1 = Environment.TickCount;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
449
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
450 AsyncPool.RunThread(
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
451 () => {
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
452 var buffer = new int[wBatch];
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
453 for(int i = 0; i<wBatch; i++)
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
454 buffer[i] = 1;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
455
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
456 for(int i =0; i < wCount; i++)
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
457 queue.EnqueueRange(buffer,0,wBatch);
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
458 Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1);
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
459 },
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
460 () => {
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
461 var buffer = new int[wBatch];
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
462 for(int i = 0; i<wBatch; i++)
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
463 buffer[i] = 2;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
464
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
465 for(int i =0; i < wCount; i++)
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
466 queue.EnqueueRange(buffer,0,wBatch);
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
467 Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1);
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
468 },
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
469 () => {
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
470 var buffer = new int[wBatch];
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
471 for(int i = 0; i<wBatch; i++)
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
472 buffer[i] = 3;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
473
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
474 for(int i =0; i < wCount; i++)
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
475 queue.EnqueueRange(buffer,0,wBatch);
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
476 Console.WriteLine("done writer #3: {0} ms", Environment.TickCount - t1);
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
477 },
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
478 () => {
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
479 var buffer = new int[rBatch];
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
480 int count = 1;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
481 double avgchunk = 0;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
482 while(read < total) {
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
483 int actual;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
484 if (queue.TryDequeueChunk(buffer,0,rBatch,out actual)) {
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
485 for(int i=0; i< actual; i++)
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
486 r2 += buffer[i];
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
487 Interlocked.Add(ref read, actual);
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
488 avgchunk = avgchunk*(count-1)/count + actual/(double)count;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
489 count ++;
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
490 }
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
491 }
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
492
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
493 Console.WriteLine("done reader #2: {0} ms, avg chunk size: {1}", Environment.TickCount - t1, avgchunk);
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
494 }
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
495 )
205
8200ab154c8a Added ResetState to RunnableComponent to reset in case of failure
cin
parents: 151
diff changeset
496 .PromiseAll()
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
497 .Join();
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
498
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
499 Assert.AreEqual(summ , r1 + r2);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
500
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
501 Console.WriteLine(
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
502 "done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}",
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
503 Environment.TickCount - t1,
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
504 r1,
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
505 r2,
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
506 r1 + r2,
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
507 total
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
508 );
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
509 }
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
510
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
511 [TestMethod]
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
512 public void AsyncQueueDrainTest() {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
513 var queue = new AsyncQueue<int>();
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
514
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
515 const int wBatch = 32;
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
516 const int wCount = 200000;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
517 const int total = wBatch * wCount * 3;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
518 const int summ = wBatch * wCount * 3;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
519
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
520 int r1 = 0, r2 = 0;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
521 int read = 0;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
522
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
523 var t1 = Environment.TickCount;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
524
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
525 AsyncPool.RunThread(
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
526 () => {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
527 var buffer = new int[wBatch];
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
528 for(int i = 0; i<wBatch; i++)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
529 buffer[i] = 1;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
530
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
531 for(int i =0; i < wCount; i++)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
532 queue.EnqueueRange(buffer,0,wBatch);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
533 Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
534 },
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
535 () => {
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
536 var buffer = new int[wBatch];
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
537 for (int i = 0; i < wBatch; i++)
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
538 buffer[i] = 1;
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
539
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
540 for (int i = 0; i < wCount; i++)
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
541 queue.EnqueueRange(buffer, 0, wBatch);
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
542 Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
543 },
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
544 () => {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
545 var buffer = new int[wBatch];
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
546 for(int i = 0; i<wBatch; i++)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
547 buffer[i] = 1;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
548
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
549 for(int i =0; i < wCount; i++)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
550 queue.EnqueueRange(buffer,0,wBatch);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
551 Console.WriteLine("done writer #3: {0} ms", Environment.TickCount - t1);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
552 },
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
553 /*() => {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
554 int temp;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
555 int count = 0;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
556 while (read < total)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
557 if (queue.TryDequeue(out temp)) {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
558 count++;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
559 r1 += temp;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
560 Interlocked.Increment(ref read);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
561 }
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
562 Console.WriteLine("done reader #1: {0} ms, {1} count", Environment.TickCount - t1, count);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
563 },*/
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
564 /*() => {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
565 var buffer = new int[rBatch];
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
566 var count = 0;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
567 while(read < total) {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
568 int actual;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
569 if (queue.TryDequeueRange(buffer,0,rBatch,out actual)) {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
570 for(int i=0; i< actual; i++)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
571 r1 += buffer[i];
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
572 Interlocked.Add(ref read, actual);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
573 count += actual;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
574 }
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
575 }
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
576
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
577 Console.WriteLine("done reader #1: {0} ms, {1} items", Environment.TickCount - t1, count);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
578 },*/
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
579 () => {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
580 var count = 0;
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
581 int emptyDrains = 0;
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
582
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
583 while (read < total) {
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
584 var buffer = queue.Drain();
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
585 if (buffer.Count == 0)
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
586 emptyDrains++;
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
587 for(int i=0; i< buffer.Count; i++)
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
588 r1 += buffer[i];
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
589 Interlocked.Add(ref read, buffer.Count);
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
590 count += buffer.Count;
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
591 }
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
592 Console.WriteLine("done reader #1: {0} ms, {1} items, empty: {2}", Environment.TickCount - t1, count, emptyDrains);
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
593 },
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
594 () => {
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
595 var count = 0;
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
596 int emptyDrains = 0;
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
597
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
598 while (read < total) {
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
599 var buffer = queue.Drain();
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
600 if (buffer.Count == 0)
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
601 emptyDrains++;
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
602
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
603 for (int i=0; i< buffer.Count; i++)
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
604 r2 += buffer[i];
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
605 Interlocked.Add(ref read, buffer.Count);
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
606 count += buffer.Count;
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
607 }
233
d6fe09f5592c Improved AsyncQueue
cin
parents: 205
diff changeset
608 Console.WriteLine("done reader #2: {0} ms, {1} items, empty: {2}", Environment.TickCount - t1, count, emptyDrains);
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
609 }
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 122
diff changeset
610 )
205
8200ab154c8a Added ResetState to RunnableComponent to reset in case of failure
cin
parents: 151
diff changeset
611 .PromiseAll()
122
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
612 .Join();
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
613
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
614 Assert.AreEqual(summ , r1 + r2);
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
615
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
616 Console.WriteLine(
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
617 "done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}",
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
618 Environment.TickCount - t1,
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
619 r1,
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
620 r2,
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
621 r1 + r2,
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
622 total
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
623 );
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
624 }
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
625
0c8685c8b56b minor fixes and improvements of AsyncQueue, additional tests
cin
parents: 121
diff changeset
626 [TestMethod]
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
627 public void ParallelMapTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
628
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
629 const int count = 100000;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
630
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
631 var args = new double[count];
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
632 var rand = new Random();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
633
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
634 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
635 args[i] = rand.NextDouble();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
636
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
637 var t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
638 var res = args.ParallelMap(x => Math.Sin(x*x), 4).Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
639
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
640 Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
641
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
642 t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
643 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
644 Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
645 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
646 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
647
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
648 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
649 public void ChainedMapTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
650
125
f803565868a4 improved performance of promises
cin
parents: 124
diff changeset
651 using (var pool = new WorkerPool()) {
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
652 const int count = 10000;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
653
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
654 var args = new double[count];
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
655 var rand = new Random();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
656
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
657 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
658 args[i] = rand.NextDouble();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
659
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
660 var t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
661 var res = args
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
662 .ChainedMap(
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
663 // Analysis disable once AccessToDisposedClosure
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
664 x => pool.Invoke(
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
665 () => Math.Sin(x * x)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
666 ),
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
667 4
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
668 )
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
669 .Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
670
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
671 Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
672
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
673 t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
674 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
675 Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
676 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
677 Console.WriteLine("Max workers: {0}", pool.MaxRunningThreads);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
678 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
679 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
680
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
681 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
682 public void ParallelForEachTest() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
683
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
684 const int count = 100000;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
685
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
686 var args = new int[count];
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
687 var rand = new Random();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
688
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
689 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
690 args[i] = (int)(rand.NextDouble() * 100);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
691
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
692 int result = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
693
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
694 var t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
695 args.ParallelForEach(x => Interlocked.Add(ref result, x), 4).Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
696
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
697 Console.WriteLine("Iteration complete in {0} ms, result: {1}", Environment.TickCount - t, result);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
698
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
699 int result2 = 0;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
700
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
701 t = Environment.TickCount;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
702 for (int i = 0; i < count; i++)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
703 result2 += args[i];
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
704 Assert.AreEqual(result2, result);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
705 Console.WriteLine("Verified in {0} ms", Environment.TickCount - t);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
706 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
707
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
708 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
709 public void ComplexCase1Test() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
710 var flags = new bool[3];
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
711
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
712 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
713
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
714 var step1 = PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
715 .Sleep(200, "Alan")
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
716 .On(() => flags[0] = true, PromiseEventType.Cancelled);
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
717 var p = step1
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
718 .Chain(x =>
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
719 PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
720 .Sleep(200, "Hi, " + x)
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
721 .Then(y => y)
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
722 .On(() => flags[1] = true, PromiseEventType.Cancelled)
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
723 )
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 106
diff changeset
724 .On(() => flags[2] = true, PromiseEventType.Cancelled);
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
725 step1.Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
726 p.Cancel();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
727 try {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
728 Assert.AreEqual(p.Join(), "Hi, Alan");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
729 Assert.Fail("Shouldn't get here");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
730 } catch (OperationCanceledException) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
731 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
732
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
733 Assert.IsFalse(flags[0]);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
734 Assert.IsTrue(flags[1]);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
735 Assert.IsTrue(flags[2]);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
736 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
737
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
738 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
739 public void ChainedCancel1Test() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
740 // при отмене сцепленной асинхронной операции все обещание должно
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
741 // завершаться ошибкой OperationCanceledException
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
742 var p = PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
743 .Sleep(1, "Hi, HAL!")
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
744 .Then(x => {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
745 // запускаем две асинхронные операции
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
746 var result = PromiseHelper.Sleep(1000, "HEM ENABLED!!!");
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
747 // вторая операция отменяет первую до завершения
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
748 PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
749 .Sleep(100, "HAL, STOP!")
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
750 .Then(result.Cancel);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
751 return result;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
752 });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
753 try {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
754 p.Join();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
755 } catch (TargetInvocationException err) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
756 Assert.IsTrue(err.InnerException is OperationCanceledException);
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
757 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
758 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
759
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
760 [TestMethod]
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
761 public void ChainedCancel2Test() {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
762 // при отмене цепочки обещаний, вложенные операции также должны отменяться
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
763 var pSurvive = new Promise<bool>();
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
764 var hemStarted = new Signal();
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
765 var p = PromiseHelper
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
766 .Sleep(1, "Hi, HAL!")
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
767 .Chain(() => {
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
768 hemStarted.Set();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
769 // запускаем две асинхронные операции
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
770 var result = PromiseHelper
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
771 .Sleep(2000, "HEM ENABLED!!!")
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
772 .Then(() => pSurvive.Resolve(false));
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
773
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
774 result
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
775 .On(() => pSurvive.Resolve(true), PromiseEventType.Cancelled);
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
776
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
777 return result;
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
778 });
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
779
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
780 hemStarted.Wait();
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
781 p.Cancel();
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
782
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
783 try {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
784 p.Join();
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
785 Assert.Fail();
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
786 } catch (OperationCanceledException) {
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
787 }
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
788 Assert.IsTrue(pSurvive.Join());
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
789 }
136
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
790
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
791 [TestMethod]
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
792 public void SharedLockTest() {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
793 var l = new SharedLock();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
794 int shared = 0;
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
795 int exclusive = 0;
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
796 var s1 = new Signal();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
797 var log = new AsyncQueue<string>();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
798
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
799 try {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
800 AsyncPool.RunThread(
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
801 () => {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
802 log.Enqueue("Reader #1 started");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
803 try {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
804 l.LockShared();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
805 log.Enqueue("Reader #1 lock got");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
806 if (Interlocked.Increment(ref shared) == 2)
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
807 s1.Set();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
808 s1.Wait();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
809 log.Enqueue("Reader #1 finished");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
810 Interlocked.Decrement(ref shared);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
811 } finally {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
812 l.Release();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
813 log.Enqueue("Reader #1 lock released");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
814 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
815 },
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
816 () => {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
817 log.Enqueue("Reader #2 started");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
818
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
819 try {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
820 l.LockShared();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
821 log.Enqueue("Reader #2 lock got");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
822
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
823 if (Interlocked.Increment(ref shared) == 2)
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
824 s1.Set();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
825 s1.Wait();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
826 log.Enqueue("Reader #2 upgrading to writer");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
827 Interlocked.Decrement(ref shared);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
828 l.Upgrade();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
829 log.Enqueue("Reader #2 upgraded");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
830
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
831 Assert.AreEqual(1, Interlocked.Increment(ref exclusive));
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
832 Assert.AreEqual(0, shared);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
833 log.Enqueue("Reader #2 finished");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
834 Interlocked.Decrement(ref exclusive);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
835 } finally {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
836 l.Release();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
837 log.Enqueue("Reader #2 lock released");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
838 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
839 },
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
840 () => {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
841 log.Enqueue("Writer #1 started");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
842 try {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
843 l.LockExclusive();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
844 log.Enqueue("Writer #1 got the lock");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
845 Assert.AreEqual(1, Interlocked.Increment(ref exclusive));
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
846 Interlocked.Decrement(ref exclusive);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
847 log.Enqueue("Writer #1 is finished");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
848 } finally {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
849 l.Release();
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
850 log.Enqueue("Writer #1 lock released");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
851 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
852 }
205
8200ab154c8a Added ResetState to RunnableComponent to reset in case of failure
cin
parents: 151
diff changeset
853 ).PromiseAll().Join(1000);
136
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
854 log.Enqueue("Done");
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
855 } catch(Exception error) {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
856 log.Enqueue(error.Message);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
857 throw;
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
858 } finally {
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
859 foreach (var m in log)
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
860 Console.WriteLine(m);
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
861 }
e9e7940c7d98 shared locks + tests
cin
parents: 125
diff changeset
862 }
151
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
863
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
864 #if NET_4_5
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
865
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
866 [TestMethod]
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
867 public async void TaskInteropTest() {
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
868 var promise = new Promise<int>();
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
869 promise.Resolve(10);
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
870 var res = await promise;
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
871
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
872 Assert.AreEqual(10, res);
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
873 }
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
874
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
875 #endif
77
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
876 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
877 }
91362ffbecf8 ported tests to mono
cin
parents: 76
diff changeset
878