Mercurial > pub > ImplabNet
annotate Implab.Test/RunnableComponentTests.cs @ 203:4d9830a9bbb8 v2
Added 'Fail' method to RunnableComponent which allows component to move from
Running to Failed state.
Added PollingComponent a timer based runnable component
More tests
Added FailPromise a thin class to wrap exceptions
Fixed error handling in SuccessPromise classes.
author | cin |
---|---|
date | Tue, 18 Oct 2016 17:49:54 +0300 |
parents | d45bdf510514 |
children | 8200ab154c8a |
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 | |
42 comp.Init(); | |
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 | |
68 ShouldThrow(comp.Init); | |
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()); | |
88 ShouldThrow(comp.Init); | |
89 | |
90 Assert.AreEqual(ExecutionState.Disposed, comp.State); | |
91 } | |
92 | |
93 [TestMethod] | |
94 public void StartCancelTest() { | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
95 var comp = new MockRunnableComponent(true) { |
185 | 96 MockStart = () => PromiseHelper.Sleep(100000, 0) |
97 }; | |
98 | |
99 var p = comp.Start(); | |
100 Assert.AreEqual(ExecutionState.Starting, comp.State); | |
101 p.Cancel(); | |
102 ShouldThrow(() => p.Join(1000)); | |
103 Assert.AreEqual(ExecutionState.Failed, comp.State); | |
188 | 104 |
194 | 105 Assert.IsTrue(comp.LastError is OperationCanceledException); |
185 | 106 |
107 comp.Dispose(); | |
108 } | |
109 | |
110 [TestMethod] | |
111 public void StartStopTest() { | |
112 var stop = new Signal(); | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
113 var comp = new MockRunnableComponent(true) { |
185 | 114 MockStart = () => PromiseHelper.Sleep(100000, 0), |
115 MockStop = () => AsyncPool.RunThread(stop.Wait) | |
116 }; | |
117 | |
118 var p1 = comp.Start(); | |
119 var p2 = comp.Stop(); | |
120 // should enter stopping state | |
121 | |
122 ShouldThrow(p1.Join); | |
123 Assert.IsTrue(p1.IsCancelled); | |
124 Assert.AreEqual(ExecutionState.Stopping, comp.State); | |
125 | |
126 stop.Set(); | |
127 p2.Join(1000); | |
128 Assert.AreEqual(ExecutionState.Disposed, comp.State); | |
129 } | |
130 | |
131 [TestMethod] | |
132 public void StartStopFailTest() { | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
133 var comp = new MockRunnableComponent(true) { |
185 | 134 MockStart = () => PromiseHelper.Sleep(100000, 0).Then(null,null,x => { throw new Exception("I'm dead"); }) |
135 }; | |
136 | |
137 comp.Start(); | |
138 var p = comp.Stop(); | |
139 // if Start fails to cancel, should fail to stop | |
140 ShouldThrow(() => p.Join(1000)); | |
141 Assert.AreEqual(ExecutionState.Failed, comp.State); | |
142 Assert.IsNotNull(comp.LastError); | |
143 Assert.AreEqual("I'm dead", comp.LastError.Message); | |
144 } | |
145 | |
146 [TestMethod] | |
147 public void StopCancelTest() { | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
194
diff
changeset
|
148 var comp = new MockRunnableComponent(true) { |
185 | 149 MockStop = () => PromiseHelper.Sleep(100000, 0) |
150 }; | |
151 | |
152 comp.Start(); | |
153 var p = comp.Stop(); | |
154 Assert.AreEqual(ExecutionState.Stopping, comp.State); | |
155 p.Cancel(); | |
156 ShouldThrow(() => p.Join(1000)); | |
188 | 157 Assert.AreEqual(ExecutionState.Failed, comp.State); |
194 | 158 Assert.IsTrue(comp.LastError is OperationCanceledException); |
185 | 159 |
160 comp.Dispose(); | |
161 } | |
162 | |
163 } | |
164 } | |
165 |