Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Fix blocking test methods #158

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
[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,21 +50,21 @@
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 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.

TcpClient tcpClient = new();

Expand All @@ -74,25 +74,25 @@
.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<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,21 +109,21 @@
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 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.

TcpClient tcpClient = new();

Expand All @@ -133,13 +133,13 @@
.Should().Throw<SocketException>();

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<HealthCheckService>();
healthCheckServiceMock.CheckHealthAsync(Arg.Any<Func<HealthCheckRegistration, bool>?>(), Arg.Any<CancellationToken>()).Returns(Task.FromResult(
new HealthReport(
Expand All @@ -165,21 +165,21 @@
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 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.

TcpClient tcpClient = new();

Expand All @@ -188,7 +188,7 @@
x.Connect(options.Value.IpAddress, options.Value.Port))
.Should().NotThrow();

Thread.Sleep(TimeSpan.FromMilliseconds(75.0));

Check warning on line 191 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 191 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.

healthCheckEndpointServer.StopServer();

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

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<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 @@
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<InvalidOperationException>();

cancellationTokenSource.Cancel();
await task;
}

[Fact]
Expand Down
Loading