@@ -132,6 +132,99 @@ await reader.ReadLineAsync(cancellationToken),
132
132
}
133
133
}
134
134
135
+ private class EmptyPluginProvider : IPluginProvider {
136
+ public IReadOnlyCollection < IPlugin > Plugins { get ; } = [ ] ;
137
+ public INodeSessionCallback ? SessionCallback => null ;
138
+ }
139
+
140
+ private class EmptyNode : LocalNode {
141
+ public override IPluginProvider PluginProvider { get ; } = new EmptyPluginProvider ( ) ;
142
+ public override string HostName => "munin-node.localhost" ;
143
+
144
+ public EmptyNode ( )
145
+ : base (
146
+ listenerFactory : null ,
147
+ accessRule : null ,
148
+ logger : null
149
+ )
150
+ {
151
+ }
152
+ }
153
+
154
+ private class HookStartNode : EmptyNode {
155
+ public EventHandler ? Starting = null ;
156
+ public EventHandler ? Started = null ;
157
+
158
+ public HookStartNode ( )
159
+ : base ( )
160
+ {
161
+ }
162
+
163
+ protected override ValueTask StartingAsync ( CancellationToken cancellationToken )
164
+ {
165
+ Starting ? . Invoke ( this , EventArgs . Empty ) ;
166
+
167
+ return default ;
168
+ }
169
+
170
+ protected override ValueTask StartedAsync ( CancellationToken cancellationToken )
171
+ {
172
+ Started ? . Invoke ( this , EventArgs . Empty ) ;
173
+
174
+ return default ;
175
+ }
176
+ }
177
+
178
+ [ Test ]
179
+ public void StartAsync_CancellationRequestedBeforeStarting ( )
180
+ {
181
+ var numberOfTimesStartingInvoked = 0 ;
182
+ var numberOfTimesStartedInvoked = 0 ;
183
+
184
+ using var hookStartNode = new HookStartNode ( ) ;
185
+
186
+ hookStartNode . Starting += ( _ , _ ) => numberOfTimesStartingInvoked ++ ;
187
+ hookStartNode . Started += ( _ , _ ) => numberOfTimesStartedInvoked ++ ;
188
+
189
+ using var muninNodeService = new MuninNodeBackgroundService ( hookStartNode ) ;
190
+
191
+ // start service
192
+ Assert . That (
193
+ async ( ) => await muninNodeService . StartAsync ( cancellationToken : new ( canceled : true ) ) ,
194
+ Throws . InstanceOf < OperationCanceledException > ( )
195
+ ) ;
196
+
197
+ Assert . That ( numberOfTimesStartingInvoked , Is . Zero ) ;
198
+ Assert . That ( numberOfTimesStartedInvoked , Is . Zero ) ;
199
+ }
200
+
201
+ [ Test ]
202
+ public void StartAsync_CancellationRequestedWhileStarting ( )
203
+ {
204
+ var numberOfTimesStartingInvoked = 0 ;
205
+ var numberOfTimesStartedInvoked = 0 ;
206
+
207
+ using var hookStartNode = new HookStartNode ( ) ;
208
+ using var ctsStarting = new CancellationTokenSource ( ) ;
209
+
210
+ hookStartNode . Starting += ( _ , _ ) => {
211
+ numberOfTimesStartingInvoked ++ ;
212
+ ctsStarting . Cancel ( ) ;
213
+ } ;
214
+ hookStartNode . Started += ( _ , _ ) => numberOfTimesStartedInvoked ++ ;
215
+
216
+ using var muninNodeService = new MuninNodeBackgroundService ( hookStartNode ) ;
217
+
218
+ // start service
219
+ Assert . That (
220
+ async ( ) => await muninNodeService . StartAsync ( cancellationToken : ctsStarting . Token ) ,
221
+ Throws . InstanceOf < OperationCanceledException > ( )
222
+ ) ;
223
+
224
+ Assert . That ( numberOfTimesStartingInvoked , Is . EqualTo ( 1 ) ) ;
225
+ Assert . That ( numberOfTimesStartedInvoked , Is . Zero ) ;
226
+ }
227
+
135
228
private class NullMuninNode : IMuninNode {
136
229
public string HostName => "munin-node.localhost" ;
137
230
public EndPoint EndPoint { get ; } = new IPEndPoint ( IPAddress . Any , 0 ) ;
@@ -160,24 +253,12 @@ public async Task StopAsync_NodeDoesNotSupportGracefulShutdown(CancellationToken
160
253
) ;
161
254
}
162
255
163
- private class HookStopNode : LocalNode {
164
- private sealed class NullPluginProvider : IPluginProvider {
165
- public IReadOnlyCollection < IPlugin > Plugins { get ; } = [ ] ;
166
- public INodeSessionCallback ? SessionCallback => null ;
167
- }
168
-
169
- public override IPluginProvider PluginProvider { get ; } = new NullPluginProvider ( ) ;
170
- public override string HostName => "munin-node.localhost" ;
171
-
256
+ private class HookStopNode : EmptyNode {
172
257
public EventHandler ? Stopping = null ;
173
258
public EventHandler ? Stopped = null ;
174
259
175
260
public HookStopNode ( )
176
- : base (
177
- listenerFactory : null ,
178
- accessRule : null ,
179
- logger : null
180
- )
261
+ : base ( )
181
262
{
182
263
}
183
264
0 commit comments