-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathquery-history-scrubber.test.ts
More file actions
200 lines (167 loc) · 5.67 KB
/
query-history-scrubber.test.ts
File metadata and controls
200 lines (167 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { readdirSync, mkdirSync, writeFileSync } from "fs-extra";
import { join } from "path";
import * as vscode from "vscode";
import { extLogger } from "../../../../src/common";
import { registerQueryHistoryScrubber } from "../../../../src/query-history/query-history-scrubber";
import { QueryHistoryManager } from "../../../../src/query-history/query-history-manager";
import { dirSync } from "tmp-promise";
import {
ONE_DAY_IN_MS,
ONE_HOUR_IN_MS,
THREE_HOURS_IN_MS,
TWO_HOURS_IN_MS,
} from "../../../../src/pure/time";
import { mockedObject } from "../../utils/mocking.helpers";
describe("query history scrubber", () => {
const now = Date.now();
let deregister: vscode.Disposable | undefined;
let mockCtx: vscode.ExtensionContext;
let runCount = 0;
// We don't want our times to align exactly with the hour,
// so we can better mimic real life
const LESS_THAN_ONE_DAY = ONE_DAY_IN_MS - 1000;
const tmpDir = dirSync({
unsafeCleanup: true,
});
beforeEach(() => {
jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
jest.useFakeTimers({
doNotFake: ["setTimeout"],
now,
});
mockCtx = {
globalState: {
lastScrubTime: now,
get(key: string) {
if (key !== "lastScrubTime") {
throw new Error(`Unexpected key: ${key}`);
}
return this.lastScrubTime;
},
async update(key: string, value: any) {
if (key !== "lastScrubTime") {
throw new Error(`Unexpected key: ${key}`);
}
this.lastScrubTime = value;
},
},
} as any as vscode.ExtensionContext;
});
afterEach(() => {
if (deregister) {
deregister.dispose();
deregister = undefined;
}
});
it("should not throw an error when the query directory does not exist", async () => {
registerScrubber("idontexist");
jest.advanceTimersByTime(ONE_HOUR_IN_MS);
await wait();
// "Should not have called the scrubber"
expect(runCount).toBe(0);
jest.advanceTimersByTime(ONE_HOUR_IN_MS - 1);
await wait();
// "Should not have called the scrubber"
expect(runCount).toBe(0);
jest.advanceTimersByTime(1);
await wait();
// "Should have called the scrubber once"
expect(runCount).toBe(1);
jest.advanceTimersByTime(TWO_HOURS_IN_MS);
await wait();
// "Should have called the scrubber a second time"
expect(runCount).toBe(2);
expect((mockCtx.globalState as any).lastScrubTime).toBe(
now + TWO_HOURS_IN_MS * 2,
);
});
it("should scrub directories", async () => {
// create two query directories that are right around the cut off time
const queryDir = createMockQueryDir(
ONE_HOUR_IN_MS,
TWO_HOURS_IN_MS,
THREE_HOURS_IN_MS,
);
registerScrubber(queryDir);
jest.advanceTimersByTime(TWO_HOURS_IN_MS);
await wait();
// should have deleted only the invalid locations
expectDirectories(
queryDir,
toQueryDirName(ONE_HOUR_IN_MS),
toQueryDirName(TWO_HOURS_IN_MS),
toQueryDirName(THREE_HOURS_IN_MS),
);
jest.advanceTimersByTime(LESS_THAN_ONE_DAY);
await wait();
// nothing should have happened...yet
expectDirectories(
queryDir,
toQueryDirName(ONE_HOUR_IN_MS),
toQueryDirName(TWO_HOURS_IN_MS),
toQueryDirName(THREE_HOURS_IN_MS),
);
jest.advanceTimersByTime(1000);
await wait();
// should have deleted the two older directories
// even though they have different time stamps,
// they both expire during the same scrubbing period
expectDirectories(queryDir, toQueryDirName(THREE_HOURS_IN_MS));
// Wait until the next scrub time and the final directory is deleted
jest.advanceTimersByTime(TWO_HOURS_IN_MS);
await wait();
// should have deleted everything
expectDirectories(queryDir);
});
function expectDirectories(queryDir: string, ...dirNames: string[]) {
const files = readdirSync(queryDir);
expect(files.sort()).toEqual(dirNames.sort());
}
function createMockQueryDir(...timestamps: number[]) {
const dir = tmpDir.name;
const queryDir = join(dir, "query");
// create qyuery directory and fill it with some query directories
mkdirSync(queryDir);
// create an invalid file
const invalidFile = join(queryDir, "invalid.txt");
writeFileSync(invalidFile, "invalid");
// create a directory without a timestamp file
const noTimestampDir = join(queryDir, "noTimestampDir");
mkdirSync(noTimestampDir);
writeFileSync(join(noTimestampDir, "invalid.txt"), "invalid");
// create a directory with a timestamp file, but is invalid
const invalidTimestampDir = join(queryDir, "invalidTimestampDir");
mkdirSync(invalidTimestampDir);
writeFileSync(join(invalidTimestampDir, "timestamp"), "invalid");
// create a directories with a valid timestamp files from the args
timestamps.forEach((timestamp) => {
const dir = join(queryDir, toQueryDirName(timestamp));
mkdirSync(dir);
writeFileSync(join(dir, "timestamp"), `${now + timestamp}`);
});
return queryDir;
}
function toQueryDirName(timestamp: number) {
return `query-${timestamp}`;
}
function registerScrubber(dir: string) {
deregister = registerQueryHistoryScrubber(
ONE_HOUR_IN_MS,
TWO_HOURS_IN_MS,
LESS_THAN_ONE_DAY,
{ localQueriesDirPath: dir, variantAnalysesDirPath: dir },
mockedObject<QueryHistoryManager>({
removeDeletedQueries: () => {
return Promise.resolve();
},
}),
mockCtx,
{
increment: () => runCount++,
},
);
}
async function wait(ms = 500) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
});