From 62080c5d1270b1ac0701123648b4e5a7f66a2e39 Mon Sep 17 00:00:00 2001 From: telli Date: Thu, 1 Oct 2026 09:16:36 -0700 Subject: [PATCH] test: stabilize nacos retry timing assertion --- src/OpenClaw.Tests/FakeNacosRouterMcpTools.cs | 5 +++-- src/OpenClaw.Tests/NacosRouterIntegrationTests.cs | 14 ++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/OpenClaw.Tests/FakeNacosRouterMcpTools.cs b/src/OpenClaw.Tests/FakeNacosRouterMcpTools.cs index cad0d0b6..e9eb2731 100644 --- a/src/OpenClaw.Tests/FakeNacosRouterMcpTools.cs +++ b/src/OpenClaw.Tests/FakeNacosRouterMcpTools.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.Diagnostics; using System.Text.Json; using ModelContextProtocol.Protocol; using ModelContextProtocol.Server; @@ -22,7 +23,7 @@ public sealed class NacosRouterFixtureState public HashSet SucceedUseServers { get; } = new(StringComparer.Ordinal); // Issue #233: timestamps of every use_tool call, for backoff assertions. - public List UseTimestamps { get; } = []; + public List UseTimestamps { get; } = []; } // Parameters and prose envelopes follow the pinned upstream Python Router. @@ -58,7 +59,7 @@ public string Add(string mcp_server_name) [McpServerTool(Name = "use_tool"), Description("Proxy an installed tool.")] public CallToolResult Use(string mcp_server_name, string mcp_tool_name, string @params) { - state.UseTimestamps.Add(DateTimeOffset.UtcNow); + state.UseTimestamps.Add(Stopwatch.GetTimestamp()); state.Calls.Add("use:" + mcp_server_name + ":" + mcp_tool_name); var serverOk = mcp_server_name == "weather-mcp" || state.SucceedUseServers.Contains(mcp_server_name); var invalid = !serverOk || mcp_tool_name != "get_weather"; diff --git a/src/OpenClaw.Tests/NacosRouterIntegrationTests.cs b/src/OpenClaw.Tests/NacosRouterIntegrationTests.cs index e6bd59af..15f43aac 100644 --- a/src/OpenClaw.Tests/NacosRouterIntegrationTests.cs +++ b/src/OpenClaw.Tests/NacosRouterIntegrationTests.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using System.Reflection; using System.Text.Json; using Microsoft.AspNetCore.Builder; @@ -296,10 +297,15 @@ public async Task DynamicSlot_UseToolFailureRetries_ThenFallsBack(bool maf) Assert.Equal(new[] { "search", "add:weather-mcp", "use:weather-mcp:get_weather", "use:weather-mcp:get_weather", "use:weather-mcp:get_weather" }, state.Calls); var timestamps = state.UseTimestamps; Assert.Equal(3, timestamps.Count); - Assert.True((timestamps[1] - timestamps[0]).TotalMilliseconds >= 100, - $"expected >= 100 ms backoff between attempts 1 and 2, got {(timestamps[1] - timestamps[0]).TotalMilliseconds:0} ms"); - Assert.True((timestamps[2] - timestamps[1]).TotalMilliseconds >= 100, - $"expected >= 100 ms backoff between attempts 2 and 3, got {(timestamps[2] - timestamps[1]).TotalMilliseconds:0} ms"); + const double configuredBackoffMilliseconds = 100; + const double timerToleranceMilliseconds = 10; + for (var attempt = 1; attempt < timestamps.Count; attempt++) + { + var elapsed = Stopwatch.GetElapsedTime(timestamps[attempt - 1], timestamps[attempt]).TotalMilliseconds; + Assert.True(elapsed >= configuredBackoffMilliseconds - timerToleranceMilliseconds, + $"expected approximately {configuredBackoffMilliseconds:0} ms backoff between attempts {attempt} and {attempt + 1}, " + + $"allowing {timerToleranceMilliseconds:0} ms timer tolerance; got {elapsed:0.000} ms"); + } var run = Assert.Single(session.MetaRunHistory); var query = Assert.Single(run.StepResults, step => step.Id == "query"); Assert.Equal("capability_execution_failed", query.FailureCode);