Skip to content

Commit c2b61a4

Browse files
authored
perf(test): fake audit contention retry clock (#126213)
1 parent e8fbaf2 commit c2b61a4

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

‎src/audit/audit-event-writer.test.ts‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DatabaseSync } from "node:sqlite";
2-
import { afterEach, describe, expect, it } from "vitest";
2+
import { afterEach, describe, expect, it, vi } from "vitest";
33
import type { DecisionReceiptV1 } from "../../packages/gateway-protocol/src/index.js";
44
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
55
import { readSqliteBusyTimeout } from "../infra/sqlite-busy-timeout.js";
@@ -459,23 +459,42 @@ describe("audit event writer", () => {
459459
const { path } = openOpenClawStateDatabase(database);
460460
const contender = new DatabaseSync(path);
461461
contender.exec("PRAGMA busy_timeout = 0; BEGIN IMMEDIATE");
462+
let fakeTimersActive = false;
462463

463464
try {
465+
vi.useFakeTimers({
466+
toFake: ["setImmediate", "clearImmediate", "setTimeout", "clearTimeout"],
467+
});
468+
fakeTimersActive = true;
464469
expect(
465470
writer.record({
466471
...input(),
467472
sourceId: "sustained-contention",
468473
runId: "sustained-contention",
469474
}),
470475
).toBe(true);
471-
await new Promise<void>((resolve) => {
472-
setTimeout(resolve, 1_750);
473-
});
476+
await vi.advanceTimersByTimeAsync(1_750);
474477
expect(contentions).toEqual(["audit event persistence delayed by SQLite lock contention"]);
475478
} finally {
476-
contender.exec("ROLLBACK");
477-
contender.close();
478-
await writer.stop();
479+
try {
480+
try {
481+
contender.exec("ROLLBACK");
482+
} finally {
483+
contender.close();
484+
}
485+
} finally {
486+
try {
487+
const stopPromise = writer.stop();
488+
if (fakeTimersActive) {
489+
await vi.advanceTimersToNextTimerAsync();
490+
}
491+
await stopPromise;
492+
} finally {
493+
if (fakeTimersActive) {
494+
vi.useRealTimers();
495+
}
496+
}
497+
}
479498
}
480499

481500
expect(errors).toEqual([]);

0 commit comments

Comments
 (0)