annotate Implab.Test/RunnableComponentTests.cs @ 276:b4e0f81c7425 v3

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