Take in account code review for test

\n\nCommit migrated from 49bf9920e4
This commit is contained in:
Nathanael Marchand 2019-02-21 11:12:32 +01:00 committed by Ryan Nowak
parent 30c6051d3c
commit 1f05a66661
1 changed files with 38 additions and 27 deletions

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.Logging.Testing;
@ -379,46 +380,56 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
public async Task CheckHealthAsync_ChecksAreRunInParallel() public async Task CheckHealthAsync_ChecksAreRunInParallel()
{ {
// Arrange // Arrange
var sink = new TestSink(); var input1 = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
async Task<HealthCheckResult> CheckMethod() var input2 = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
{ var output1 = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
await Task.Delay(100); var output2 = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
return HealthCheckResult.Healthy();
}
var service = CreateHealthChecksService(b => var service = CreateHealthChecksService(b =>
{ {
b.AddAsyncCheck("test1", CheckMethod); b.AddAsyncCheck("test1",
b.AddAsyncCheck("test2", CheckMethod); async () =>
b.AddAsyncCheck("test3", CheckMethod); {
}, sink); output1.SetResult(null);
await input1.Task;
return HealthCheckResult.Healthy();
});
b.AddAsyncCheck("test2",
async () =>
{
output2.SetResult(null);
await input2.Task;
return HealthCheckResult.Healthy();
});
});
// Act // Act
_ = await service.CheckHealthAsync(); var checkHealthTask = service.CheckHealthAsync();
await Task.WhenAll(output1.Task, output2.Task).TimeoutAfter(TimeSpan.FromSeconds(10));
input1.SetResult(null);
input2.SetResult(null);
await checkHealthTask;
// Assert // Assert
Assert.Collection( Assert.Collection(checkHealthTask.Result.Entries,
sink.Writes, entry =>
entry => { Assert.Equal(DefaultHealthCheckService.EventIds.HealthCheckProcessingBegin, entry.EventId); }, {
entry => { Assert.Equal(DefaultHealthCheckService.EventIds.HealthCheckBegin, entry.EventId); }, Assert.Equal("test1", entry.Key);
entry => { Assert.Equal(DefaultHealthCheckService.EventIds.HealthCheckBegin, entry.EventId); }, Assert.Equal(HealthStatus.Healthy, entry.Value.Status);
entry => { Assert.Equal(DefaultHealthCheckService.EventIds.HealthCheckBegin, entry.EventId); }, },
entry => { Assert.Equal(DefaultHealthCheckService.EventIds.HealthCheckEnd, entry.EventId); }, entry =>
entry => { Assert.Equal(DefaultHealthCheckService.EventIds.HealthCheckEnd, entry.EventId); }, {
entry => { Assert.Equal(DefaultHealthCheckService.EventIds.HealthCheckEnd, entry.EventId); }, Assert.Equal("test2", entry.Key);
entry => { Assert.Equal(DefaultHealthCheckService.EventIds.HealthCheckProcessingEnd, entry.EventId); }); Assert.Equal(HealthStatus.Healthy, entry.Value.Status);
});
} }
private static DefaultHealthCheckService CreateHealthChecksService(Action<IHealthChecksBuilder> configure, ITestSink sink = null) private static DefaultHealthCheckService CreateHealthChecksService(Action<IHealthChecksBuilder> configure)
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddLogging(); services.AddLogging();
services.AddOptions(); services.AddOptions();
if (sink != null)
{
services.AddSingleton<ILoggerFactory>(new TestLoggerFactory(sink, enabled: true));
}
var builder = services.AddHealthChecks(); var builder = services.AddHealthChecks();
if (configure != null) if (configure != null)
{ {