Use a jest.fn() to track calls to the scrubber, instead of a local variable counter

This commit is contained in:
Robert
2023-08-03 14:17:23 +01:00
parent 28a0075fe4
commit 8bce7b531c
2 changed files with 17 additions and 19 deletions

View File

@@ -10,10 +10,6 @@ import { getErrorMessage } from "../common/helpers-pure";
const LAST_SCRUB_TIME_KEY = "lastScrubTime";
type Counter = {
increment: () => void;
};
/**
* Registers an interval timer that will periodically check for queries old enought
* to be deleted.
@@ -37,8 +33,8 @@ export function registerQueryHistoryScrubber(
qhm: QueryHistoryManager,
ctx: ExtensionContext,
// optional counter to keep track of how many times the scrubber has run
counter?: Counter,
// optional callback to keep track of how many times the scrubber has run
onScrubberRun?: () => void,
): Disposable {
const deregister = setInterval(
scrubQueries,
@@ -48,7 +44,7 @@ export function registerQueryHistoryScrubber(
queryHistoryDirs,
qhm,
ctx,
counter,
onScrubberRun,
);
return {
@@ -64,7 +60,7 @@ async function scrubQueries(
queryHistoryDirs: QueryHistoryDirs,
qhm: QueryHistoryManager,
ctx: ExtensionContext,
counter?: Counter,
onScrubberRun?: () => void,
) {
const lastScrubTime = ctx.globalState.get<number>(LAST_SCRUB_TIME_KEY);
const now = Date.now();
@@ -76,7 +72,7 @@ async function scrubQueries(
let scrubCount = 0; // total number of directories deleted
try {
counter?.increment();
onScrubberRun?.();
void extLogger.log(
"Cleaning up query history directories. Removing old entries.",
);

View File

@@ -21,7 +21,6 @@ const LESS_THAN_ONE_DAY = ONE_DAY_IN_MS - 1000;
describe("query history scrubber", () => {
let deregister: vscode.Disposable | undefined;
let runCount = 0;
const tmpDir = dirSync({
unsafeCleanup: true,
@@ -45,27 +44,27 @@ describe("query history scrubber", () => {
it("should not throw an error when the query directory does not exist", async () => {
const mockCtx = createMockContext();
registerScrubber("idontexist", mockCtx);
const runCounter = registerScrubber("idontexist", mockCtx);
jest.advanceTimersByTime(ONE_HOUR_IN_MS);
await wait();
// "Should not have called the scrubber"
expect(runCount).toBe(0);
expect(runCounter).toHaveBeenCalledTimes(0);
jest.advanceTimersByTime(ONE_HOUR_IN_MS - 1);
await wait();
// "Should not have called the scrubber"
expect(runCount).toBe(0);
expect(runCounter).toHaveBeenCalledTimes(0);
jest.advanceTimersByTime(1);
await wait();
// "Should have called the scrubber once"
expect(runCount).toBe(1);
expect(runCounter).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(TWO_HOURS_IN_MS);
await wait();
// "Should have called the scrubber a second time"
expect(runCount).toBe(2);
expect(runCounter).toHaveBeenCalledTimes(2);
expect((mockCtx.globalState as any).lastScrubTime).toBe(
now + TWO_HOURS_IN_MS * 2,
@@ -178,7 +177,11 @@ describe("query history scrubber", () => {
} as any as vscode.ExtensionContext;
}
function registerScrubber(dir: string, ctx: vscode.ExtensionContext) {
function registerScrubber(
dir: string,
ctx: vscode.ExtensionContext,
): jest.Mock {
const onScrubberRun = jest.fn();
deregister = registerQueryHistoryScrubber(
ONE_HOUR_IN_MS,
TWO_HOURS_IN_MS,
@@ -190,10 +193,9 @@ describe("query history scrubber", () => {
},
}),
ctx,
{
increment: () => runCount++,
},
onScrubberRun,
);
return onScrubberRun;
}
async function wait(ms = 500) {