Mercurial > pub > ImplabNet
annotate Implab.Test/RunnableComponentTests.cs @ 206:86b61d53b7db v2
Слияние
| author | cin |
|---|---|
| date | Tue, 25 Oct 2016 17:40:45 +0300 |
| parents | 8200ab154c8a |
| children | 7d07503621fe |
| 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; |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
98 comp.MockDispose = (disposing, error) => { |
|
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; |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
118 comp.MockDispose = (disposing, error) => { |
|
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; |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
134 Exception lastError = null; |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
135 comp.MockDispose = (disposing, error) => { |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
136 disposed = true; |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
137 lastError = error; |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
138 }; |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
139 |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
140 comp.Start().Join(1000); |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
141 comp.Stop().Join(1000); |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
142 |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
143 Assert.AreEqual(ExecutionState.Ready, comp.State); |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
144 Assert.IsFalse(disposed); |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
145 |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
146 comp.MockStop = () => { |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
147 comp.Dispose(); |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
148 return Promise.Success; |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
149 }; |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
150 |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
151 comp.Start().Join(1000); |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
152 comp.Stop().Join(1000); |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
153 |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
154 Assert.AreEqual(ExecutionState.Disposed, comp.State); |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
155 Assert.IsTrue(disposed); |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
156 } |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
157 |
|
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
158 [TestMethod] |
| 185 | 159 public void StartCancelTest() { |
|
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
160 var comp = new MockRunnableComponent(true) { |
| 185 | 161 MockStart = () => PromiseHelper.Sleep(100000, 0) |
| 162 }; | |
| 163 | |
| 164 var p = comp.Start(); | |
| 165 Assert.AreEqual(ExecutionState.Starting, comp.State); | |
| 166 p.Cancel(); | |
| 167 ShouldThrow(() => p.Join(1000)); | |
| 168 Assert.AreEqual(ExecutionState.Failed, comp.State); | |
| 188 | 169 |
| 194 | 170 Assert.IsTrue(comp.LastError is OperationCanceledException); |
| 185 | 171 |
| 172 comp.Dispose(); | |
| 173 } | |
| 174 | |
| 175 [TestMethod] | |
| 176 public void StartStopTest() { | |
| 177 var stop = new Signal(); | |
|
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
178 var comp = new MockRunnableComponent(true) { |
| 185 | 179 MockStart = () => PromiseHelper.Sleep(100000, 0), |
| 180 MockStop = () => AsyncPool.RunThread(stop.Wait) | |
| 181 }; | |
| 182 | |
| 183 var p1 = comp.Start(); | |
| 184 var p2 = comp.Stop(); | |
| 185 // should enter stopping state | |
| 186 | |
| 187 ShouldThrow(p1.Join); | |
| 188 Assert.IsTrue(p1.IsCancelled); | |
| 189 Assert.AreEqual(ExecutionState.Stopping, comp.State); | |
| 190 | |
| 191 stop.Set(); | |
| 192 p2.Join(1000); | |
| 193 Assert.AreEqual(ExecutionState.Disposed, comp.State); | |
| 194 } | |
| 195 | |
| 196 [TestMethod] | |
| 197 public void StartStopFailTest() { | |
|
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
198 var comp = new MockRunnableComponent(true) { |
| 185 | 199 MockStart = () => PromiseHelper.Sleep(100000, 0).Then(null,null,x => { throw new Exception("I'm dead"); }) |
| 200 }; | |
| 201 | |
| 202 comp.Start(); | |
| 203 var p = comp.Stop(); | |
| 204 // if Start fails to cancel, should fail to stop | |
| 205 ShouldThrow(() => p.Join(1000)); | |
| 206 Assert.AreEqual(ExecutionState.Failed, comp.State); | |
| 207 Assert.IsNotNull(comp.LastError); | |
| 208 Assert.AreEqual("I'm dead", comp.LastError.Message); | |
| 209 } | |
| 210 | |
| 211 [TestMethod] | |
| 212 public void StopCancelTest() { | |
|
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
213 var comp = new MockRunnableComponent(true) { |
| 185 | 214 MockStop = () => PromiseHelper.Sleep(100000, 0) |
| 215 }; | |
| 216 | |
| 217 comp.Start(); | |
| 218 var p = comp.Stop(); | |
| 219 Assert.AreEqual(ExecutionState.Stopping, comp.State); | |
| 220 p.Cancel(); | |
| 221 ShouldThrow(() => p.Join(1000)); | |
| 188 | 222 Assert.AreEqual(ExecutionState.Failed, comp.State); |
| 194 | 223 Assert.IsTrue(comp.LastError is OperationCanceledException); |
| 185 | 224 |
| 225 comp.Dispose(); | |
| 226 } | |
| 227 | |
| 228 } | |
| 229 } | |
| 230 |
