fix(overview): P3 review fixes 003-005 - no redirects on probes, per-target publish so a hung instance cannot stale the rest, probe docs (scadaproj#48)
Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -401,6 +401,55 @@ public class OverviewPollerServiceTests
|
||||
Assert.All(snapshot.AllInstances, i => Assert.Equal(InstanceStatus.Up, i.Status));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Sweep_HungInstanceDoesNotHoldUpResultsOrLaterSweepsForTheOthers()
|
||||
{
|
||||
// Overview-004: a hung instance (long timeout override) used to gate the whole sweep, so
|
||||
// every other instance went Stale between polls. The fast instance must publish as soon as
|
||||
// it answers, and a later sweep must not wait for the hung one.
|
||||
var handler = new RoutingHandler()
|
||||
.Json("http://fast:8080/health/ready", HttpStatusCode.OK, HealthBody.Ready("Healthy"))
|
||||
.Json("http://slow:8080/health/ready", HttpStatusCode.OK, HealthBody.Ready("Healthy"));
|
||||
var options = Registry(App("App", Node("fast", "http://fast:8080"), Node("slow", "http://slow:8080")));
|
||||
var time = new FakeTimeProvider(Start);
|
||||
var (poller, store) = Build(options, new HangingHandler(handler, "slow"), time);
|
||||
|
||||
var first = poller.SweepAsync();
|
||||
|
||||
// The fast result is published while the slow probe is still hanging.
|
||||
await WaitUntilAsync(() => store.Current.AllInstances.Any(i => i.Instance == "fast" && i.Status == InstanceStatus.Up));
|
||||
Assert.False(first.IsCompleted);
|
||||
|
||||
time.Advance(TimeSpan.FromSeconds(11));
|
||||
var second = await poller.SweepAsync().WaitAsync(TimeSpan.FromSeconds(10));
|
||||
|
||||
Assert.False(first.IsCompleted);
|
||||
Assert.Equal(2, ReadyProbes(handler, "fast"));
|
||||
Assert.Equal(InstanceStatus.Up, second.AllInstances.Single(i => i.Instance == "fast").Status);
|
||||
}
|
||||
|
||||
private static async Task WaitUntilAsync(Func<bool> condition)
|
||||
{
|
||||
var deadline = DateTime.UtcNow.AddSeconds(10);
|
||||
while (!condition())
|
||||
{
|
||||
Assert.True(DateTime.UtcNow < deadline, "condition not met in time");
|
||||
await Task.Delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class HangingHandler(RoutingHandler inner, string hangingHost) : HttpMessageHandler
|
||||
{
|
||||
protected override async Task<HttpResponseMessage> SendAsync(
|
||||
HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.RequestUri!.Host == hangingHost)
|
||||
await Task.Delay(Timeout.Infinite, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return await inner.HandleAsync(request).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Sweep_UnregisteredInstances_RenderAsPendingNotAsHealthy()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
using System.Net;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ZB.MOM.WW.Overview.Polling;
|
||||
|
||||
namespace ZB.MOM.WW.Overview.Tests;
|
||||
|
||||
/// <summary>Overview-003: the pooled probe client must not follow redirects.</summary>
|
||||
public sealed class ProbeRedirectTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task ActiveProbe_AuthRedirectToLoginPage_IsNotReportedAsActive()
|
||||
{
|
||||
var port = FreePort();
|
||||
var prefix = $"http://127.0.0.1:{port}/";
|
||||
using var listener = new HttpListener();
|
||||
listener.Prefixes.Add(prefix);
|
||||
listener.Start();
|
||||
var server = Task.Run(async () =>
|
||||
{
|
||||
while (listener.IsListening)
|
||||
{
|
||||
HttpListenerContext ctx;
|
||||
try { ctx = await listener.GetContextAsync(); } catch { return; }
|
||||
if (ctx.Request.Url!.AbsolutePath == "/login")
|
||||
{
|
||||
ctx.Response.StatusCode = 200;
|
||||
ctx.Response.ContentType = "text/html";
|
||||
await ctx.Response.OutputStream.WriteAsync("<html>login</html>"u8.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
ctx.Response.StatusCode = 302;
|
||||
ctx.Response.RedirectLocation = "/login";
|
||||
}
|
||||
ctx.Response.Close();
|
||||
}
|
||||
});
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
services.AddOverviewPolling();
|
||||
await using var provider = services.BuildServiceProvider();
|
||||
var client = provider.GetRequiredService<IHttpClientFactory>().CreateClient(OverviewPollerService.HttpClientName);
|
||||
|
||||
var probe = await new ZbHealthReportClient(client)
|
||||
.ProbeStatusAsync($"{prefix}health/active", TimeSpan.FromSeconds(5));
|
||||
|
||||
listener.Stop();
|
||||
Assert.Equal(302, probe.StatusCode);
|
||||
Assert.Equal(ActiveState.Unknown, StatusDerivation.ObserveActive(true, probe));
|
||||
}
|
||||
|
||||
private static int FreePort()
|
||||
{
|
||||
var l = new System.Net.Sockets.TcpListener(IPAddress.Loopback, 0);
|
||||
l.Start();
|
||||
var port = ((IPEndPoint)l.LocalEndpoint).Port;
|
||||
l.Stop();
|
||||
return port;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user