From 76f9eb6fcffca53d3a9c39f8f3c4efaa6f2c2ffa Mon Sep 17 00:00:00 2001 From: Omar Boukli-Hacene Date: Mon, 6 Nov 2023 22:10:17 +0200 Subject: [PATCH] test: Fix blocking test methods --- .../HealthCheckEndpointServerTest.cs | 171 +++++++++--------- 1 file changed, 85 insertions(+), 86 deletions(-) diff --git a/test/Aktabook.Diagnostics.HealthChecks.UnitTest/HealthCheckEndpointServerTest.cs b/test/Aktabook.Diagnostics.HealthChecks.UnitTest/HealthCheckEndpointServerTest.cs index 2e2f520..7d50d6c 100644 --- a/test/Aktabook.Diagnostics.HealthChecks.UnitTest/HealthCheckEndpointServerTest.cs +++ b/test/Aktabook.Diagnostics.HealthChecks.UnitTest/HealthCheckEndpointServerTest.cs @@ -18,22 +18,22 @@ public class HealthCheckEndpointServerTest [Theory] [InlineData(HealthStatus.Healthy)] [InlineData(HealthStatus.Degraded)] - public void GiveStartAsync_WhenHealthyEnough_ThenAClientCanConnect( + public async Task GiveStartAsync_WhenHealthyEnough_ThenAClientCanConnect( HealthStatus healthStatus) { - CancellationTokenSource cancellationTokenSource = new(); + using CancellationTokenSource cancellationTokenSource = new(); HealthCheckService healthCheckServiceMock = Substitute.For(); healthCheckServiceMock.CheckHealthAsync(Arg.Any?>(), Arg.Any()) - .Returns(Task.FromResult( - new HealthReport( - new Dictionary - { + .Returns(Task.FromResult( + new HealthReport( + new Dictionary { - "dummy", new HealthReportEntry(healthStatus, - null, TimeSpan.Zero, null, null) - } - }, TimeSpan.Zero))); + { + "dummy", new HealthReportEntry(healthStatus, + null, TimeSpan.Zero, null, null) + } + }, TimeSpan.Zero))); NullLogger logger = NullLogger.Instance; @@ -50,19 +50,19 @@ public void GiveStartAsync_WhenHealthyEnough_ThenAClientCanConnect( healthCheckEndpointServer.SetStoppingToken( cancellationTokenSource.Token); - Thread t = new(() => - { - try + var task = Task.Run(async () => { - healthCheckEndpointServer.StartServerAsync().GetAwaiter().GetResult(); - } - catch (OperationCanceledException) - { - healthCheckEndpointServer.StopServer(); - } - }) - { IsBackground = true }; - t.Start(); + try + { + await healthCheckEndpointServer.StartServerAsync(); + } + catch (OperationCanceledException) + { + healthCheckEndpointServer.StopServer(); + } + }, + cancellationTokenSource.Token + ); Thread.Sleep(TimeSpan.FromMilliseconds(75.0)); @@ -74,25 +74,25 @@ public void GiveStartAsync_WhenHealthyEnough_ThenAClientCanConnect( .Should().NotThrow(); cancellationTokenSource.Cancel(); - t.Join(); + await task; } [Fact] - public void GiveStartAsync_WhenUnhealthy_ThenAClientCannotConnect() + public async Task GiveStartAsync_WhenUnhealthy_ThenAClientCannotConnect() { - CancellationTokenSource cancellationTokenSource = new(); + using CancellationTokenSource cancellationTokenSource = new(); HealthCheckService healthCheckServiceMock = Substitute.For(); healthCheckServiceMock.CheckHealthAsync(Arg.Any?>(), Arg.Any()) - .Returns(Task.FromResult( - new HealthReport( - new Dictionary - { + .Returns(Task.FromResult( + new HealthReport( + new Dictionary { - "dummy", new HealthReportEntry(HealthStatus.Unhealthy, - null, TimeSpan.Zero, null, null) - } - }, TimeSpan.Zero))); + { + "dummy", new HealthReportEntry(HealthStatus.Unhealthy, + null, TimeSpan.Zero, null, null) + } + }, TimeSpan.Zero))); NullLogger logger = NullLogger.Instance; @@ -109,19 +109,19 @@ public void GiveStartAsync_WhenUnhealthy_ThenAClientCannotConnect() healthCheckEndpointServer.SetStoppingToken( cancellationTokenSource.Token); - Thread t = new(() => - { - try - { - healthCheckEndpointServer.StartServerAsync().GetAwaiter().GetResult(); - } - catch (OperationCanceledException) + var task = Task.Run(async () => { - healthCheckEndpointServer.StopServer(); - } - }) - { IsBackground = true }; - t.Start(); + try + { + await healthCheckEndpointServer.StartServerAsync(); + } + catch (OperationCanceledException) + { + healthCheckEndpointServer.StopServer(); + } + }, + cancellationTokenSource.Token + ); Thread.Sleep(TimeSpan.FromMilliseconds(75.0)); @@ -133,13 +133,13 @@ public void GiveStartAsync_WhenUnhealthy_ThenAClientCannotConnect() .Should().Throw(); cancellationTokenSource.Cancel(); - t.Join(); + await task; } [Fact] - public void GivenStop_WhenInvokedAfterStart_ThenClientCannotConnect() + public async Task GivenStop_WhenInvokedAfterStart_ThenClientCannotConnect() { - CancellationTokenSource cancellationTokenSource = new(); + using CancellationTokenSource cancellationTokenSource = new(); HealthCheckService healthCheckServiceMock = Substitute.For(); healthCheckServiceMock.CheckHealthAsync(Arg.Any?>(), Arg.Any()).Returns(Task.FromResult( new HealthReport( @@ -165,19 +165,19 @@ public void GivenStop_WhenInvokedAfterStart_ThenClientCannotConnect() healthCheckEndpointServer.SetStoppingToken( cancellationTokenSource.Token); - Thread t = new(() => - { - try + var task = Task.Run(async () => { - healthCheckEndpointServer.StartServerAsync().GetAwaiter().GetResult(); - } - catch (OperationCanceledException) - { - healthCheckEndpointServer.StopServer(); - } - }) - { IsBackground = true }; - t.Start(); + try + { + await healthCheckEndpointServer.StartServerAsync(); + } + catch (OperationCanceledException) + { + healthCheckEndpointServer.StopServer(); + } + }, + cancellationTokenSource.Token + ); Thread.Sleep(TimeSpan.FromMilliseconds(75.0)); @@ -198,27 +198,27 @@ public void GivenStop_WhenInvokedAfterStart_ThenClientCannotConnect() .Should().Throw(); cancellationTokenSource.Cancel(); - t.Join(); + await task; } [Fact] - public void + public async Task GivenSetStoppingToken_WhenIsRunning_ThenInvalidOperationException() { - CancellationTokenSource cancellationTokenSource = new(); + using CancellationTokenSource cancellationTokenSource = new(); HealthCheckService healthCheckServiceMock = Substitute.For(); healthCheckServiceMock.CheckHealthAsync(Arg.Any?>(), Arg.Any()) - .Returns(Task.FromResult( - new HealthReport( - new Dictionary - { + .Returns(Task.FromResult( + new HealthReport( + new Dictionary { - "dummy", new HealthReportEntry(HealthStatus.Healthy, - null, - TimeSpan.Zero, null, null) - } - }, TimeSpan.Zero))); + { + "dummy", new HealthReportEntry(HealthStatus.Healthy, + null, + TimeSpan.Zero, null, null) + } + }, TimeSpan.Zero))); NullLogger logger = NullLogger.Instance; @@ -235,27 +235,26 @@ public void healthCheckEndpointServer.SetStoppingToken( cancellationTokenSource.Token); - Thread t = new(() => - { - try - { - healthCheckEndpointServer.StartServerAsync().GetAwaiter().GetResult(); - } - catch (OperationCanceledException) + var task = Task.Run(async () => { - healthCheckEndpointServer.StopServer(); - } - }) - { IsBackground = true }; - t.Start(); - cancellationTokenSource.Cancel(); - t.Join(); + try + { + await healthCheckEndpointServer.StartServerAsync(); + } + catch (OperationCanceledException) + { + healthCheckEndpointServer.StopServer(); + } + }, + cancellationTokenSource.Token + ); healthCheckEndpointServer .Invoking(x => x.SetStoppingToken(CancellationToken.None)).Should() .ThrowExactly(); cancellationTokenSource.Cancel(); + await task; } [Fact]