fix(delmia,inbound): a connect-phase failure (unreachable host, connect timeout) fails over to the next URL/peer instead of OUTCOME_UNKNOWN (DLM-1, WP4 task 4.7)

This commit is contained in:
Joseph Doherty
2026-12-03 10:01:59 -05:00
parent 11992075e8
commit c15bd210f7
11 changed files with 317 additions and 20 deletions
@@ -102,4 +102,86 @@ public class HttpRecipeSenderTests
Assert.Equal(AttemptKind.Connected, outcome.Kind);
Assert.Equal(500, outcome.StatusCode);
}
/// <summary>
/// DLM-1 regression: a powered-off / black-holed node (TEST-NET-1 never answers the SYN) is a
/// connect failure — the next URL is tried — not OUTCOME_UNKNOWN. On a host without a default
/// route the connect fails fast instead; both carry the connect marker.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task DLM1_BlackHoledHost_IsConnectFailed_NotOutcomeUnknown()
{
using var http = new HttpClient(ConnectPhase.CreateHandler(TimeSpan.FromMilliseconds(500)))
{
Timeout = TimeSpan.FromSeconds(10),
};
var sender = new HttpRecipeSender(http, "sbk_x");
var outcome = await sender.SendAsync("http://192.0.2.1:8083", Payload, CancellationToken.None);
Assert.Equal(AttemptKind.ConnectFailed, outcome.Kind);
}
/// <summary>DLM-1: with the real handler a refused connection is still a connect failure.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task DLM1_RefusedWithRealHandler_IsConnectFailed()
{
using var http = new HttpClient(ConnectPhase.CreateHandler(TimeSpan.FromSeconds(5)));
var outcome = await new HttpRecipeSender(http, "sbk_x").SendAsync("http://127.0.0.1:1", Payload, CancellationToken.None);
Assert.Equal(AttemptKind.ConnectFailed, outcome.Kind);
}
/// <summary>
/// DLM-1: the notifier moves past a black-holed first URL to the second one instead of stopping
/// at OUTCOME_UNKNOWN (both are unreachable here, so the run reports a connect failure for all).
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task DLM1_BlackHoledFirstUrl_TriesTheNextUrl()
{
using var http = new HttpClient(ConnectPhase.CreateHandler(TimeSpan.FromMilliseconds(500)))
{
Timeout = TimeSpan.FromSeconds(10),
};
var tried = new List<string>();
var sender = new RecordingSender(new HttpRecipeSender(http, "sbk_x"), tried);
var outcome = await Notifier.RunAsync(["http://192.0.2.1:8083", "http://127.0.0.1:1"], Payload, sender, CancellationToken.None);
Assert.False(outcome.Ok);
Assert.Equal(2, tried.Count);
Assert.DoesNotContain("OUTCOME_UNKNOWN", outcome.Reason);
}
/// <summary>DLM-1: a failure after the connection was up (no marker) stays outcome-unknown.</summary>
[Fact]
public void DLM1_WasNotSent_RequiresTheConnectMarker()
{
Assert.True(ConnectPhase.WasNotSent(new TaskCanceledException("canceled",
new TimeoutException("connect timeout", new ConnectPhaseException("x", new OperationCanceledException())))));
Assert.False(ConnectPhase.WasNotSent(new TaskCanceledException("HttpClient.Timeout",
new TimeoutException("A task was canceled.", new TaskCanceledException()))));
Assert.False(ConnectPhase.WasNotSent(new HttpRequestException(HttpRequestError.ResponseEnded, "ended",
new IOException("reset"))));
}
/// <summary>ConnectTimeoutFor stays strictly inside the overall timeout.</summary>
[Fact]
public void ConnectTimeoutFor_IsCappedAtHalfTheOverallTimeout()
{
Assert.Equal(TimeSpan.FromSeconds(5), ConnectPhase.ConnectTimeoutFor(TimeSpan.FromSeconds(30)));
Assert.Equal(TimeSpan.FromSeconds(1.5), ConnectPhase.ConnectTimeoutFor(TimeSpan.FromSeconds(3)));
}
private sealed class RecordingSender(IRecipeSender inner, List<string> tried) : IRecipeSender
{
/// <inheritdoc />
public Task<AttemptOutcome> SendAsync(string baseUrl, RecipeDownload payload, CancellationToken ct)
{
tried.Add(baseUrl);
return inner.SendAsync(baseUrl, payload, ct);
}
}
}
@@ -297,6 +297,33 @@ public class StandbyRequestForwardingTests
AssertStatus(result, StatusCodes.Status503ServiceUnavailable);
}
/// <summary>
/// DLM-1 regression: a powered-off / black-holed peer (TEST-NET-1 never answers the SYN) is a
/// not-dispatched connect failure with the production handler — the relay moves on and, with
/// every peer unreachable, answers 503 ACTIVE_NODE_UNREACHABLE instead of a 60 s wait ending
/// in 502 OUTCOME_UNKNOWN.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Forwarder_BlackHoledPeer_IsNotDispatched_TriesNextPeer()
{
using var handler = RelayConnectPhase.CreateHandler(TimeSpan.FromMilliseconds(500));
var forwarder = new HttpStandbyRequestForwarder(
new FakeHttpClientFactory(handler),
new StaticOptionsMonitor<InboundApiOptions>(new InboundApiOptions
{
StandbyForwardPeerEndpoints = ["http://192.0.2.1:5000", "http://127.0.0.1:1"],
StandbyForwardTimeout = TimeSpan.FromSeconds(20),
}),
NullLogger<HttpStandbyRequestForwarder>.Instance);
var sw = System.Diagnostics.Stopwatch.StartNew();
var result = await forwarder.ForwardAsync(BuildRequestContext("{}"), CancellationToken.None);
AssertStatus(result, StatusCodes.Status503ServiceUnavailable);
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(15), $"took {sw.Elapsed}");
}
/// <summary>Verifies that a peer timeout returns an uncertain result without trying the next peer.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]