Skip to content

Fix flaky HealthCheck test #20741

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

Merged
merged 2 commits into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
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
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand All @@ -22,6 +22,7 @@ internal sealed class HealthCheckPublisherHostedService : IHostedService

private CancellationTokenSource _stopping;
private Timer _timer;
private CancellationTokenSource _runTokenSource;

public HealthCheckPublisherHostedService(
HealthCheckService healthCheckService,
Expand Down Expand Up @@ -69,7 +70,7 @@ public Task StartAsync(CancellationToken cancellationToken = default)
}

// IMPORTANT - make sure this is the last thing that happens in this method. The timer can
// fire before other code runs.
// fire before other code runs.
_timer = NonCapturingTimer.Create(Timer_Tick, null, dueTime: _options.Value.Delay, period: _options.Value.Period);

return Task.CompletedTask;
Expand Down Expand Up @@ -104,6 +105,12 @@ private async void Timer_Tick(object state)
await RunAsync();
}

// Internal for testing
internal void CancelToken()
{
_runTokenSource.Cancel();
}

// Internal for testing
internal async Task RunAsync()
{
Expand All @@ -116,6 +123,7 @@ internal async Task RunAsync()
var timeout = _options.Value.Timeout;

cancellation = CancellationTokenSource.CreateLinkedTokenSource(_stopping.Token);
_runTokenSource = cancellation;
cancellation.CancelAfter(timeout);

await RunAsyncCore(cancellation.Token);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -145,7 +145,7 @@ public async Task StopAsync_CancelsExecution()
await service.StopAsync(); // Trigger cancellation

// Assert
await AssertCancelledAsync(publishers[0].Entries[0].cancellationToken);
await AssertCanceledAsync(publishers[0].Entries[0].cancellationToken);
Assert.False(service.IsTimerRunning);
Assert.True(service.IsStopping);

Expand Down Expand Up @@ -286,10 +286,7 @@ public async Task RunAsync_PublishersCanTimeout()
new TestPublisher() { Wait = unblock.Task, },
};

var service = CreateService(publishers, sink: sink, configure: (options) =>
{
options.Timeout = TimeSpan.FromMilliseconds(50);
});
var service = CreateService(publishers, sink: sink);

try
{
Expand All @@ -300,7 +297,9 @@ public async Task RunAsync_PublishersCanTimeout()

await publishers[0].Started.TimeoutAfter(TimeSpan.FromSeconds(10));

await AssertCancelledAsync(publishers[0].Entries[0].cancellationToken);
service.CancelToken();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 - agree this is better than doing a wait.


await AssertCanceledAsync(publishers[0].Entries[0].cancellationToken);

unblock.SetResult(null);

Expand Down Expand Up @@ -483,7 +482,7 @@ private HealthCheckPublisherHostedService CreateService(
return services.GetServices<IHostedService>().OfType< HealthCheckPublisherHostedService>().Single();
}

private static async Task AssertCancelledAsync(CancellationToken cancellationToken)
private static async Task AssertCanceledAsync(CancellationToken cancellationToken)
{
await Assert.ThrowsAsync<TaskCanceledException>(() => Task.Delay(TimeSpan.FromSeconds(10), cancellationToken));
}
Expand Down