This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 659
/
TestServerWorker.ts
415 lines (362 loc) · 11.3 KB
/
TestServerWorker.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import {Server, ServerRequest, WorkerBridge} from "@internal/core";
import {ErrorFrame} from "@internal/errors";
import {
InspectorClient,
InspectorClientCloseError,
urlToFilename,
} from "@internal/v8";
import {createWebSocketClient} from "@internal/codec-websocket";
import TestServer from "@internal/core/server/testing/TestServer";
import {
DIAGNOSTIC_CATEGORIES,
createSingleDiagnosticsError,
deriveDiagnosticFromErrorStructure,
} from "@internal/diagnostics";
import {markup} from "@internal/markup";
import {ReporterProgress} from "@internal/cli-reporter";
import {
AbsoluteFilePathMap,
AbsoluteFilePathSet,
createPath,
} from "@internal/path";
import {ansiEscapes} from "@internal/cli-layout";
import {PathLocker} from "@internal/async/lockers";
import TestServerFile from "@internal/core/server/testing/TestServerFile";
import {BridgeServer} from "@internal/events";
import {Duration} from "@internal/numbers";
import {ThreadWorkerContainer} from "@internal/core/worker/types";
import {createResourceFromTimeout} from "@internal/resources";
export default class TestServerWorker {
constructor(
{server, request, runner, container}: {
server: Server;
container: ThreadWorkerContainer;
runner: TestServer;
request: ServerRequest;
},
) {
this.server = server;
this.runner = runner;
this.request = request;
this.thread = container.thread;
this.bridge = container.bridge;
this.inspector = undefined;
this.prepareLock = new PathLocker();
this.preparedPaths = new AbsoluteFilePathSet();
this.transferredCompiled = new AbsoluteFilePathSet();
}
private server: Server;
private request: ServerRequest;
private runner: TestServer;
private transferredCompiled: AbsoluteFilePathSet;
private preparedPaths: AbsoluteFilePathSet;
private prepareLock: PathLocker;
public bridge: BridgeServer<typeof WorkerBridge>;
public thread: ThreadWorkerContainer["thread"];
public inspector: undefined | InspectorClient;
public async init() {
const {bridge, runner} = this;
bridge.startHeartbeatMonitor(
Duration.fromSeconds(10),
() => {
this.server.fatalErrorHandler.wrapPromise(
this.handleTimeout("10 seconds"),
);
},
);
// Start debugger
const {inspectorUrl} = await bridge.events.inspectorDetails.call();
if (inspectorUrl !== undefined) {
const client = new InspectorClient(
await createWebSocketClient(inspectorUrl),
);
this.inspector = client;
this.thread.resources.add(client);
await client.call("Debugger.enable");
// When a debugger is attached there's always a "Debugger attached" log emitted
// This is written to stderr from native and there's no way for us to intercept it, and no way to disable it
// https://github.com/nodejs/node/issues/34799
// Until we have a way to disable it we need to resort to grossness like this...
process.stderr.write(ansiEscapes.cursorUp() + ansiEscapes.eraseLine);
}
bridge.events.testDiskSnapshotDiscovered.subscribe((
{testPath, snapshotPath},
) => {
this.runner.files.assert(testPath).discoveredDiskSnapshot(
snapshotPath,
this,
);
});
bridge.events.testSnapshotEntry.subscribe(({testPath, snapshotPath, entry}) => {
this.runner.files.assert(testPath).addSnapshotEntry(snapshotPath, entry);
});
bridge.events.testInlineSnapshotUpdate.subscribe(({testPath, update}) => {
this.runner.files.assert(testPath).addInlineSnapshotUpdate(update);
});
bridge.events.testDiagnostic.subscribe(({ref, diagnostic}) => {
if (ref !== undefined) {
this.runner.files.assert(ref.path).onDiagnostics();
}
runner.addDiagnostic(diagnostic, ref);
});
}
public async handleTimeout(duration: string): Promise<void> {
return new Promise((resolve, reject) => {
const timeout = createResourceFromTimeout(
"TimeoutResolver",
setTimeout(
() => {
resolve(
this.bridge.end(
`Test worker was unresponsive for ${duration}. We tried to collect some additional metadata but we timed out again trying to fetch it...`,
false,
),
);
},
3_000,
),
);
if (this.inspector === undefined) {
timeout.release();
} else {
this.inspector.resources.add(timeout);
}
this._handleTimeout(duration).then(() => {
timeout.release();
resolve();
}).catch((err) => {
timeout.release();
if (err instanceof InspectorClientCloseError) {
this.bridge.end(
`Test worker was unresponsive for ${duration}. We tried to collect some additional metadata but the inspector connection closed abruptly`,
false,
);
} else {
reject(err);
}
});
});
}
private async _handleTimeout(duration: string): Promise<void> {
const {inspector, bridge} = this;
if (inspector === undefined || !inspector.alive) {
await bridge.end(
`Test worker was unresponsive for ${duration}. There was no inspector connected so we were unable to capture stack frames before it was terminated.`,
false,
);
return undefined;
}
inspector.call("Debugger.pause");
const params = await inspector.wait("Debugger.paused");
const frames: ErrorFrame[] = [];
const callFrames = Array.from(params.get("callFrames").asIterable()).slice(
0,
20,
);
for (const callFrame of callFrames) {
const loc = callFrame.get("location");
const resolved = this.runner.sourceMaps.assertApproxOriginalPositionFor(
createPath(urlToFilename(callFrame.get("url").asString())),
loc.get("lineNumber").asZeroIndexedNumber().toOneIndexed(),
loc.get("columnNumber").asZeroIndexedNumber(),
);
const name = callFrame.get("scopeChain").getIndex(0).get("name").required(
"",
).asString().split("$").pop();
frames.push({
resolvedLocation: resolved.found,
typeName: undefined,
functionName: name,
methodName: undefined,
path: resolved.source,
lineNumber: resolved.line,
columnNumber: resolved.column,
isTopLevel: false,
isEval: false,
isNative: false,
isConstructor: false,
isAsync: false,
});
}
await bridge.endWithError(
createSingleDiagnosticsError(
deriveDiagnosticFromErrorStructure(
{
name: "Error",
frames,
},
{
description: {
category: DIAGNOSTIC_CATEGORIES["tests/timeout"],
message: markup`Test worker was unresponsive for <emphasis>${duration}</emphasis>. Possible infinite loop. Below is a stack trace before the test was terminated.`,
advice: [
{
type: "log",
category: "info",
text: markup`You can find the specific test that caused this by running <code>rome test --run-in-sync</code>`,
},
],
},
},
),
),
);
}
public async prepareAll(
progress: ReporterProgress,
fileQueue: TestServerFile[],
): Promise<void> {
const {inspector, runner} = this;
const {options: opts} = runner;
if (inspector !== undefined && opts.coverage) {
await inspector.call("Profiler.enable");
await inspector.call(
"Profiler.startPreciseCoverage",
{
// Turning this on disables V8 optimizations https://v8.dev/blog/javascript-code-coverage#precise-coverage-(function-granularity)
callCount: false,
// Otherwise coverage will only have function granularity
detailed: true,
},
);
}
while (fileQueue.length > 0) {
const file = fileQueue.pop()!;
await this.prepareTest({file, progress, partial: false});
}
}
public async prepareTest(
{file, progress, partial}: {
partial: boolean;
file: TestServerFile;
progress?: ReporterProgress;
},
) {
const {bridge, runner} = this;
const globalOptions = runner.options;
const {ref, bundle} = file;
const req = this.request;
const {flags} = req.client;
const path = ref.real;
const lock = this.prepareLock.getNewLock(path);
this.preparedPaths.add(path);
let progressId;
if (progress !== undefined) {
progressId = progress.pushText(ref.uid);
}
try {
const assembled = bundle.entry.js.assembled;
// Transfer over compiled code that this test worker needs to assemble the file but doesn't have
const pending: AbsoluteFilePathMap<string> = new AbsoluteFilePathMap();
for (const item of assembled) {
if (item[0] === 1) {
const path = item[1];
if (!this.transferredCompiled.has(path)) {
this.transferredCompiled.add(path);
const compiled = bundle.bundler.compiles.get(path);
if (compiled !== undefined) {
pending.set(path, compiled.value.compiledCode);
}
}
}
}
if (pending.size > 0) {
await bridge.events.testReceiveCompiledDependency.call(pending);
}
const {focusedTests, foundTests} = await bridge.events.testPrepare.call({
globalOptions,
partial,
contextDirectory: req.server.projectManager.getRootProjectForPath(
ref.real,
).directory,
projectDirectory: req.server.projectManager.assertProjectExisting(
ref.real,
).directory.join(),
path,
cwd: flags.cwd.join(),
assembled,
logFound: true,
});
if (!partial) {
for (const [testName, callsiteLocation] of foundTests) {
runner.onTestFound({testName, path}, callsiteLocation);
}
for (const test of focusedTests) {
runner.focusedTests.push(test);
}
}
} catch (err) {
runner.handlePossibleBridgeError(err, bridge);
}
lock.release();
if (progress !== undefined && progressId !== undefined) {
progress.popText(progressId);
progress.tick();
}
}
private async runTest(): Promise<void> {
const {bridge} = this;
// Find a test we've already prepared
for (const path of this.preparedPaths) {
const file = this.runner.files.assert(path);
for (const testName of file.getPendingTests()) {
file.removePendingTest(testName, this);
await this.prepareLock.waitLock(path);
await bridge.events.testRun.call({
path,
testNames: [testName],
});
if (file.markCompletedTest()) {
// Start this async
await Promise.all([file.teardown(), this.runTest()]);
return;
} else {
return this.runTest();
}
}
// Exhausted all tests in this file, no longer consider it for future tests
this.preparedPaths.delete(path);
}
// Prepare another file with pending tests
while (this.runner.testFilesStack.length > 0) {
const path = this.runner.testFilesStack.shift()!;
const file = this.runner.files.assert(path);
const pendingTests = file.getPendingTests();
if (pendingTests.size === 0) {
continue;
} else {
this.runner.testFilesStack.push(path);
}
await this.prepareTest({partial: true, file, progress: undefined});
return this.runTest();
}
}
public async run() {
const {bridge, inspector, runner} = this;
const {options: opts} = runner;
try {
const promises: Promise<void>[] = [];
for (let i = 0; i < 5; i++) {
promises.push(this.runTest());
}
await Promise.all(promises);
} catch (err) {
runner.handlePossibleBridgeError(err, bridge);
} finally {
if (inspector !== undefined) {
if (opts.coverage) {
if (inspector.alive) {
const profile = await inspector.call("Profiler.takePreciseCoverage");
runner.coverageCollector.addCoverage(profile.get("result").asAny());
// Not really necessary but let's clean up anyway for completeness
await inspector.call("Profiler.stopPreciseCoverage");
await inspector.call("Profiler.disable");
} else {
// TODO log that we failed to fetch some coverage
}
}
await inspector.resources.release();
}
}
}
}