Fix flaky HealthCheck test (#20741)

This commit is contained in:
Brennan 2020-04-11 17:22:22 -07:00 committed by GitHub
parent 3c1bd093e9
commit 2b7b79f4db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 10 deletions

View File

@ -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;
@ -22,6 +22,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
private CancellationTokenSource _stopping;
private Timer _timer;
private CancellationTokenSource _runTokenSource;
public HealthCheckPublisherHostedService(
HealthCheckService healthCheckService,
@ -69,7 +70,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
}
// 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;
@ -104,6 +105,12 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
await RunAsync();
}
// Internal for testing
internal void CancelToken()
{
_runTokenSource.Cancel();
}
// Internal for testing
internal async Task RunAsync()
{
@ -116,6 +123,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
var timeout = _options.Value.Timeout;
cancellation = CancellationTokenSource.CreateLinkedTokenSource(_stopping.Token);
_runTokenSource = cancellation;
cancellation.CancelAfter(timeout);
await RunAsyncCore(cancellation.Token);

View File

@ -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;
@ -145,7 +145,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
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);
@ -286,10 +286,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
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
{
@ -300,7 +297,9 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
await publishers[0].Started.TimeoutAfter(TimeSpan.FromSeconds(10));
await AssertCancelledAsync(publishers[0].Entries[0].cancellationToken);
service.CancelToken();
await AssertCanceledAsync(publishers[0].Entries[0].cancellationToken);
unblock.SetResult(null);
@ -483,7 +482,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
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));
}