259
|
1 using System;
|
|
2 using System.Threading;
|
|
3 using System.Threading.Tasks;
|
|
4 using Implab.Components;
|
|
5 using Xunit;
|
|
6
|
260
|
7 namespace Implab.Test {
|
259
|
8
|
260
|
9 public class RunnableComponentTests {
|
259
|
10 [Fact]
|
262
|
11 public async Task SimpleStartStop() {
|
259
|
12
|
260
|
13 using (var m = new MockPollComponent(true)) {
|
|
14 m.StartWorker = async (ct) => await Task.Yield();
|
|
15 m.StopWorker = async (ct) => await Task.Yield();
|
259
|
16
|
260
|
17 Assert.Equal(ExecutionState.Ready, m.State);
|
|
18 Assert.NotNull(m.Completion);
|
262
|
19
|
|
20 m.Start();
|
260
|
21 await m.Completion;
|
|
22 Assert.Equal(ExecutionState.Running, m.State);
|
259
|
23
|
262
|
24 m.Stop();
|
|
25 await m.Completion;
|
|
26 Assert.Equal(ExecutionState.Stopped, m.State);
|
|
27 }
|
|
28 }
|
|
29
|
|
30 [Fact]
|
|
31 public async Task SyncStart() {
|
|
32 using (var m = new MockPollComponent(true)) {
|
|
33 m.Start();
|
|
34 Assert.Equal(ExecutionState.Running, m.State);
|
|
35 await m.Completion;
|
|
36 }
|
|
37 }
|
|
38
|
|
39 [Fact]
|
|
40 public async Task AsyncStarting() {
|
|
41 using (var m = new MockPollComponent(true)) {
|
|
42 var signal = Safe.CreateTask();
|
|
43
|
|
44 m.StartWorker = async (ct) => await signal;
|
|
45 m.Start();
|
|
46
|
|
47 Assert.Equal(ExecutionState.Starting, m.State);
|
|
48 Assert.False(m.Completion.IsCompleted);
|
|
49
|
|
50 signal.Start();
|
|
51
|
|
52 await m.Completion;
|
|
53
|
|
54 Assert.Equal(ExecutionState.Running, m.State);
|
|
55 }
|
|
56 }
|
|
57
|
|
58 [Fact]
|
|
59 public async Task FailWhileStarting() {
|
|
60 using (var m = new MockPollComponent(true)) {
|
|
61 const string failMessage = "Fail me";
|
|
62 var signal = new Task(() => {
|
|
63 throw new Exception(failMessage);
|
|
64 });
|
|
65
|
|
66 m.StartWorker = async (ct) => await signal;
|
|
67 m.Start();
|
|
68
|
|
69 Assert.Equal(ExecutionState.Starting, m.State);
|
|
70 Assert.False(m.Completion.IsCompleted);
|
|
71
|
|
72 signal.Start();
|
|
73 try {
|
|
74 await m.Completion;
|
|
75 Assert.True(false);
|
|
76 } catch (Exception e) {
|
|
77 Assert.Equal(failMessage, e.Message);
|
|
78 }
|
|
79
|
|
80 Assert.Equal(ExecutionState.Failed, m.State);
|
|
81 }
|
|
82 }
|
|
83
|
|
84 [Fact]
|
|
85 public async Task SyncStop() {
|
|
86 using (var m = new MockPollComponent(true)) {
|
|
87 m.Start();
|
|
88 Assert.Equal(ExecutionState.Running, m.State);
|
|
89 m.Stop();
|
|
90 Assert.Equal(ExecutionState.Stopped, m.State);
|
|
91 await m.Completion;
|
|
92 }
|
|
93 }
|
|
94
|
|
95 [Fact]
|
|
96 public async Task AsyncStopping() {
|
|
97 using (var m = new MockPollComponent(true)) {
|
|
98 var signal = Safe.CreateTask();
|
|
99
|
|
100 m.StopWorker = async (ct) => await signal;
|
|
101
|
|
102 // Start
|
|
103 m.Start();
|
|
104 Assert.Equal(ExecutionState.Running, m.State);
|
|
105
|
|
106 // Stop
|
|
107 m.Stop();
|
|
108 Assert.Equal(ExecutionState.Stopping, m.State);
|
|
109 Assert.False(m.Completion.IsCompleted);
|
|
110 signal.Start();
|
|
111
|
|
112 await m.Completion;
|
|
113
|
|
114 Assert.Equal(ExecutionState.Stopped, m.State);
|
|
115 }
|
|
116 }
|
|
117
|
|
118 [Fact]
|
|
119 public async Task FailWhileStopping() {
|
|
120 using (var m = new MockPollComponent(true)) {
|
|
121 const string failMessage = "Fail me";
|
|
122 var signal = new Task(() => {
|
|
123 throw new Exception(failMessage);
|
|
124 });
|
|
125
|
|
126 m.StopWorker = async (ct) => await signal;
|
|
127
|
|
128 // Start
|
|
129 m.Start();
|
|
130 Assert.Equal(ExecutionState.Running, m.State);
|
|
131
|
|
132 // Stop
|
|
133 m.Stop();
|
|
134 Assert.Equal(ExecutionState.Stopping, m.State);
|
|
135 Assert.False(m.Completion.IsCompleted);
|
|
136
|
|
137 signal.Start();
|
|
138 try {
|
|
139 await m.Completion;
|
|
140 Assert.True(false);
|
|
141 } catch (Exception e) {
|
|
142 Assert.Equal(failMessage, e.Message);
|
|
143 }
|
|
144
|
|
145 Assert.Equal(ExecutionState.Failed, m.State);
|
|
146 }
|
|
147 }
|
|
148
|
|
149 [Fact]
|
|
150 public async Task ThrowOnInvalidTrasition() {
|
|
151 using (var m = new MockPollComponent(false)) {
|
|
152 var started = Safe.CreateTask();
|
|
153 var stopped = Safe.CreateTask();
|
|
154
|
|
155 m.StartWorker = async (ct) => await started;
|
|
156 m.StopWorker = async (ct) => await stopped;
|
|
157
|
|
158 Assert.Throws<InvalidOperationException>(() => m.Start());
|
|
159
|
|
160 // Initialize
|
|
161 m.Initialize();
|
|
162 await m.Completion;
|
|
163
|
|
164 // Start
|
|
165 m.Start();
|
|
166 Assert.Equal(ExecutionState.Starting, m.State);
|
|
167
|
|
168 // Check invalid transitions
|
|
169 Assert.Throws<InvalidOperationException>(() => m.Start());
|
|
170
|
|
171 // Component can be stopped before started
|
|
172 // m.Stop(CancellationToken.None);
|
|
173
|
|
174 // Running
|
|
175 started.Start();
|
|
176 await m.Completion;
|
|
177 Assert.Equal(ExecutionState.Running, m.State);
|
|
178
|
|
179
|
|
180 Assert.Throws<InvalidOperationException>(() => m.Start());
|
|
181
|
|
182 // Stop
|
|
183 m.Stop();
|
|
184
|
|
185 // Check invalid transitions
|
|
186 Assert.Throws<InvalidOperationException>(() => m.Start());
|
|
187 Assert.Throws<InvalidOperationException>(() => m.Stop());
|
|
188
|
|
189 // Stopped
|
|
190 stopped.Start();
|
|
191 await m.Completion;
|
|
192 Assert.Equal(ExecutionState.Stopped, m.State);
|
|
193
|
|
194 // Check invalid transitions
|
|
195 Assert.Throws<InvalidOperationException>(() => m.Start());
|
|
196 Assert.Throws<InvalidOperationException>(() => m.Stop());
|
|
197 }
|
|
198 }
|
|
199
|
|
200 [Fact]
|
|
201 public async Task CancelStart() {
|
|
202 using (var m = new MockPollComponent(true)) {
|
|
203 m.StartWorker = (ct) => Safe.CreateTask(ct);
|
|
204
|
|
205 m.Start();
|
|
206 var start = m.Completion;
|
|
207
|
|
208 Assert.Equal(ExecutionState.Starting, m.State);
|
|
209 m.Stop();
|
|
210 await m.Completion;
|
|
211 Assert.Equal(ExecutionState.Stopped, m.State);
|
|
212 Assert.True(start.IsCompleted);
|
|
213 Assert.True(start.IsCanceled);
|
|
214 }
|
|
215 }
|
|
216
|
|
217 [Fact]
|
|
218 public async Task AwaitWorker() {
|
|
219 using (var m = new MockPollComponent(true)) {
|
|
220 var worker = Safe.CreateTask();
|
|
221
|
|
222 m.PollWorker = (ct) => worker;
|
|
223
|
|
224 m.Start(CancellationToken.None);
|
|
225 await m.Completion;
|
|
226
|
|
227 Assert.Equal(ExecutionState.Running, m.State);
|
|
228
|
260
|
229 m.Stop(CancellationToken.None);
|
262
|
230 Assert.Equal(ExecutionState.Stopping, m.State);
|
|
231 worker.Start();
|
|
232 await m.Completion;
|
|
233 Assert.Equal(ExecutionState.Stopped, m.State);
|
|
234 }
|
|
235 }
|
|
236
|
|
237 [Fact]
|
|
238 public async Task CancelWorker() {
|
|
239 using (var m = new MockPollComponent(true)) {
|
|
240 var worker = Safe.CreateTask();
|
|
241
|
|
242 var started = Safe.CreateTask();
|
|
243
|
|
244 m.PollWorker = async (ct) => {
|
|
245 started.Start();
|
|
246 await worker;
|
|
247 ct.ThrowIfCancellationRequested();
|
|
248 };
|
|
249
|
|
250 m.Start(CancellationToken.None);
|
|
251 await m.Completion;
|
|
252 await started; // await for the poll worker to start
|
|
253
|
|
254 Assert.Equal(ExecutionState.Running, m.State);
|
|
255
|
|
256 m.Stop(CancellationToken.None);
|
|
257 Assert.Equal(ExecutionState.Stopping, m.State);
|
|
258 worker.Start();
|
260
|
259 await m.Completion;
|
|
260 Assert.Equal(ExecutionState.Stopped, m.State);
|
259
|
261 }
|
|
262 }
|
|
263 }
|
|
264 }
|