Skip to content

Commit 3960ece

Browse files
Merge pull request #2667 from github/robertbrignull/scrubber_tests
Try to fix flakes in query history scrubber tests
2 parents 87bbf29 + 2ffbb9c commit 3960ece

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

extensions/ql-vscode/src/query-history/query-history-scrubber.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import { getErrorMessage } from "../common/helpers-pure";
1010

1111
const LAST_SCRUB_TIME_KEY = "lastScrubTime";
1212

13-
type Counter = {
14-
increment: () => void;
15-
};
16-
1713
/**
1814
* Registers an interval timer that will periodically check for queries old enought
1915
* to be deleted.
@@ -37,8 +33,8 @@ export function registerQueryHistoryScrubber(
3733
qhm: QueryHistoryManager,
3834
ctx: ExtensionContext,
3935

40-
// optional counter to keep track of how many times the scrubber has run
41-
counter?: Counter,
36+
// optional callback to keep track of how many times the scrubber has run
37+
onScrubberRun?: () => void,
4238
): Disposable {
4339
const deregister = setInterval(
4440
scrubQueries,
@@ -48,7 +44,7 @@ export function registerQueryHistoryScrubber(
4844
queryHistoryDirs,
4945
qhm,
5046
ctx,
51-
counter,
47+
onScrubberRun,
5248
);
5349

5450
return {
@@ -64,7 +60,7 @@ async function scrubQueries(
6460
queryHistoryDirs: QueryHistoryDirs,
6561
qhm: QueryHistoryManager,
6662
ctx: ExtensionContext,
67-
counter?: Counter,
63+
onScrubberRun?: () => void,
6864
) {
6965
const lastScrubTime = ctx.globalState.get<number>(LAST_SCRUB_TIME_KEY);
7066
const now = Date.now();
@@ -76,7 +72,7 @@ async function scrubQueries(
7672

7773
let scrubCount = 0; // total number of directories deleted
7874
try {
79-
counter?.increment();
75+
onScrubberRun?.();
8076
void extLogger.log(
8177
"Cleaning up query history directories. Removing old entries.",
8278
);

extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-scrubber.test.ts

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,77 +13,61 @@ import {
1313
TWO_HOURS_IN_MS,
1414
} from "../../../../src/common/time";
1515
import { mockedObject } from "../../utils/mocking.helpers";
16+
import { DirResult } from "tmp";
1617

17-
describe("query history scrubber", () => {
18-
const now = Date.now();
18+
const now = Date.now();
19+
// We don't want our times to align exactly with the hour,
20+
// so we can better mimic real life
21+
const LESS_THAN_ONE_DAY = ONE_DAY_IN_MS - 1000;
1922

23+
describe("query history scrubber", () => {
2024
let deregister: vscode.Disposable | undefined;
21-
let mockCtx: vscode.ExtensionContext;
22-
let runCount = 0;
23-
24-
// We don't want our times to align exactly with the hour,
25-
// so we can better mimic real life
26-
const LESS_THAN_ONE_DAY = ONE_DAY_IN_MS - 1000;
27-
const tmpDir = dirSync({
28-
unsafeCleanup: true,
29-
});
25+
let tmpDir: DirResult;
3026

3127
beforeEach(() => {
28+
tmpDir = dirSync({
29+
unsafeCleanup: true,
30+
});
31+
3232
jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
3333

3434
jest.useFakeTimers({
3535
doNotFake: ["setTimeout"],
3636
now,
3737
});
38-
39-
mockCtx = {
40-
globalState: {
41-
lastScrubTime: now,
42-
get(key: string) {
43-
if (key !== "lastScrubTime") {
44-
throw new Error(`Unexpected key: ${key}`);
45-
}
46-
return this.lastScrubTime;
47-
},
48-
async update(key: string, value: any) {
49-
if (key !== "lastScrubTime") {
50-
throw new Error(`Unexpected key: ${key}`);
51-
}
52-
this.lastScrubTime = value;
53-
},
54-
},
55-
} as any as vscode.ExtensionContext;
5638
});
5739

5840
afterEach(() => {
5941
if (deregister) {
6042
deregister.dispose();
6143
deregister = undefined;
6244
}
45+
tmpDir.removeCallback();
6346
});
6447

6548
it("should not throw an error when the query directory does not exist", async () => {
66-
registerScrubber("idontexist");
49+
const mockCtx = createMockContext();
50+
const runCounter = registerScrubber("idontexist", mockCtx);
6751

6852
jest.advanceTimersByTime(ONE_HOUR_IN_MS);
6953
await wait();
7054
// "Should not have called the scrubber"
71-
expect(runCount).toBe(0);
55+
expect(runCounter).toHaveBeenCalledTimes(0);
7256

7357
jest.advanceTimersByTime(ONE_HOUR_IN_MS - 1);
7458
await wait();
7559
// "Should not have called the scrubber"
76-
expect(runCount).toBe(0);
60+
expect(runCounter).toHaveBeenCalledTimes(0);
7761

7862
jest.advanceTimersByTime(1);
7963
await wait();
8064
// "Should have called the scrubber once"
81-
expect(runCount).toBe(1);
65+
expect(runCounter).toHaveBeenCalledTimes(1);
8266

8367
jest.advanceTimersByTime(TWO_HOURS_IN_MS);
8468
await wait();
8569
// "Should have called the scrubber a second time"
86-
expect(runCount).toBe(2);
70+
expect(runCounter).toHaveBeenCalledTimes(2);
8771

8872
expect((mockCtx.globalState as any).lastScrubTime).toBe(
8973
now + TWO_HOURS_IN_MS * 2,
@@ -97,7 +81,7 @@ describe("query history scrubber", () => {
9781
TWO_HOURS_IN_MS,
9882
THREE_HOURS_IN_MS,
9983
);
100-
registerScrubber(queryDir);
84+
registerScrubber(queryDir, createMockContext());
10185

10286
jest.advanceTimersByTime(TWO_HOURS_IN_MS);
10387
await wait();
@@ -176,7 +160,31 @@ describe("query history scrubber", () => {
176160
return `query-${timestamp}`;
177161
}
178162

179-
function registerScrubber(dir: string) {
163+
function createMockContext(): vscode.ExtensionContext {
164+
return {
165+
globalState: {
166+
lastScrubTime: now,
167+
get(key: string) {
168+
if (key !== "lastScrubTime") {
169+
throw new Error(`Unexpected key: ${key}`);
170+
}
171+
return this.lastScrubTime;
172+
},
173+
async update(key: string, value: any) {
174+
if (key !== "lastScrubTime") {
175+
throw new Error(`Unexpected key: ${key}`);
176+
}
177+
this.lastScrubTime = value;
178+
},
179+
},
180+
} as any as vscode.ExtensionContext;
181+
}
182+
183+
function registerScrubber(
184+
dir: string,
185+
ctx: vscode.ExtensionContext,
186+
): jest.Mock {
187+
const onScrubberRun = jest.fn();
180188
deregister = registerQueryHistoryScrubber(
181189
ONE_HOUR_IN_MS,
182190
TWO_HOURS_IN_MS,
@@ -187,11 +195,10 @@ describe("query history scrubber", () => {
187195
return Promise.resolve();
188196
},
189197
}),
190-
mockCtx,
191-
{
192-
increment: () => runCount++,
193-
},
198+
ctx,
199+
onScrubberRun,
194200
);
201+
return onScrubberRun;
195202
}
196203

197204
async function wait(ms = 500) {

0 commit comments

Comments
 (0)