|
| 1 | +// ------------------------------------------------------------ |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// Licensed under the MIT License (MIT). See License.txt in the repo root for license information. |
| 4 | +// ------------------------------------------------------------ |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.IO; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 13 | +using Microsoft.Extensions.Logging; |
| 14 | +using Fabrikam.Workflow.Service.Services; |
| 15 | +using Moq; |
| 16 | +using Xunit; |
| 17 | + |
| 18 | +namespace Fabrikam.Workflow.Service.Tests |
| 19 | +{ |
| 20 | + public class ReadinessLivenessPublisherTests |
| 21 | + { |
| 22 | + private const int DelayCompletionMs = 1000; |
| 23 | + |
| 24 | + private readonly ReadinessLivenessPublisher _publisher; |
| 25 | + |
| 26 | + public ReadinessLivenessPublisherTests() |
| 27 | + { |
| 28 | + var servicesBuilder = new ServiceCollection(); |
| 29 | + servicesBuilder.AddLogging(logging => logging.AddDebug()); |
| 30 | + var services = servicesBuilder.BuildServiceProvider(); |
| 31 | + |
| 32 | + _publisher = |
| 33 | + new ReadinessLivenessPublisher( |
| 34 | + services.GetService<ILogger<ReadinessLivenessPublisher>>()); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public async Task WhenPublishingAndReportIsHealthy_FileExists() |
| 39 | + { |
| 40 | + // Arrange |
| 41 | + var healthReportEntries = new Dictionary<string, HealthReportEntry>() |
| 42 | + { |
| 43 | + {"healthy", new HealthReportEntry(HealthStatus.Healthy, null,TimeSpan.MinValue, null, null) } |
| 44 | + }; |
| 45 | + |
| 46 | + // Act |
| 47 | + await _publisher.PublishAsync( |
| 48 | + new HealthReport(healthReportEntries, TimeSpan.MinValue), |
| 49 | + new CancellationTokenSource().Token); |
| 50 | + |
| 51 | + // Arrange |
| 52 | + Assert.True(File.Exists(ReadinessLivenessPublisher.FilePath)); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task WhenPublishingAndReportIsUnhealthy_FileDateTimeIsNotModified() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + var healthReportEntries = new Dictionary<string, HealthReportEntry>() |
| 60 | + { |
| 61 | + {"healthy", new HealthReportEntry(HealthStatus.Healthy, null,TimeSpan.MinValue, null, null) } |
| 62 | + }; |
| 63 | + |
| 64 | + await _publisher.PublishAsync( |
| 65 | + new HealthReport( |
| 66 | + healthReportEntries, |
| 67 | + TimeSpan.MinValue), |
| 68 | + new CancellationTokenSource().Token); |
| 69 | + |
| 70 | + healthReportEntries.Add( |
| 71 | + "unhealthy", |
| 72 | + new HealthReportEntry( |
| 73 | + HealthStatus.Unhealthy, |
| 74 | + null,TimeSpan.MinValue, null, null)); |
| 75 | + |
| 76 | + // Act |
| 77 | + DateTime healthyWriteTime = File.GetLastWriteTime(ReadinessLivenessPublisher.FilePath); |
| 78 | + await _publisher.PublishAsync( |
| 79 | + new HealthReport(healthReportEntries, TimeSpan.MinValue), |
| 80 | + new CancellationTokenSource().Token); |
| 81 | + |
| 82 | + // Arrange |
| 83 | + Assert.True(File.Exists(ReadinessLivenessPublisher.FilePath)); |
| 84 | + Assert.Equal(healthyWriteTime, File.GetLastWriteTime(ReadinessLivenessPublisher.FilePath)); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact(Timeout = DelayCompletionMs * 3)] |
| 88 | + public async Task WhenPublishingAndReportIsHealthyTwice_FileDateTimeIsModified() |
| 89 | + { |
| 90 | + // Arrange |
| 91 | + Func<Task> emulatePeriodicHealthCheckAsync = |
| 92 | + () => Task.Delay(DelayCompletionMs); |
| 93 | + |
| 94 | + var healthReportEntries = new Dictionary<string, HealthReportEntry>() |
| 95 | + { |
| 96 | + {"healthy", new HealthReportEntry(HealthStatus.Healthy, null,TimeSpan.MinValue, null, null) } |
| 97 | + }; |
| 98 | + |
| 99 | + // Act |
| 100 | + await _publisher.PublishAsync( |
| 101 | + new HealthReport( |
| 102 | + healthReportEntries, |
| 103 | + TimeSpan.MinValue), |
| 104 | + new CancellationTokenSource().Token); |
| 105 | + |
| 106 | + DateTime firstTimehealthyWriteTime = File.GetLastWriteTime(ReadinessLivenessPublisher.FilePath); |
| 107 | + |
| 108 | + await emulatePeriodicHealthCheckAsync(); |
| 109 | + |
| 110 | + await _publisher.PublishAsync( |
| 111 | + new HealthReport(healthReportEntries, TimeSpan.MinValue), |
| 112 | + new CancellationTokenSource().Token); |
| 113 | + |
| 114 | + DateTime sencondTimehealthyWriteTime = File.GetLastWriteTime(ReadinessLivenessPublisher.FilePath); |
| 115 | + |
| 116 | + // Arrange |
| 117 | + Assert.True(firstTimehealthyWriteTime < sencondTimehealthyWriteTime); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments