diff --git a/src/ServiceControl.AcceptanceTests/Monitoring/CustomChecks/When_a_failing_custom_check_is_dismissed.cs b/src/ServiceControl.AcceptanceTests/Monitoring/CustomChecks/When_a_failing_custom_check_is_dismissed.cs index 29c462b580..2a1967cd6b 100644 --- a/src/ServiceControl.AcceptanceTests/Monitoring/CustomChecks/When_a_failing_custom_check_is_dismissed.cs +++ b/src/ServiceControl.AcceptanceTests/Monitoring/CustomChecks/When_a_failing_custom_check_is_dismissed.cs @@ -7,6 +7,8 @@ namespace ServiceControl.AcceptanceTests.Monitoring.CustomChecks using AcceptanceTesting; using AcceptanceTesting.EndpointTemplates; using Contracts.CustomChecks; + using Infrastructure.DomainEvents; + using Microsoft.Extensions.DependencyInjection; using NServiceBus; using NServiceBus.AcceptanceTesting; using NServiceBus.CustomChecks; @@ -23,6 +25,8 @@ public async Task Should_come_back_while_the_check_is_still_failing() CustomCheckView dismissed = null; CustomCheckView returned = null; + CustomizeHostBuilder = hostBuilder => hostBuilder.Services.AddSingleton, DismissalObserver>(); + await Define() .WithEndpoint() .Do("Wait for the check to report a failure", async ctx => @@ -36,16 +40,13 @@ await Define() }) .Do("Dismiss it from the page", async _ => await this.Delete($"/api/customchecks/{WithoutPrefix(dismissed.Id)}")) - .Do("Wait until it has gone", async _ => - { - var checks = await this.TryGetMany("/api/customchecks"); - - return checks.Items.All(check => check.CustomCheckId != CheckId); - }) - .Do("Wait for the next report from the endpoint", async _ => + .Do("Wait until the dismissal has been processed", ctx => Task.FromResult(ctx.DismissedAt != null)) + // The endpoint reports every second, so the check is only ever absent for an instant and a + // poll cannot be expected to catch it. A report from after the dismissal proves the same thing. + .Do("Wait for a report from after the dismissal", async ctx => { var checks = await this.TryGetMany("/api/customchecks", - check => check.CustomCheckId == CheckId && check.Status == CheckStatus.Fail); + check => check.CustomCheckId == CheckId && check.Status == CheckStatus.Fail && check.ReportedAt > ctx.DismissedAt); returned = checks.HasResult ? checks.Items.Single() : null; @@ -67,6 +68,16 @@ static string WithoutPrefix(string id) => class Context : ScenarioContext, ISequenceContext { public int Step { get; set; } + public DateTime? DismissedAt { get; set; } + } + + class DismissalObserver(Context context) : IDomainHandler + { + public Task Handle(ServiceControl.CustomChecks.CustomCheckDeleted domainEvent, CancellationToken cancellationToken = default) + { + context.DismissedAt = DateTime.UtcNow; + return Task.CompletedTask; + } } public class Checked : EndpointConfigurationBuilder