|
12 | 12 |
|
13 | 13 | using NUnit.Framework; |
14 | 14 |
|
| 15 | +using Smdn.Net.MuninNode.Transport; |
15 | 16 | using Smdn.Net.MuninPlugin; |
16 | 17 |
|
17 | 18 | namespace Smdn.Net.MuninNode; |
@@ -69,6 +70,63 @@ protected override EndPoint GetLocalEndPointToBind() |
69 | 70 | ); |
70 | 71 | } |
71 | 72 |
|
| 73 | + private class TestLifecycleLocalNode : NodeBase { |
| 74 | + public EventHandler? NodeStarting = null; |
| 75 | + public EventHandler? NodeStarted = null; |
| 76 | + public EventHandler? NodeStopping = null; |
| 77 | + public EventHandler? NodeStopped = null; |
| 78 | + |
| 79 | + private class NullPluginProvider : IPluginProvider { |
| 80 | + public IReadOnlyCollection<IPlugin> Plugins { get; } = []; |
| 81 | + public INodeSessionCallback? SessionCallback => null; |
| 82 | + } |
| 83 | + |
| 84 | + public override IPluginProvider PluginProvider { get; } = new NullPluginProvider(); |
| 85 | + public override string HostName => "test.munin-node.localhost"; |
| 86 | + |
| 87 | + public TestLifecycleLocalNode() |
| 88 | +#pragma warning disable CS0618 |
| 89 | + : base( |
| 90 | + listenerFactory: new PseudoMuninNodeListenerFactory(), |
| 91 | + accessRule: null, |
| 92 | + logger: null |
| 93 | + ) |
| 94 | +#pragma warning restore CS0618 |
| 95 | + { |
| 96 | + } |
| 97 | + |
| 98 | + protected override EndPoint GetLocalEndPointToBind() |
| 99 | + => new IPEndPoint(IPAddress.Any, port: 0); |
| 100 | + |
| 101 | + protected override ValueTask StartingAsync(CancellationToken cancellationToken) |
| 102 | + { |
| 103 | + NodeStarting?.Invoke(this, EventArgs.Empty); |
| 104 | + |
| 105 | + return default; |
| 106 | + } |
| 107 | + |
| 108 | + protected override ValueTask StartedAsync(CancellationToken cancellationToken) |
| 109 | + { |
| 110 | + NodeStarted?.Invoke(this, EventArgs.Empty); |
| 111 | + |
| 112 | + return default; |
| 113 | + } |
| 114 | + |
| 115 | + protected override ValueTask StoppingAsync(CancellationToken cancellationToken) |
| 116 | + { |
| 117 | + NodeStopping?.Invoke(this, EventArgs.Empty); |
| 118 | + |
| 119 | + return default; |
| 120 | + } |
| 121 | + |
| 122 | + protected override ValueTask StoppedAsync(CancellationToken cancellationToken) |
| 123 | + { |
| 124 | + NodeStopped?.Invoke(this, EventArgs.Empty); |
| 125 | + |
| 126 | + return default; |
| 127 | + } |
| 128 | + } |
| 129 | + |
72 | 130 | private static NodeBase CreateNode() |
73 | 131 | => CreateNode(accessRule: null, plugins: Array.Empty<IPlugin>()); |
74 | 132 |
|
@@ -217,6 +275,72 @@ public async Task StartAsync_Restart() |
217 | 275 | #pragma warning restore CS0618 |
218 | 276 | } |
219 | 277 |
|
| 278 | + [Test] |
| 279 | + public async Task StartAsync_CancellationRequestedBeforeStarting() |
| 280 | + { |
| 281 | + await using var node = new TestLifecycleLocalNode(); |
| 282 | + var cancellationToken = new CancellationToken(canceled: true); |
| 283 | + |
| 284 | + var numberOfInvocationOfStartingAsync = 0; |
| 285 | + var numberOfInvocationOfStartedAsync = 0; |
| 286 | + |
| 287 | + node.NodeStarting += (_, _) => numberOfInvocationOfStartingAsync++; |
| 288 | + node.NodeStarted += (_, _) => numberOfInvocationOfStartedAsync++; |
| 289 | + |
| 290 | + Assert.That(async () => await node.StartAsync(cancellationToken), Throws.InstanceOf<OperationCanceledException>()); |
| 291 | + |
| 292 | + Assert.That(numberOfInvocationOfStartingAsync, Is.Zero); |
| 293 | + Assert.That(numberOfInvocationOfStartedAsync, Is.Zero); |
| 294 | + |
| 295 | + Assert.That(() => _ = node.EndPoint, Throws.InvalidOperationException, "must not be started"); |
| 296 | + } |
| 297 | + |
| 298 | + [Test] |
| 299 | + public async Task StartAsync_CancellationRequestedWhileStarting() |
| 300 | + { |
| 301 | + await using var node = new TestLifecycleLocalNode(); |
| 302 | + using var cts = new CancellationTokenSource(); |
| 303 | + |
| 304 | + var numberOfInvocationOfStartingAsync = 0; |
| 305 | + var numberOfInvocationOfStartedAsync = 0; |
| 306 | + |
| 307 | + node.NodeStarting += (_, _) => { |
| 308 | + numberOfInvocationOfStartingAsync++; |
| 309 | + cts.Cancel(); |
| 310 | + }; |
| 311 | + node.NodeStarted += (_, _) => numberOfInvocationOfStartedAsync++; |
| 312 | + |
| 313 | + Assert.That(async () => await node.StartAsync(cts.Token), Throws.InstanceOf<OperationCanceledException>()); |
| 314 | + |
| 315 | + Assert.That(numberOfInvocationOfStartingAsync, Is.EqualTo(1)); |
| 316 | + Assert.That(numberOfInvocationOfStartedAsync, Is.Zero); |
| 317 | + |
| 318 | + Assert.That(() => _ = node.EndPoint, Throws.InvalidOperationException, "must not be started"); |
| 319 | + } |
| 320 | + |
| 321 | + [Test] |
| 322 | + public async Task StartAsync_CancellationRequestedAfterStarted() |
| 323 | + { |
| 324 | + await using var node = new TestLifecycleLocalNode(); |
| 325 | + using var cts = new CancellationTokenSource(); |
| 326 | + |
| 327 | + var numberOfInvocationOfStartingAsync = 0; |
| 328 | + var numberOfInvocationOfStartedAsync = 0; |
| 329 | + |
| 330 | + node.NodeStarting += (_, _) => numberOfInvocationOfStartingAsync++; |
| 331 | + node.NodeStarted += (_, _) => { |
| 332 | + numberOfInvocationOfStartedAsync++; |
| 333 | + cts.Cancel(); |
| 334 | + }; |
| 335 | + |
| 336 | + Assert.That(async () => await node.StartAsync(cts.Token), Throws.Nothing, "requested cancellation must be ignored"); |
| 337 | + |
| 338 | + Assert.That(numberOfInvocationOfStartingAsync, Is.EqualTo(1)); |
| 339 | + Assert.That(numberOfInvocationOfStartedAsync, Is.EqualTo(1)); |
| 340 | + |
| 341 | + Assert.That(() => _ = node.EndPoint, Throws.Nothing, "must be started"); |
| 342 | + } |
| 343 | + |
220 | 344 | [Test] |
221 | 345 | public async Task StopAsync_StartedByStartAsync() |
222 | 346 | { |
@@ -298,4 +422,79 @@ public async Task StopAsync_WhileProcessingClient() |
298 | 422 | Throws.Nothing |
299 | 423 | ); |
300 | 424 | } |
| 425 | + |
| 426 | + [Test] |
| 427 | + public async Task StopAsync_CancellationRequestedBeforeStopping() |
| 428 | + { |
| 429 | + await using var node = new TestLifecycleLocalNode(); |
| 430 | + |
| 431 | + await node.StartAsync(); |
| 432 | + |
| 433 | + var cancellationToken = new CancellationToken(canceled: true); |
| 434 | + |
| 435 | + var numberOfInvocationOfStoppingAsync = 0; |
| 436 | + var numberOfInvocationOfStoppedAsync = 0; |
| 437 | + |
| 438 | + node.NodeStopping += (_, _) => numberOfInvocationOfStoppingAsync++; |
| 439 | + node.NodeStopped += (_, _) => numberOfInvocationOfStoppedAsync++; |
| 440 | + |
| 441 | + Assert.That(async () => await node.StopAsync(cancellationToken), Throws.InstanceOf<OperationCanceledException>()); |
| 442 | + |
| 443 | + Assert.That(numberOfInvocationOfStoppingAsync, Is.Zero); |
| 444 | + Assert.That(numberOfInvocationOfStoppedAsync, Is.Zero); |
| 445 | + |
| 446 | + Assert.That(() => _ = node.EndPoint, Throws.Nothing, "must not be stopped"); |
| 447 | + } |
| 448 | + |
| 449 | + [Test] |
| 450 | + public async Task StopAsync_CancellationRequestedWhileStopping() |
| 451 | + { |
| 452 | + await using var node = new TestLifecycleLocalNode(); |
| 453 | + |
| 454 | + await node.StartAsync(); |
| 455 | + |
| 456 | + using var cts = new CancellationTokenSource(); |
| 457 | + |
| 458 | + var numberOfInvocationOfStoppingAsync = 0; |
| 459 | + var numberOfInvocationOfStoppedAsync = 0; |
| 460 | + |
| 461 | + node.NodeStopping += (_, _) => { |
| 462 | + numberOfInvocationOfStoppingAsync++; |
| 463 | + cts.Cancel(); |
| 464 | + }; |
| 465 | + node.NodeStopped += (_, _) => numberOfInvocationOfStoppedAsync++; |
| 466 | + |
| 467 | + Assert.That(async () => await node.StopAsync(cts.Token), Throws.InstanceOf<OperationCanceledException>()); |
| 468 | + |
| 469 | + Assert.That(numberOfInvocationOfStoppingAsync, Is.EqualTo(1)); |
| 470 | + Assert.That(numberOfInvocationOfStoppedAsync, Is.Zero); |
| 471 | + |
| 472 | + Assert.That(() => _ = node.EndPoint, Throws.Nothing, "must not be stopped"); |
| 473 | + } |
| 474 | + |
| 475 | + [Test] |
| 476 | + public async Task StopAsync_CancellationRequestedAfterStopped() |
| 477 | + { |
| 478 | + await using var node = new TestLifecycleLocalNode(); |
| 479 | + |
| 480 | + await node.StartAsync(); |
| 481 | + |
| 482 | + using var cts = new CancellationTokenSource(); |
| 483 | + |
| 484 | + var numberOfInvocationOfStoppingAsync = 0; |
| 485 | + var numberOfInvocationOfStoppedAsync = 0; |
| 486 | + |
| 487 | + node.NodeStopping += (_, _) => numberOfInvocationOfStoppingAsync++; |
| 488 | + node.NodeStopped += (_, _) => { |
| 489 | + numberOfInvocationOfStoppedAsync++; |
| 490 | + cts.Cancel(); |
| 491 | + }; |
| 492 | + |
| 493 | + Assert.That(async () => await node.StopAsync(cts.Token), Throws.Nothing, "requested cancellation must be ignored"); |
| 494 | + |
| 495 | + Assert.That(numberOfInvocationOfStoppingAsync, Is.EqualTo(1)); |
| 496 | + Assert.That(numberOfInvocationOfStoppedAsync, Is.EqualTo(1)); |
| 497 | + |
| 498 | + Assert.That(() => _ = node.EndPoint, Throws.InvalidOperationException, "must be stopped"); |
| 499 | + } |
301 | 500 | } |
0 commit comments