From 52a27e219ffd7958df3e76dcda41d06a77ca7a26 Mon Sep 17 00:00:00 2001 From: Sergey Chernyshev Date: Thu, 25 Apr 2024 04:18:43 +0200 Subject: [PATCH] test: deflake `test-esm-loader-resolve-type` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/50273 Backport-PR-URL: https://github.com/nodejs/node/pull/50669 Fixes: https://github.com/nodejs/node/issues/50040 Reviewed-By: Michaƫl Zasso Reviewed-By: Michael Dawson Reviewed-By: Marco Ippolito Reviewed-By: Richard Lau --- .../hook-resolve-type-loader.mjs | 19 +++++++---- .../es-module-loaders/hook-resolve-type.mjs | 32 ++++++------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs b/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs index f2dc0aba5ca..c410401876e 100644 --- a/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs +++ b/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs @@ -1,7 +1,14 @@ -/** @type {MessagePort} */ -let port; -export function initialize(data) { - port = data.port; +/** @type {Uint8Array} */ +let data; +/** @type {number} */ +let ESM_MODULE_INDEX; +/** @type {number} */ +let CJS_MODULE_INDEX; + +export function initialize({ sab, ESM_MODULE_INDEX:e, CJS_MODULE_INDEX:c }) { + data = new Uint8Array(sab); + ESM_MODULE_INDEX = e; + CJS_MODULE_INDEX = c; } export async function resolve(specifier, context, next) { @@ -9,9 +16,9 @@ export async function resolve(specifier, context, next) { const { format } = nextResult; if (format === 'module' || specifier.endsWith('.mjs')) { - port.postMessage({ type: 'module' }); + Atomics.add(data, ESM_MODULE_INDEX, 1); } else if (format == null || format === 'commonjs') { - port.postMessage({ type: 'commonjs' }); + Atomics.add(data, CJS_MODULE_INDEX, 1); } return nextResult; diff --git a/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type.mjs b/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type.mjs index 6fa7a553b8e..7324a08e84b 100644 --- a/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type.mjs +++ b/graal-nodejs/test/fixtures/es-module-loaders/hook-resolve-type.mjs @@ -1,30 +1,18 @@ import * as fixtures from '../../common/fixtures.mjs'; import { register } from 'node:module'; -import { MessageChannel } from 'node:worker_threads'; -let importedESM = 0; -let importedCJS = 0; +const sab = new SharedArrayBuffer(2); +const data = new Uint8Array(sab); + +const ESM_MODULE_INDEX = 0 +const CJS_MODULE_INDEX = 1 + export function getModuleTypeStats() { + const importedESM = Atomics.load(data, ESM_MODULE_INDEX); + const importedCJS = Atomics.load(data, CJS_MODULE_INDEX); return { importedESM, importedCJS }; -}; - -const { port1, port2 } = new MessageChannel(); +} register(fixtures.fileURL('es-module-loaders/hook-resolve-type-loader.mjs'), { - data: { port: port2 }, - transferList: [port2], + data: { sab, ESM_MODULE_INDEX, CJS_MODULE_INDEX }, }); - -port1.on('message', ({ type }) => { - switch (type) { - case 'module': - importedESM++; - break; - case 'commonjs': - importedCJS++; - break; - } -}); - -port1.unref(); -port2.unref();