comparison Implab.Test/CancelationTests.cs @ 192:f1da3afc3521 release v2.1

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