annotate Implab.Test/RunnableComponentTests.cs @ 186:75103928da09 ref20160224

working on cancelation and error handling
author cin
date Tue, 19 Apr 2016 00:50:14 +0300
parents 822aab37b107
children 3071220371f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
1 using System;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
2 using System.Reflection;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
3 using System.Threading;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
4 using Implab.Parallels;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
5 using Implab.Components;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
6
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
7 #if MONO
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
8
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
9 using NUnit.Framework;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
10 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
11 using TestMethodAttribute = NUnit.Framework.TestAttribute;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
12
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
13 #else
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
14
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
15 using Microsoft.VisualStudio.TestTools.UnitTesting;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
16
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
17 #endif
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
18
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
19 namespace Implab.Test {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
20 [TestClass]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
21 public class RunnableComponentTests {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
22
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
23 static void ShouldThrow(Action action) {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
24 try {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
25 action();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
26 Assert.Fail();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
27 } catch(AssertionException) {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
28 throw;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
29 } catch {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
30 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
31 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
32
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
33 class Runnable : RunnableComponent {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
34 public Runnable(bool initialized) : base(initialized) {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
35 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
36
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
37 public Action MockInit {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
38 get;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
39 set;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
40 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
41
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
42 public Func<IPromise> MockStart {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
43 get;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
44 set;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
45 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
46
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
47 public Func<IPromise> MockStop {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
48 get;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
49 set;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
50 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
51
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
52 protected override IPromise OnStart() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
53 return MockStart != null ? MockStart() : base.OnStart();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
54 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
55
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
56 protected override IPromise OnStop() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
57 return MockStop != null ? MockStop() : base.OnStart();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
58 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
59
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
60 protected override void OnInitialize() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
61 if (MockInit != null)
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
62 MockInit();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
63 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
64 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
65
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
66 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
67 public void NormalFlowTest() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
68 var comp = new Runnable(false);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
69
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
70 Assert.AreEqual(ExecutionState.Created, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
71
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
72 comp.Init();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
73
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
74 Assert.AreEqual(ExecutionState.Ready, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
75
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
76 comp.Start().Join(1000);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
77
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
78 Assert.AreEqual(ExecutionState.Running, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
79
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
80 comp.Stop().Join(1000);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
81
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
82 Assert.AreEqual(ExecutionState.Disposed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
83
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
84 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
85
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
86 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
87 public void InitFailTest() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
88 var comp = new Runnable(false) {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
89 MockInit = () => {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
90 throw new Exception("BAD");
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
91 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
92 };
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
93
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
94 ShouldThrow(() => comp.Start());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
95 ShouldThrow(() => comp.Stop());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
96 Assert.AreEqual(ExecutionState.Created, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
97
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
98 ShouldThrow(comp.Init);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
99
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
100 Assert.AreEqual(ExecutionState.Failed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
101
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
102 ShouldThrow(() => comp.Start());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
103 ShouldThrow(() => comp.Stop());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
104 Assert.AreEqual(ExecutionState.Failed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
105
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
106 comp.Dispose();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
107 Assert.AreEqual(ExecutionState.Disposed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
108 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
109
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
110 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
111 public void DisposedTest() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
112
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
113 var comp = new Runnable(false);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
114 comp.Dispose();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
115
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
116 ShouldThrow(() => comp.Start());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
117 ShouldThrow(() => comp.Stop());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
118 ShouldThrow(comp.Init);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
119
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
120 Assert.AreEqual(ExecutionState.Disposed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
121 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
122
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
123 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
124 public void StartCancelTest() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
125 var comp = new Runnable(true) {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
126 MockStart = () => PromiseHelper.Sleep(100000, 0)
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
127 };
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
128
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
129 var p = comp.Start();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
130 Assert.AreEqual(ExecutionState.Starting, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
131 p.Cancel();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
132 ShouldThrow(() => p.Join(1000));
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
133 Assert.AreEqual(ExecutionState.Failed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
134 Assert.IsInstanceOfType(typeof(OperationCanceledException), comp.LastError);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
135
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
136 comp.Dispose();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
137 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
138
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
139 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
140 public void StartStopTest() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
141 var stop = new Signal();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
142 var comp = new Runnable(true) {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
143 MockStart = () => PromiseHelper.Sleep(100000, 0),
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
144 MockStop = () => AsyncPool.RunThread(stop.Wait)
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
145 };
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
146
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
147 var p1 = comp.Start();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
148 var p2 = comp.Stop();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
149 // should enter stopping state
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
150
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
151 ShouldThrow(p1.Join);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
152 Assert.IsTrue(p1.IsCancelled);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
153 Assert.AreEqual(ExecutionState.Stopping, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
154
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
155 stop.Set();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
156 p2.Join(1000);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
157 Assert.AreEqual(ExecutionState.Disposed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
158 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
159
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
160 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
161 public void StartStopFailTest() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
162 var comp = new Runnable(true) {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
163 MockStart = () => PromiseHelper.Sleep(100000, 0).Then(null,null,x => { throw new Exception("I'm dead"); })
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
164 };
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
165
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
166 comp.Start();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
167 var p = comp.Stop();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
168 // if Start fails to cancel, should fail to stop
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
169 ShouldThrow(() => p.Join(1000));
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
170 Assert.AreEqual(ExecutionState.Failed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
171 Assert.IsNotNull(comp.LastError);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
172 Assert.AreEqual("I'm dead", comp.LastError.Message);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
173 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
174
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
175 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
176 public void StopCancelTest() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
177 var comp = new Runnable(true) {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
178 MockStop = () => PromiseHelper.Sleep(100000, 0)
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
179 };
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
180
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
181 comp.Start();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
182 var p = comp.Stop();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
183 Assert.AreEqual(ExecutionState.Stopping, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
184 p.Cancel();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
185 ShouldThrow(() => p.Join(1000));
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
186 Assert.AreEqual(ExecutionState.Failed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
187 Assert.IsInstanceOfType(typeof(OperationCanceledException), comp.LastError);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
188
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
189 comp.Dispose();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
190 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
191
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
192 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
193 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
194