Skip to content

Commit 219dbd6

Browse files
authored
test: unflake debugger and REPL tests
Run the debugger function-formatting checks against an auto-resumed, long-lived target so they do not depend on initial-break rendering. Use a synchronous VM evaluator for the REPL handleError test. Submit the throwing input separately and wait for clean REPL teardown. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64718 Refs: https://github.com/nodejs/node/actions/runs/30080039199/job/89439279468?pr=64339 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 745d896 commit 219dbd6

3 files changed

Lines changed: 64 additions & 20 deletions

File tree

test/parallel/test-debugger-extract-function-name.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import startCLI from '../common/debugger.js';
77

88
import assert from 'assert';
99

10-
const cli = startCLI([fixtures.path('debugger', 'three-lines.js')]);
10+
const env = {
11+
...process.env,
12+
NODE_INSPECT_RESUME_ON_START: '1',
13+
};
14+
const cli = startCLI(
15+
[fixtures.path('debugger', 'alive.js')], [], { env });
1116

1217
try {
13-
await cli.waitForInitialBreak();
1418
await cli.waitForPrompt();
1519
await cli.command('exec a = function func() {}; a;');
1620
assert.match(cli.output, /\[Function: func\]/);

test/parallel/test-repl-user-error-handler.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ const { PassThrough } = require('node:stream');
66
const { once } = require('node:events');
77
const test = require('node:test');
88
const { spawn } = require('node:child_process');
9+
const vm = require('node:vm');
910

1011
common.skipIfInspectorDisabled();
1112

13+
function evaluate(code, context, filename, callback) {
14+
try {
15+
callback(null, vm.runInContext(code, context, { filename }));
16+
} catch (err) {
17+
callback(err);
18+
}
19+
}
20+
1221
function* generateCases() {
1322
for (const async of [false, true]) {
1423
for (const handleErrorReturn of ['ignore', 'print', 'unhandled', 'badvalue']) {
@@ -23,13 +32,12 @@ function* generateCases() {
2332

2433
for (const { async, handleErrorReturn } of generateCases()) {
2534
test(`async: ${async}, handleErrorReturn: ${handleErrorReturn}`, async () => {
26-
let err;
2735
const options = {
2836
input: new PassThrough(),
2937
output: new PassThrough().setEncoding('utf8'),
30-
handleError: common.mustCall((e) => {
31-
err = e;
32-
queueMicrotask(() => repl.emit('handled-error'));
38+
eval: evaluate,
39+
handleError: common.mustCall((err) => {
40+
queueMicrotask(() => repl.emit('handled-error', err));
3341
return handleErrorReturn;
3442
})
3543
};
@@ -45,13 +53,16 @@ for (const { async, handleErrorReturn } of generateCases()) {
4553
let outputString = '';
4654
options.output.on('data', (chunk) => { outputString += chunk; });
4755

48-
const inputString = async ?
49-
'setImmediate(() => { throw new Error("testerror") })\n42\n' :
50-
'throw new Error("testerror")\n42\n';
51-
options.input.end(inputString);
56+
const errorInput = async ?
57+
'setImmediate(() => { throw new Error("testerror") })\n' :
58+
'throw new Error("testerror")\n';
59+
const handledErrorEvent = once(repl, 'handled-error');
60+
options.input.write(errorInput);
5261

53-
await once(repl, 'handled-error');
62+
const [err] = await handledErrorEvent;
5463
assert.strictEqual(err.message, 'testerror');
64+
const exitEvent = once(repl, 'exit');
65+
options.input.end('42\n');
5566
while (!/42/.test(outputString)) {
5667
await once(options.output, 'data');
5768
}
@@ -66,6 +77,7 @@ for (const { async, handleErrorReturn } of generateCases()) {
6677
const [uncaughtErr] = await uncaughtExceptionEvent;
6778
assert.strictEqual(uncaughtErr, err);
6879
}
80+
await exitEvent;
6981
});
7082
}
7183

test/sequential/test-debug-prompt.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,45 @@
22

33
const common = require('../common');
44
common.skipIfInspectorDisabled();
5+
const assert = require('assert');
6+
const fixtures = require('../common/fixtures');
57
const spawn = require('child_process').spawn;
68

7-
const proc = spawn(process.execPath, ['inspect', 'foo']);
9+
const proc = spawn(process.execPath, [
10+
'inspect', fixtures.path('debugger', 'alive.js'),
11+
]);
812
proc.stdout.setEncoding('utf8');
913

10-
let needToSendExit = true;
14+
const TIMEOUT = common.platformTimeout(10_000);
1115
let output = '';
12-
proc.stdout.on('data', (data) => {
13-
output += data;
14-
if (output.includes('debug> ') && needToSendExit) {
15-
proc.stdin.write('.exit\n');
16-
needToSendExit = false;
17-
}
18-
});
16+
let promptSeen = false;
17+
18+
(async () => {
19+
await new Promise((resolve, reject) => {
20+
const timer = setTimeout(() => {
21+
proc.kill();
22+
reject(new Error(`Timed out waiting for the debugger prompt; output: ${output}`));
23+
}, TIMEOUT);
24+
25+
proc.stdout.on('data', (data) => {
26+
output += data;
27+
if (output.includes('debug> ') && !promptSeen) {
28+
promptSeen = true;
29+
proc.stdin.end('.exit\n');
30+
}
31+
});
32+
33+
proc.once('error', reject);
34+
proc.once('close', common.mustCall((code, signal) => {
35+
clearTimeout(timer);
36+
if (!promptSeen) {
37+
reject(new Error(
38+
`Debugger exited before showing the prompt (code ${code}, signal ${signal}); ` +
39+
`output: ${output}`));
40+
return;
41+
}
42+
assert.strictEqual(code, 0);
43+
resolve();
44+
}));
45+
});
46+
})().then(common.mustCall());

0 commit comments

Comments
 (0)