|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using OpenClaw.Shared; |
| 9 | +using OpenClaw.TestSupport; |
| 10 | +using Xunit; |
| 11 | +using Xunit.Abstractions; |
| 12 | + |
| 13 | +namespace OpenClaw.SetupEngine.Tests; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Regression: WaitForNodeConnection must not report a caller-driven cancellation as a |
| 17 | +/// connection <see cref="NodeConnectionOutcome.Timeout"/>. The method links the caller's |
| 18 | +/// token with an internal CancelAfter(timeout); only the internal timeout firing is a |
| 19 | +/// Timeout, while the caller cancelling (user aborting setup) must propagate. Before the |
| 20 | +/// fix this test is red: the method returns Timeout ~0.3s into a 15s window. |
| 21 | +/// </summary> |
| 22 | +public sealed class SetupNodeConnectCancellationTests : IDisposable |
| 23 | +{ |
| 24 | + private readonly ITestOutputHelper _output; |
| 25 | + private readonly string _dataDir; |
| 26 | + |
| 27 | + public SetupNodeConnectCancellationTests(ITestOutputHelper output) |
| 28 | + { |
| 29 | + _output = output; |
| 30 | + _dataDir = Path.Combine(Path.GetTempPath(), "setup-cancel-" + Guid.NewGuid().ToString("N")); |
| 31 | + Directory.CreateDirectory(_dataDir); |
| 32 | + } |
| 33 | + |
| 34 | + public void Dispose() |
| 35 | + { |
| 36 | + try { Directory.Delete(_dataDir, recursive: true); } catch { /* best effort */ } |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public async Task WaitForNodeConnection_CallerCancellation_PropagatesInsteadOfTimeout() |
| 41 | + { |
| 42 | + // Completes the WS upgrade then stays silent, so the node client parks in the wait. |
| 43 | + using var server = new SilentWebSocketServer(); |
| 44 | + |
| 45 | + var logger = new SetupLogger(filePath: null, LogLevel.Trace); |
| 46 | + var ctx = new SetupContext( |
| 47 | + new SetupConfig(), logger, new TransactionJournal(filePath: null), |
| 48 | + new CommandRunner(logger), CancellationToken.None, |
| 49 | + dataDir: _dataDir, localDataDir: _dataDir); |
| 50 | + |
| 51 | + using var client = new WindowsNodeClient( |
| 52 | + $"ws://127.0.0.1:{server.Port}", "test-token-placeholder", _dataDir); |
| 53 | + |
| 54 | + var method = typeof(SetupStep).Assembly.GetTypes() |
| 55 | + .SelectMany(t => t.GetMethods(BindingFlags.NonPublic | BindingFlags.Static)) |
| 56 | + .Single(m => m.Name == "WaitForNodeConnection"); |
| 57 | + |
| 58 | + // Caller cancels 300ms in; the method's own timeout window is 15s, so an outcome |
| 59 | + // of "Timeout" is provably a misclassification of the caller's cancellation. |
| 60 | + using var callerCts = new CancellationTokenSource(TimeSpan.FromMilliseconds(300)); |
| 61 | + var sw = Stopwatch.StartNew(); |
| 62 | + var task = (Task)method.Invoke(null, |
| 63 | + new object[] { client, ctx, TimeSpan.FromSeconds(15), callerCts.Token })!; |
| 64 | + |
| 65 | + try |
| 66 | + { |
| 67 | + await task.WaitAsync(TimeSpan.FromSeconds(30)); |
| 68 | + sw.Stop(); |
| 69 | + var result = task.GetType().GetProperty("Result")!.GetValue(task)!; |
| 70 | + var outcome = result.GetType().GetProperty("Outcome")!.GetValue(result)!.ToString(); |
| 71 | + _output.WriteLine($"completed with Outcome={outcome} after {sw.ElapsedMilliseconds}ms (timeout window 15000ms)"); |
| 72 | + Assert.Fail( |
| 73 | + $"Caller cancellation was misclassified: returned Outcome={outcome} after {sw.ElapsedMilliseconds}ms " + |
| 74 | + "with the 15s timeout never elapsed and the caller's token cancelled. Expected OperationCanceledException."); |
| 75 | + } |
| 76 | + catch (OperationCanceledException) |
| 77 | + { |
| 78 | + sw.Stop(); |
| 79 | + _output.WriteLine($"caller cancellation propagated after {sw.ElapsedMilliseconds}ms"); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments