annotate Implab.Test/CancelationTests.cs @ 145:706fccb85524 v2

RC: cancellation support for promises + tests
author cin
date Sun, 08 Mar 2015 02:52:27 +0300
parents
children eb793fbbe4ea
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;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
51 }, null, null);
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();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
68 }, null, null);
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() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
88 var check1 = new Signal();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
89 var requested = false;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
90 var p1 = AsyncPool.RunThread(token => {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
91 token.CancellationRequested(reason => requested = true);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
92 check1.Wait();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
93 token.CancelOperationIfRequested();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
94 });
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
95
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
96 var p2 = p1.Then(() => {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
97 });
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
98
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
99 Assert.IsFalse(p1.IsResolved);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
100 Assert.IsFalse(p2.IsResolved);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
101
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
102 p2.Cancel();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
103
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
104 Assert.IsFalse(p2.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
105 Assert.IsFalse(p1.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
106 Assert.IsTrue(requested);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
107
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
108 check1.Set();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
109
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
110 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
111 p2.Join(1000);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
112 Assert.Fail("The chain isn't cancelled");
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
113 } catch(OperationCanceledException){
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
114 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
115
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
116 Assert.IsTrue(p1.IsCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
117 Assert.IsTrue(p2.IsCancelled);
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
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
121
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
122 [TestMethod]
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
123 public void CancellableAsyncTask() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
124 var finish = new Signal();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
125 var started = new Signal();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
126
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
127 var p = AsyncPool.RunThread(token => {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
128 token.CancellationRequested(r => finish.Set());
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
129 started.Set();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
130 finish.Wait();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
131 Assert.IsTrue(token.CancelOperationIfRequested());
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
132 });
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
133
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
134 started.Wait(1000);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
135 Assert.IsFalse(p.IsResolved);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
136 p.Cancel();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
137 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
138 p.Join(1000);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
139 } catch (OperationCanceledException) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
140 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
141 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
142 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
143 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
144