From cf02160cbdbf60ec8292b75bb5ca012ab2068b56 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 20 Aug 2023 06:50:06 +0200 Subject: [PATCH] test: reduce flakiness of `test-esm-loader-hooks` PR-URL: https://github.com/nodejs/node/pull/49248 Reviewed-By: Moshe Atlow Reviewed-By: Jacob Smith Reviewed-By: Geoffrey Booth Reviewed-By: Yagiz Nizipli --- test/es-module/test-esm-loader-hooks.mjs | 16 ++++++++++------ .../es-module-loaders/hooks-initialize.mjs | 5 +++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/test/es-module/test-esm-loader-hooks.mjs b/test/es-module/test-esm-loader-hooks.mjs index 8d7d885164ae48..046e3d2e663fb1 100644 --- a/test/es-module/test-esm-loader-hooks.mjs +++ b/test/es-module/test-esm-loader-hooks.mjs @@ -547,7 +547,7 @@ describe('Loader hooks', { concurrency: true }, () => { ` import {MessageChannel} from 'node:worker_threads'; import {register} from 'node:module'; - import {setTimeout} from 'node:timers/promises'; + import {once} from 'node:events'; const {port1, port2} = new MessageChannel(); port1.on('message', (msg) => { console.log('message', msg); @@ -558,8 +558,12 @@ describe('Loader hooks', { concurrency: true }, () => { ); console.log('register', result); - await import('node:os'); - await setTimeout(99); // delay to limit flakiness + const timeout = setTimeout(() => {}, 2**31 - 1); // to keep the process alive. + await Promise.all([ + once(port1, 'message').then(() => once(port1, 'message')), + import('node:os'), + ]); + clearTimeout(timeout); port1.close(); `, ]); @@ -655,10 +659,10 @@ describe('Loader hooks', { concurrency: true }, () => { ]); assert.strictEqual(stderr, ''); - assert.deepStrictEqual(stdout.split('\n'), [ 'result 1', - 'result 2', - 'hooks initialize 1', + assert.deepStrictEqual(stdout.split('\n'), [ 'hooks initialize 1', + 'result 1', 'hooks initialize 2', + 'result 2', '' ]); assert.strictEqual(code, 0); assert.strictEqual(signal, null); diff --git a/test/fixtures/es-module-loaders/hooks-initialize.mjs b/test/fixtures/es-module-loaders/hooks-initialize.mjs index 646be145503134..ab6f2c50d146e3 100644 --- a/test/fixtures/es-module-loaders/hooks-initialize.mjs +++ b/test/fixtures/es-module-loaders/hooks-initialize.mjs @@ -1,7 +1,8 @@ +import { writeFileSync } from 'node:fs'; + let counter = 0; export async function initialize() { - counter += 1; - console.log('hooks initialize', counter); + writeFileSync(1, `hooks initialize ${++counter}\n`); return counter; }