From 2b3b291c97c485731f2f9ee82ee4d79423551cce Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Sat, 22 Oct 2022 21:02:09 +0300 Subject: [PATCH] test: watch mode inspect restart repeatedly PR-URL: https://github.com/nodejs/node/pull/45060 Fixes: https://github.com/nodejs/node/issues/44805 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell --- test/sequential/test-watch-mode-inspect.mjs | 22 ++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/test/sequential/test-watch-mode-inspect.mjs b/test/sequential/test-watch-mode-inspect.mjs index 8981c8c24ca243..211c71ced15288 100644 --- a/test/sequential/test-watch-mode-inspect.mjs +++ b/test/sequential/test-watch-mode-inspect.mjs @@ -3,6 +3,7 @@ import * as fixtures from '../common/fixtures.mjs'; import assert from 'node:assert'; import { describe, it } from 'node:test'; import { writeFileSync, readFileSync } from 'node:fs'; +import { setTimeout } from 'node:timers/promises'; import { NodeInstance } from '../common/inspector-helper.js'; @@ -11,6 +12,12 @@ if (common.isIBMi) common.skipIfInspectorDisabled(); +function restart(file) { + writeFileSync(file, readFileSync(file)); + const interval = setInterval(() => writeFileSync(file, readFileSync(file)), 500); + return () => clearInterval(interval); +} + describe('watch mode - inspect', () => { async function getDebuggedPid(instance, waitForLog = true) { const session = await instance.connectInspectorSession(); @@ -29,20 +36,25 @@ describe('watch mode - inspect', () => { const file = fixtures.path('watch-mode/inspect.js'); const instance = new NodeInstance(['--inspect=0', '--watch'], undefined, file); let stderr = ''; + const stdout = []; instance.on('stderr', (data) => { stderr += data; }); + instance.on('stdout', (data) => { stdout.push(data); }); const pids = [instance.pid]; pids.push(await getDebuggedPid(instance)); instance.resetPort(); - writeFileSync(file, readFileSync(file)); + const stopRestarting = restart(file); pids.push(await getDebuggedPid(instance)); + stopRestarting(); + await setTimeout(common.platformTimeout(500)); await instance.kill(); - // There should be 3 pids (one parent + 2 restarts). - // Message about Debugger should only appear twice. - assert.strictEqual(stderr.match(/Debugger listening on ws:\/\//g).length, 2); - assert.strictEqual(new Set(pids).size, 3); + // There should be a process per restart and one per parent process. + // Message about Debugger should appear once per restart. + const restarts = stdout.filter((line) => line === 'safe to debug now').length; + assert.strictEqual(stderr.match(/Debugger listening on ws:\/\//g).length, restarts); + assert.strictEqual(new Set(pids).size, restarts + 1); }); it('should prevent attaching debugger with SIGUSR1 to outer process', { skip: common.isWindows }, async () => {