test: pass TestContext.Current.CancellationToken at xUnit1051 call sites (analyzer code fix, 451 sites) (lmxopcua#725)

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 13:19:34 -05:00
parent 5db01053e8
commit 1d7a4b7a53
77 changed files with 452 additions and 457 deletions
@@ -107,7 +107,7 @@ public sealed class ScriptedAlarmEngineRunGuardTests
await WaitForAsync(() => eng.RunGuard.StartedRunCount > before);
}
eng.RunGuard.StartedRunCount.ShouldBe(1 + 1 + pushes, "one runaway dispatch, then healthy-only dispatches");
await Task.Delay(300); // absence window for trailing emissions
await Task.Delay(300, TestContext.Current.CancellationToken); // absence window for trailing emissions
ScriptedAlarmEvent[] snapshot;
lock (events) snapshot = events.ToArray();
@@ -215,7 +215,7 @@ public sealed class ScriptedAlarmEngineTests
eng.OnEvent += (_, e) => events.Add(e);
up.Push("Temp", 150);
await Task.Delay(200);
await Task.Delay(200, TestContext.Current.CancellationToken);
events.Any(e => e.Emission == EmissionKind.Activated).ShouldBeFalse(
"OneShot shelve suppresses activation emission");
@@ -252,7 +252,7 @@ public sealed class ScriptedAlarmEngineTests
await eng.DisableAsync("HighTemp", "alice", TestContext.Current.CancellationToken);
up.Push("Temp", 150);
await Task.Delay(100);
await Task.Delay(100, TestContext.Current.CancellationToken);
eng.GetState("HighTemp")!.Active.ShouldBe(AlarmActiveState.Inactive,
"disabled alarm ignores predicate");
@@ -353,7 +353,7 @@ public sealed class ScriptedAlarmEngineTests
up.Push($"Temp{round % alarmCount}", round % 2 == 0 ? 150 : 50);
round++;
}
});
}, TestContext.Current.CancellationToken);
// Readers: continuously touch the three unguarded read paths.
var readers = Enumerable.Range(0, 4).Select(_reader => Task.Run(() =>
@@ -839,7 +839,7 @@ public sealed class ScriptedAlarmEngineTests
// Dispose must wait for the in-flight reevaluation to complete rather
// than returning while a background task still runs.
var disposeTask = Task.Run(() => eng.Dispose());
var disposeTask = Task.Run(() => eng.Dispose(), TestContext.Current.CancellationToken);
// Verify Dispose does NOT complete immediately — it should block waiting
// for the in-flight task. Without the -006 fix Dispose returns straight
@@ -1003,7 +1003,7 @@ public sealed class ScriptedAlarmEngineTests
// load could keep firing between the failed second load and Dispose.
// After the fix, it is stopped at the START of every LoadAsync attempt
// (successful or not), so Dispose always finds a clean timer state.
var disposeTask = Task.Run(() => eng.Dispose());
var disposeTask = Task.Run(() => eng.Dispose(), TestContext.Current.CancellationToken);
await disposeTask.WaitAsync(TimeSpan.FromSeconds(3), TestContext.Current.CancellationToken);
// If Dispose threw or hung, the WaitAsync would surface it.
}
@@ -1748,12 +1748,12 @@ public sealed class ScriptedAlarmEngineTests
eng.TrackBackgroundTaskForTest(() => gate.Task);
eng.InFlightCountForTest.ShouldBe(1);
var disposeTask = Task.Run(() => eng.Dispose());
var disposeTask = Task.Run(() => eng.Dispose(), TestContext.Current.CancellationToken);
var raced = await Task.WhenAny(disposeTask, Task.Delay(300));
raced.ShouldNotBe(disposeTask, "Dispose must not return while registered background work is still running");
gate.SetResult();
await disposeTask.WaitAsync(TimeSpan.FromSeconds(5));
await disposeTask.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
eng.InFlightCountForTest.ShouldBe(0, "completed work must unregister itself");
}
@@ -1785,7 +1785,7 @@ public sealed class ScriptedAlarmEngineTests
() => throw new InvalidOperationException("factory failed (test)")));
eng.InFlightCountForTest.ShouldBe(0, "work that never started must not be left registered");
await Task.Run(() => eng.Dispose()).WaitAsync(TimeSpan.FromSeconds(5));
await Task.Run(() => eng.Dispose(), TestContext.Current.CancellationToken).WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
}
/// <summary>Verifies a faulted background task is still drained by Dispose and its failure is
@@ -1801,11 +1801,11 @@ public sealed class ScriptedAlarmEngineTests
// Fault it while Dispose is actually blocked on the drain, so the WhenAll observes
// the failure rather than finding an already-unregistered task.
var disposeTask = Task.Run(() => Should.NotThrow(() => eng.Dispose()));
await Task.Delay(200);
var disposeTask = Task.Run(() => Should.NotThrow(() => eng.Dispose()), TestContext.Current.CancellationToken);
await Task.Delay(200, TestContext.Current.CancellationToken);
gate.SetException(new InvalidOperationException("background failed (test)"));
await disposeTask.WaitAsync(TimeSpan.FromSeconds(5));
await disposeTask.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
eng.InFlightCountForTest.ShouldBe(0);
}
@@ -46,7 +46,7 @@ public sealed class ScriptedAlarmSourceLoggingTests
if (sinkCapture.Events.Any(e => e.Level == LogEventLevel.Warning && e.Exception?.Message == "subscriber broke"))
break;
}
await Task.Delay(25);
await Task.Delay(25, TestContext.Current.CancellationToken);
}
LogEvent warning;
@@ -53,7 +53,7 @@ public sealed class ScriptedAlarmSourceTests
var handle = await source.SubscribeAlarmsAsync([], TestContext.Current.CancellationToken);
up.Push("Temp", 150);
await Task.Delay(200);
await Task.Delay(200, TestContext.Current.CancellationToken);
events.Count.ShouldBe(1);
events[0].ConditionId.ShouldBe("Plant/Line1::HighTemp");
@@ -80,7 +80,7 @@ public sealed class ScriptedAlarmSourceTests
var handle = await source.SubscribeAlarmsAsync(["Plant/Line1"], TestContext.Current.CancellationToken);
up.Push("Temp", 150);
await Task.Delay(200);
await Task.Delay(200, TestContext.Current.CancellationToken);
events.Count.ShouldBe(1);
events[0].SourceNodeId.ShouldBe("Plant/Line1");
@@ -102,7 +102,7 @@ public sealed class ScriptedAlarmSourceTests
await source.UnsubscribeAlarmsAsync(handle, TestContext.Current.CancellationToken);
up.Push("Temp", 150);
await Task.Delay(200);
await Task.Delay(200, TestContext.Current.CancellationToken);
events.Count.ShouldBe(0);
}
@@ -124,7 +124,7 @@ public sealed class ScriptedAlarmSourceTests
// Drive HighTemp's input Bad: the predicate freezes (no transition), but the worst-quality bucket
// moves Good→Bad → the engine emits QualityChanged. The source must swallow it.
up.Push("Temp", 50, 0x80000000u);
await Task.Delay(200);
await Task.Delay(200, TestContext.Current.CancellationToken);
events.ShouldBeEmpty();
}
@@ -138,7 +138,7 @@ public sealed class ScriptedAlarmSourceTests
using var _s = source;
up.Push("Temp", 150);
await Task.Delay(200);
await Task.Delay(200, TestContext.Current.CancellationToken);
engine.GetState("Plant/Line1::HighTemp")!.Acked.ShouldBe(AlarmAckedState.Unacknowledged);
await source.AcknowledgeAsync([new AlarmAcknowledgeRequest(
@@ -165,7 +165,7 @@ public sealed class ScriptedAlarmSourceTests
// Supplied principal -> honored.
up.Push("Temp", 150);
await Task.Delay(200);
await Task.Delay(200, TestContext.Current.CancellationToken);
await source.AcknowledgeAsync([new AlarmAcknowledgeRequest(
"Plant/Line1", "Plant/Line1::HighTemp", "ack as bob", "bob")],
TestContext.Current.CancellationToken);
@@ -173,9 +173,9 @@ public sealed class ScriptedAlarmSourceTests
// No principal -> default audit identity.
up.Push("Temp", 50);
await Task.Delay(200);
await Task.Delay(200, TestContext.Current.CancellationToken);
up.Push("Temp", 150);
await Task.Delay(200);
await Task.Delay(200, TestContext.Current.CancellationToken);
await source.AcknowledgeAsync([new AlarmAcknowledgeRequest(
"Plant/Line1", "Plant/Line1::HighTemp", "ack anon", null)],
TestContext.Current.CancellationToken);