Mercurial > pub > ImplabNet
annotate Implab.Test/RunnableComponentTests.cs @ 217:6efb77590b15 v2
More verbose output with TextFileListener
author | cin |
---|---|
date | Tue, 02 May 2017 17:33:04 +0300 |
parents | 7d07503621fe |
children |
rev | line source |
---|---|
185 | 1 using System; |
2 using System.Reflection; | |
3 using System.Threading; | |
4 using Implab.Parallels; | |
5 using Implab.Components; | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
6 using Implab.Test.Mock; |
185 | 7 |
8 #if MONO | |
9 | |
10 using NUnit.Framework; | |
11 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; | |
12 using TestMethodAttribute = NUnit.Framework.TestAttribute; | |
188 | 13 using AssertFailedException = NUnit.Framework.AssertionException; |
185 | 14 #else |
15 | |
16 using Microsoft.VisualStudio.TestTools.UnitTesting; | |
17 | |
18 #endif | |
19 | |
20 namespace Implab.Test { | |
21 [TestClass] | |
22 public class RunnableComponentTests { | |
23 | |
24 static void ShouldThrow(Action action) { | |
25 try { | |
26 action(); | |
188 | 27 Assert.Fail(); |
28 } catch (AssertFailedException) { | |
185 | 29 throw; |
30 } catch { | |
31 } | |
32 } | |
33 | |
34 | |
35 | |
36 [TestMethod] | |
37 public void NormalFlowTest() { | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
38 var comp = new MockRunnableComponent(false); |
185 | 39 |
40 Assert.AreEqual(ExecutionState.Created, comp.State); | |
41 | |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
42 comp.Initialize(); |
185 | 43 |
44 Assert.AreEqual(ExecutionState.Ready, comp.State); | |
45 | |
46 comp.Start().Join(1000); | |
47 | |
48 Assert.AreEqual(ExecutionState.Running, comp.State); | |
49 | |
50 comp.Stop().Join(1000); | |
51 | |
52 Assert.AreEqual(ExecutionState.Disposed, comp.State); | |
53 | |
54 } | |
55 | |
56 [TestMethod] | |
57 public void InitFailTest() { | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
58 var comp = new MockRunnableComponent(false) { |
185 | 59 MockInit = () => { |
60 throw new Exception("BAD"); | |
61 } | |
62 }; | |
63 | |
64 ShouldThrow(() => comp.Start()); | |
65 ShouldThrow(() => comp.Stop()); | |
66 Assert.AreEqual(ExecutionState.Created, comp.State); | |
67 | |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
68 ShouldThrow(comp.Initialize); |
185 | 69 |
70 Assert.AreEqual(ExecutionState.Failed, comp.State); | |
71 | |
72 ShouldThrow(() => comp.Start()); | |
73 ShouldThrow(() => comp.Stop()); | |
74 Assert.AreEqual(ExecutionState.Failed, comp.State); | |
75 | |
76 comp.Dispose(); | |
77 Assert.AreEqual(ExecutionState.Disposed, comp.State); | |
78 } | |
79 | |
80 [TestMethod] | |
81 public void DisposedTest() { | |
82 | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
83 var comp = new MockRunnableComponent(false); |
185 | 84 comp.Dispose(); |
85 | |
86 ShouldThrow(() => comp.Start()); | |
87 ShouldThrow(() => comp.Stop()); | |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
88 ShouldThrow(comp.Initialize); |
185 | 89 |
90 Assert.AreEqual(ExecutionState.Disposed, comp.State); | |
91 } | |
92 | |
93 [TestMethod] | |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
94 public void ShouldCallDisposeOnStop() { |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
95 var comp = new MockRunnableComponent(true); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
96 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
97 bool disposed = false; |
208
7d07503621fe
RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents:
205
diff
changeset
|
98 comp.MockDispose = (disposing) => { |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
99 disposed = true; |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
100 }; |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
101 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
102 comp.Start().Join(1000); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
103 comp.Stop().Join(1000); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
104 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
105 ShouldThrow(() => comp.Start()); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
106 ShouldThrow(() => comp.Stop()); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
107 ShouldThrow(comp.Initialize); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
108 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
109 Assert.AreEqual(ExecutionState.Disposed, comp.State); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
110 Assert.IsTrue(disposed); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
111 } |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
112 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
113 [TestMethod] |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
114 public void ShouldNotCallDisposeOnStop() { |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
115 var comp = new MockRunnableComponent(true, true); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
116 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
117 bool disposed = false; |
208
7d07503621fe
RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents:
205
diff
changeset
|
118 comp.MockDispose = (disposing) => { |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
119 disposed = true; |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
120 }; |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
121 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
122 comp.Start().Join(1000); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
123 comp.Stop().Join(1000); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
124 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
125 Assert.AreEqual(ExecutionState.Ready, comp.State); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
126 Assert.IsFalse(disposed); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
127 } |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
128 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
129 [TestMethod] |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
130 public void SelfDisposeOnStop() { |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
131 var comp = new MockRunnableComponent(true, true); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
132 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
133 bool disposed = false; |
208
7d07503621fe
RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents:
205
diff
changeset
|
134 comp.MockDispose = (disposing) => { |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
135 disposed = true; |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
136 }; |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
137 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
138 comp.Start().Join(1000); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
139 comp.Stop().Join(1000); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
140 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
141 Assert.AreEqual(ExecutionState.Ready, comp.State); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
142 Assert.IsFalse(disposed); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
143 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
144 comp.MockStop = () => { |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
145 comp.Dispose(); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
146 return Promise.Success; |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
147 }; |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
148 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
149 comp.Start().Join(1000); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
150 comp.Stop().Join(1000); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
151 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
152 Assert.AreEqual(ExecutionState.Disposed, comp.State); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
153 Assert.IsTrue(disposed); |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
154 } |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
155 |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
156 [TestMethod] |
185 | 157 public void StartCancelTest() { |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
158 var comp = new MockRunnableComponent(true) { |
185 | 159 MockStart = () => PromiseHelper.Sleep(100000, 0) |
160 }; | |
161 | |
162 var p = comp.Start(); | |
163 Assert.AreEqual(ExecutionState.Starting, comp.State); | |
164 p.Cancel(); | |
165 ShouldThrow(() => p.Join(1000)); | |
166 Assert.AreEqual(ExecutionState.Failed, comp.State); | |
188 | 167 |
194 | 168 Assert.IsTrue(comp.LastError is OperationCanceledException); |
185 | 169 |
170 comp.Dispose(); | |
171 } | |
172 | |
173 [TestMethod] | |
174 public void StartStopTest() { | |
175 var stop = new Signal(); | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
176 var comp = new MockRunnableComponent(true) { |
185 | 177 MockStart = () => PromiseHelper.Sleep(100000, 0), |
178 MockStop = () => AsyncPool.RunThread(stop.Wait) | |
179 }; | |
180 | |
181 var p1 = comp.Start(); | |
182 var p2 = comp.Stop(); | |
183 // should enter stopping state | |
184 | |
185 ShouldThrow(p1.Join); | |
186 Assert.IsTrue(p1.IsCancelled); | |
187 Assert.AreEqual(ExecutionState.Stopping, comp.State); | |
188 | |
189 stop.Set(); | |
190 p2.Join(1000); | |
191 Assert.AreEqual(ExecutionState.Disposed, comp.State); | |
192 } | |
193 | |
194 [TestMethod] | |
195 public void StartStopFailTest() { | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
196 var comp = new MockRunnableComponent(true) { |
185 | 197 MockStart = () => PromiseHelper.Sleep(100000, 0).Then(null,null,x => { throw new Exception("I'm dead"); }) |
198 }; | |
199 | |
200 comp.Start(); | |
201 var p = comp.Stop(); | |
202 // if Start fails to cancel, should fail to stop | |
203 ShouldThrow(() => p.Join(1000)); | |
204 Assert.AreEqual(ExecutionState.Failed, comp.State); | |
205 Assert.IsNotNull(comp.LastError); | |
206 Assert.AreEqual("I'm dead", comp.LastError.Message); | |
207 } | |
208 | |
209 [TestMethod] | |
210 public void StopCancelTest() { | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
211 var comp = new MockRunnableComponent(true) { |
185 | 212 MockStop = () => PromiseHelper.Sleep(100000, 0) |
213 }; | |
214 | |
215 comp.Start(); | |
216 var p = comp.Stop(); | |
217 Assert.AreEqual(ExecutionState.Stopping, comp.State); | |
218 p.Cancel(); | |
219 ShouldThrow(() => p.Join(1000)); | |
188 | 220 Assert.AreEqual(ExecutionState.Failed, comp.State); |
194 | 221 Assert.IsTrue(comp.LastError is OperationCanceledException); |
185 | 222 |
223 comp.Dispose(); | |
224 } | |
225 | |
226 } | |
227 } | |
228 |