From 9daffe6485d126da2b0b6cee60f2f59cc9e0a28d Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Thu, 19 Sep 2024 12:32:07 +0200 Subject: [PATCH 1/4] test: remove interval and give more time to unsync --- test/parallel/test-runner-watch-mode-complex.mjs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-runner-watch-mode-complex.mjs b/test/parallel/test-runner-watch-mode-complex.mjs index 0ec9a225c3501c..1d442fbbd9e077 100644 --- a/test/parallel/test-runner-watch-mode-complex.mjs +++ b/test/parallel/test-runner-watch-mode-complex.mjs @@ -76,15 +76,17 @@ describe('test runner watch mode with more complex setup', () => { runs.push(currentRun); currentRun = ''; const fileToDeletePathLocal = tmpdir.resolve('test-to-delete.mjs'); - unlinkSync(fileToDeletePathLocal); + await new Promise((resolve) => setTimeout(() => { + unlinkSync(fileToDeletePathLocal); + resolve(); + }, common.platformTimeout(1000))); const content = fixtureContent['dependency.mjs']; const path = fixturePaths['dependency.mjs']; - const interval = setInterval(() => writeFileSync(path, content), common.platformTimeout(1000)); + setTimeout(() => writeFileSync(path, content), common.platformTimeout(1000)); await ran2.promise; runs.push(currentRun); currentRun = ''; - clearInterval(interval); child.kill(); assert.strictEqual(runs.length, 2); From 48930c6c62fafaf839db694d51f35f57fd55eb48 Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Fri, 20 Sep 2024 18:17:33 +0200 Subject: [PATCH 2/4] feat: use node:fs/promises --- .../test-runner-watch-mode-complex.mjs | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/test/parallel/test-runner-watch-mode-complex.mjs b/test/parallel/test-runner-watch-mode-complex.mjs index 1d442fbbd9e077..eb57820db75345 100644 --- a/test/parallel/test-runner-watch-mode-complex.mjs +++ b/test/parallel/test-runner-watch-mode-complex.mjs @@ -3,7 +3,7 @@ import * as common from '../common/index.mjs'; import { describe, it } from 'node:test'; import assert from 'node:assert'; import { spawn } from 'node:child_process'; -import { writeFileSync, unlinkSync } from 'node:fs'; +import { writeFile, unlink } from 'node:fs/promises'; import util from 'internal/util'; import tmpdir from '../common/tmpdir.js'; @@ -15,6 +15,10 @@ if (common.isAIX) tmpdir.refresh(); +function wait(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + // This test updates these files repeatedly, // Reading them from disk is unreliable due to race conditions. const fixtureContent = { @@ -46,8 +50,12 @@ test('test to delete has ran');`, const fixturePaths = Object.fromEntries(Object.keys(fixtureContent) .map((file) => [file, tmpdir.resolve(file)])); -Object.entries(fixtureContent) - .forEach(([file, content]) => writeFileSync(fixturePaths[file], content)); +async function setupFixtures() { + await Promise.all(Object.entries(fixtureContent) + .map(([file, content]) => writeFile(fixturePaths[file], content))); +} + +await setupFixtures(); describe('test runner watch mode with more complex setup', () => { it('should run tests when a dependency changed after a watched test file being deleted', async () => { @@ -76,14 +84,13 @@ describe('test runner watch mode with more complex setup', () => { runs.push(currentRun); currentRun = ''; const fileToDeletePathLocal = tmpdir.resolve('test-to-delete.mjs'); - await new Promise((resolve) => setTimeout(() => { - unlinkSync(fileToDeletePathLocal); - resolve(); - }, common.platformTimeout(1000))); + await unlink(fileToDeletePathLocal); + await wait(common.platformTimeout(1000)); const content = fixtureContent['dependency.mjs']; const path = fixturePaths['dependency.mjs']; - setTimeout(() => writeFileSync(path, content), common.platformTimeout(1000)); + await writeFile(path, content); + await wait(common.platformTimeout(1000)); await ran2.promise; runs.push(currentRun); currentRun = ''; From 963ab6ca317786d23b81fbc88e9e3ad3c9e00663 Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Sat, 21 Sep 2024 20:53:23 +0200 Subject: [PATCH 3/4] test: apply suggestions --- test/parallel/test-runner-watch-mode-complex.mjs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/test/parallel/test-runner-watch-mode-complex.mjs b/test/parallel/test-runner-watch-mode-complex.mjs index eb57820db75345..9fc010a10c1abd 100644 --- a/test/parallel/test-runner-watch-mode-complex.mjs +++ b/test/parallel/test-runner-watch-mode-complex.mjs @@ -4,6 +4,7 @@ import { describe, it } from 'node:test'; import assert from 'node:assert'; import { spawn } from 'node:child_process'; import { writeFile, unlink } from 'node:fs/promises'; +import { setTimeout } from 'node:timers/promises'; import util from 'internal/util'; import tmpdir from '../common/tmpdir.js'; @@ -15,10 +16,6 @@ if (common.isAIX) tmpdir.refresh(); -function wait(ms) { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - // This test updates these files repeatedly, // Reading them from disk is unreliable due to race conditions. const fixtureContent = { @@ -51,7 +48,7 @@ const fixturePaths = Object.fromEntries(Object.keys(fixtureContent) .map((file) => [file, tmpdir.resolve(file)])); async function setupFixtures() { - await Promise.all(Object.entries(fixtureContent) + return Promise.all(Object.entries(fixtureContent) .map(([file, content]) => writeFile(fixturePaths[file], content))); } @@ -85,12 +82,12 @@ describe('test runner watch mode with more complex setup', () => { currentRun = ''; const fileToDeletePathLocal = tmpdir.resolve('test-to-delete.mjs'); await unlink(fileToDeletePathLocal); - await wait(common.platformTimeout(1000)); + await setTimeout(common.platformTimeout(1000)); const content = fixtureContent['dependency.mjs']; const path = fixturePaths['dependency.mjs']; await writeFile(path, content); - await wait(common.platformTimeout(1000)); + await setTimeout(common.platformTimeout(1000)); await ran2.promise; runs.push(currentRun); currentRun = ''; From e654e80a7d4071e7b29e39fc5f0c70d83e6c7fa1 Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Tue, 24 Sep 2024 09:01:56 +0200 Subject: [PATCH 4/4] test: set flush option --- test/parallel/test-runner-watch-mode-complex.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-runner-watch-mode-complex.mjs b/test/parallel/test-runner-watch-mode-complex.mjs index 9fc010a10c1abd..bd33be61c7d058 100644 --- a/test/parallel/test-runner-watch-mode-complex.mjs +++ b/test/parallel/test-runner-watch-mode-complex.mjs @@ -49,7 +49,7 @@ const fixturePaths = Object.fromEntries(Object.keys(fixtureContent) async function setupFixtures() { return Promise.all(Object.entries(fixtureContent) - .map(([file, content]) => writeFile(fixturePaths[file], content))); + .map(([file, content]) => writeFile(fixturePaths[file], content, { flush: true }))); } await setupFixtures(); @@ -86,7 +86,7 @@ describe('test runner watch mode with more complex setup', () => { const content = fixtureContent['dependency.mjs']; const path = fixturePaths['dependency.mjs']; - await writeFile(path, content); + await writeFile(path, content, { flush: true }); await setTimeout(common.platformTimeout(1000)); await ran2.promise; runs.push(currentRun);