Skip to content

Commit 2b7b79f

Browse files
Fix flaky HealthCheck test (#20741)
1 parent 3c1bd09 commit 2b7b79f

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/HealthChecks/HealthChecks/src/HealthCheckPublisherHostedService.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

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

2323
private CancellationTokenSource _stopping;
2424
private Timer _timer;
25+
private CancellationTokenSource _runTokenSource;
2526

2627
public HealthCheckPublisherHostedService(
2728
HealthCheckService healthCheckService,
@@ -69,7 +70,7 @@ public Task StartAsync(CancellationToken cancellationToken = default)
6970
}
7071

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

7576
return Task.CompletedTask;
@@ -104,6 +105,12 @@ private async void Timer_Tick(object state)
104105
await RunAsync();
105106
}
106107

108+
// Internal for testing
109+
internal void CancelToken()
110+
{
111+
_runTokenSource.Cancel();
112+
}
113+
107114
// Internal for testing
108115
internal async Task RunAsync()
109116
{
@@ -116,6 +123,7 @@ internal async Task RunAsync()
116123
var timeout = _options.Value.Timeout;
117124

118125
cancellation = CancellationTokenSource.CreateLinkedTokenSource(_stopping.Token);
126+
_runTokenSource = cancellation;
119127
cancellation.CancelAfter(timeout);
120128

121129
await RunAsyncCore(cancellation.Token);

src/HealthChecks/HealthChecks/test/HealthCheckPublisherHostedServiceTest.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -145,7 +145,7 @@ public async Task StopAsync_CancelsExecution()
145145
await service.StopAsync(); // Trigger cancellation
146146

147147
// Assert
148-
await AssertCancelledAsync(publishers[0].Entries[0].cancellationToken);
148+
await AssertCanceledAsync(publishers[0].Entries[0].cancellationToken);
149149
Assert.False(service.IsTimerRunning);
150150
Assert.True(service.IsStopping);
151151

@@ -286,10 +286,7 @@ public async Task RunAsync_PublishersCanTimeout()
286286
new TestPublisher() { Wait = unblock.Task, },
287287
};
288288

289-
var service = CreateService(publishers, sink: sink, configure: (options) =>
290-
{
291-
options.Timeout = TimeSpan.FromMilliseconds(50);
292-
});
289+
var service = CreateService(publishers, sink: sink);
293290

294291
try
295292
{
@@ -300,7 +297,9 @@ public async Task RunAsync_PublishersCanTimeout()
300297

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

303-
await AssertCancelledAsync(publishers[0].Entries[0].cancellationToken);
300+
service.CancelToken();
301+
302+
await AssertCanceledAsync(publishers[0].Entries[0].cancellationToken);
304303

305304
unblock.SetResult(null);
306305

@@ -483,7 +482,7 @@ private HealthCheckPublisherHostedService CreateService(
483482
return services.GetServices<IHostedService>().OfType< HealthCheckPublisherHostedService>().Single();
484483
}
485484

486-
private static async Task AssertCancelledAsync(CancellationToken cancellationToken)
485+
private static async Task AssertCanceledAsync(CancellationToken cancellationToken)
487486
{
488487
await Assert.ThrowsAsync<TaskCanceledException>(() => Task.Delay(TimeSpan.FromSeconds(10), cancellationToken));
489488
}

0 commit comments

Comments
 (0)