test(data): measure query cancellation latency from the cancel signal
The test timed from before CancelAfter, so a thread-pool timer firing late on a busy 4-core Windows runner (parallel tests block pool threads in synchronous SQLite steps) counted as interrupt latency. A dedicated thread now records the moment it cancels. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
1 file changed
+13
-2
@@ -240,10 +240,21 @@ public abstract class SessionTests(TestContext context) : DatabaseTest(context)
|
|||||||
"WITH RECURSIVE r(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM r WHERE n < 2000000000) SELECT count(*) FROM r",
|
"WITH RECURSIVE r(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM r WHERE n < 2000000000) SELECT count(*) FROM r",
|
||||||
"SELECT pg_sleep(10)");
|
"SELECT pg_sleep(10)");
|
||||||
using var cts = CancellationTokenSource.CreateLinkedTokenSource(Ct);
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(Ct);
|
||||||
cts.CancelAfter(300);
|
|
||||||
var watch = Stopwatch.StartNew();
|
var watch = Stopwatch.StartNew();
|
||||||
|
var cancelledAt = TimeSpan.Zero;
|
||||||
|
// A dedicated thread, not CancelAfter: a timer on a busy thread pool can fire late and skew the measurement.
|
||||||
|
var canceller = new Thread(() =>
|
||||||
|
{
|
||||||
|
Thread.Sleep(300);
|
||||||
|
cancelledAt = watch.Elapsed;
|
||||||
|
cts.Cancel();
|
||||||
|
})
|
||||||
|
{ IsBackground = true };
|
||||||
|
canceller.Start();
|
||||||
await Assert.ThrowsAsync<OperationCanceledException>(() => session.ExecuteAsync(sql, [], cts.Token));
|
await Assert.ThrowsAsync<OperationCanceledException>(() => session.ExecuteAsync(sql, [], cts.Token));
|
||||||
Assert.Less(watch.Elapsed.TotalSeconds, 2.0, "cancellation latency");
|
var stoppedAt = watch.Elapsed;
|
||||||
|
canceller.Join();
|
||||||
|
Assert.Less((stoppedAt - cancelledAt).TotalSeconds, 1.0, "cancellation latency");
|
||||||
Assert.Equal(1, await session.ScalarAsync<int>("SELECT 1", [], Ct));
|
Assert.Equal(1, await session.ScalarAsync<int>("SELECT 1", [], Ct));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user