Skip to content

Commit

Permalink
test: Fix blocking test methods
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Nov 6, 2023
1 parent 8b93f21 commit f2dad98
Showing 1 changed file with 82 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<HealthCheckService>();

healthCheckServiceMock.CheckHealthAsync(Arg.Any<Func<HealthCheckRegistration, bool>?>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(
new HealthReport(
new Dictionary<string, HealthReportEntry>
{
.Returns(Task.FromResult(
new HealthReport(
new Dictionary<string, HealthReportEntry>
{
"dummy", new HealthReportEntry(healthStatus,
null, TimeSpan.Zero, null, null)
}
}, TimeSpan.Zero)));
{
"dummy", new HealthReportEntry(healthStatus,
null, TimeSpan.Zero, null, null)
}
}, TimeSpan.Zero)));

NullLogger<HealthCheckEndpointServer> logger =
NullLogger<HealthCheckEndpointServer>.Instance;
Expand All @@ -50,19 +50,19 @@ public void GiveStartAsync_WhenHealthyEnough_ThenAClientCanConnect(
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));

Check warning on line 67 in test/Aktabook.Diagnostics.HealthChecks.UnitTest/HealthCheckEndpointServerTest.cs

View workflow job for this annotation

GitHub Actions / Quality gate

Do not use 'Thread.Sleep()' in a test.

Check warning on line 67 in test/Aktabook.Diagnostics.HealthChecks.UnitTest/HealthCheckEndpointServerTest.cs

View workflow job for this annotation

GitHub Actions / Quality gate

Do not use 'Thread.Sleep()' in a test.

Expand All @@ -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();
HealthCheckService healthCheckServiceMock = Substitute.For<HealthCheckService>();

healthCheckServiceMock.CheckHealthAsync(Arg.Any<Func<HealthCheckRegistration, bool>?>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(
new HealthReport(
new Dictionary<string, HealthReportEntry>
{
.Returns(Task.FromResult(
new HealthReport(
new Dictionary<string, HealthReportEntry>
{
"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<HealthCheckEndpointServer> logger =
NullLogger<HealthCheckEndpointServer>.Instance;
Expand All @@ -109,19 +109,19 @@ public void GiveStartAsync_WhenUnhealthy_ThenAClientCannotConnect()
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));

Check warning on line 126 in test/Aktabook.Diagnostics.HealthChecks.UnitTest/HealthCheckEndpointServerTest.cs

View workflow job for this annotation

GitHub Actions / Quality gate

Do not use 'Thread.Sleep()' in a test.

Check warning on line 126 in test/Aktabook.Diagnostics.HealthChecks.UnitTest/HealthCheckEndpointServerTest.cs

View workflow job for this annotation

GitHub Actions / Quality gate

Do not use 'Thread.Sleep()' in a test.

Expand All @@ -133,11 +133,11 @@ public void GiveStartAsync_WhenUnhealthy_ThenAClientCannotConnect()
.Should().Throw<SocketException>();

cancellationTokenSource.Cancel();
t.Join();
await task;
}

[Fact]
public void GivenStop_WhenInvokedAfterStart_ThenClientCannotConnect()
public async Task GivenStop_WhenInvokedAfterStart_ThenClientCannotConnect()
{
CancellationTokenSource cancellationTokenSource = new();
HealthCheckService healthCheckServiceMock = Substitute.For<HealthCheckService>();
Expand Down Expand Up @@ -165,19 +165,19 @@ public void GivenStop_WhenInvokedAfterStart_ThenClientCannotConnect()
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));

Check warning on line 182 in test/Aktabook.Diagnostics.HealthChecks.UnitTest/HealthCheckEndpointServerTest.cs

View workflow job for this annotation

GitHub Actions / Quality gate

Do not use 'Thread.Sleep()' in a test.

Check warning on line 182 in test/Aktabook.Diagnostics.HealthChecks.UnitTest/HealthCheckEndpointServerTest.cs

View workflow job for this annotation

GitHub Actions / Quality gate

Do not use 'Thread.Sleep()' in a test.

Expand All @@ -198,27 +198,27 @@ public void GivenStop_WhenInvokedAfterStart_ThenClientCannotConnect()
.Should().Throw<SocketException>();

cancellationTokenSource.Cancel();
t.Join();
await task;
}

[Fact]
public void
public async Task
GivenSetStoppingToken_WhenIsRunning_ThenInvalidOperationException()
{
CancellationTokenSource cancellationTokenSource = new();
HealthCheckService healthCheckServiceMock = Substitute.For<HealthCheckService>();

healthCheckServiceMock.CheckHealthAsync(Arg.Any<Func<HealthCheckRegistration, bool>?>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(
new HealthReport(
new Dictionary<string, HealthReportEntry>
{
.Returns(Task.FromResult(
new HealthReport(
new Dictionary<string, HealthReportEntry>
{
"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<HealthCheckEndpointServer> logger =
NullLogger<HealthCheckEndpointServer>.Instance;
Expand All @@ -235,27 +235,26 @@ public void
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();
cancellationTokenSource.Cancel();
t.Join();
try
{
await healthCheckEndpointServer.StartServerAsync();
}
catch (OperationCanceledException)
{
healthCheckEndpointServer.StopServer();
}
},
cancellationTokenSource.Token
);

healthCheckEndpointServer
.Invoking(x => x.SetStoppingToken(CancellationToken.None)).Should()
.ThrowExactly<InvalidOperationException>();

cancellationTokenSource.Cancel();
await task;
}

[Fact]
Expand Down

0 comments on commit f2dad98

Please sign in to comment.