annotate Implab.Test/CancelationTests.cs @ 236:302ca905c19e v2

JsonReader optimizations
author cin
date Tue, 21 Nov 2017 14:57:58 +0300
parents eb793fbbe4ea
children cbe10ac0731e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
1 using System;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
2 using Implab.Parallels;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
3
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
4 #if MONO
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
5
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
6 using NUnit.Framework;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
7 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
8 using TestMethodAttribute = NUnit.Framework.TestAttribute;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
9
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
10 #else
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
11
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
12 using Microsoft.VisualStudio.TestTools.UnitTesting;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
13
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
14 #endif
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
15
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
16 namespace Implab.Test {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
17 [TestClass]
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
18 public class CancelationTests {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
19
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
20 [TestMethod]
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
21 public void PromiseCancelTest() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
22 var p = new Promise();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
23 bool requested = false;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
24 var reason = new Exception("Test");
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
25
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
26 // request cancelation
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
27 p.Cancel(reason);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
28
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
29 Assert.IsTrue(p.IsCancellationRequested);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
30 Assert.AreSame(reason, p.CancellationReason);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
31 Assert.IsFalse(p.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
32
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
33 p.CancellationRequested(r => {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
34 Assert.AreSame(reason, r);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
35 requested = true;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
36 });
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
37
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
38 Assert.IsTrue(requested);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
39
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
40 // cancel the promise
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
41 Assert.IsTrue(p.CancelOperationIfRequested());
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
42 Assert.IsTrue(p.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
43 Assert.AreSame(reason, p.Error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
44 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
45
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
46 [TestMethod]
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
47 public void CancelActionBeforeStartTask() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
48 bool run = false;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
49 var task = new ActionTask(() => {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
50 run = true;
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
51 }, null, null, true);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
52
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
53 // request cancelation
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
54 task.Cancel();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
55 Assert.IsTrue(task.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
56 task.Resolve();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
57 Assert.IsFalse(run);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
58 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
59
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
60 [TestMethod]
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
61 public void CancelActionAfterTaskStarted() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
62 var finish = new Signal();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
63 var started = new Signal();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
64
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
65 var task = new ActionTask(() => {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
66 started.Set();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
67 finish.Wait();
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
68 }, null, null, true);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
69
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
70 AsyncPool.RunThread(() => {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
71 task.Resolve();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
72 });
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
73
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
74 started.Wait(1000);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
75
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
76 task.Cancel();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
77 Assert.IsTrue(task.IsCancellationRequested);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
78 Assert.IsFalse(task.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
79 Assert.IsFalse(task.IsResolved);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
80
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
81 finish.Set();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
82 task.Join(1000);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
83
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
84 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
85
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
86 [TestMethod]
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
87 public void CancelTaskChainFromBottom() {
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
88 var started = new Signal();
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
89 var check1 = new Signal();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
90 var requested = false;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
91 var p1 = AsyncPool.RunThread(token => {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
92 token.CancellationRequested(reason => requested = true);
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
93 started.Set();
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
94 check1.Wait();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
95 token.CancelOperationIfRequested();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
96 });
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
97
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
98 started.Wait();
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
99
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
100 var p2 = p1.Then(() => {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
101 });
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
102
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
103 Assert.IsFalse(p1.IsResolved);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
104 Assert.IsFalse(p2.IsResolved);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
105
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
106 p2.Cancel();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
107
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
108 Assert.IsFalse(p2.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
109 Assert.IsFalse(p1.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
110 Assert.IsTrue(requested);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
111
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
112 check1.Set();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
113
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
114 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
115 p2.Join(1000);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
116 Assert.Fail("The chain isn't cancelled");
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
117 } catch(OperationCanceledException){
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
118 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
119
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
120 Assert.IsTrue(p1.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
121 Assert.IsTrue(p2.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
122 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
123
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
124
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
125
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
126 [TestMethod]
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
127 public void CancellableAsyncTask() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
128 var finish = new Signal();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
129 var started = new Signal();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
130
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
131 var p = AsyncPool.RunThread(token => {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
132 token.CancellationRequested(r => finish.Set());
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
133 started.Set();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
134 finish.Wait();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
135 Assert.IsTrue(token.CancelOperationIfRequested());
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
136 });
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
137
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
138 started.Wait(1000);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
139 Assert.IsFalse(p.IsResolved);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
140 p.Cancel();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
141 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
142 p.Join(1000);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
143 } catch (OperationCanceledException) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
144 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
145 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
146 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
147 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
148