From f47e8fccbb06c35f356f4d4552e7bb1c1984bf18 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Sat, 11 May 2024 20:05:59 +0200 Subject: [PATCH] test: fix DNS cancel tests PR-URL: https://github.com/nodejs/node/pull/44432 Reviewed-By: Joyee Cheung Reviewed-By: Luigi Pinca --- .../test-dns-channel-cancel-promise.js | 22 +++++++------------ test/parallel/test-dns-channel-cancel.js | 19 ++++++---------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/test/parallel/test-dns-channel-cancel-promise.js b/test/parallel/test-dns-channel-cancel-promise.js index 382ac3dd508d68..6dee3e6a778687 100644 --- a/test/parallel/test-dns-channel-cancel-promise.js +++ b/test/parallel/test-dns-channel-cancel-promise.js @@ -7,23 +7,15 @@ const dgram = require('dgram'); const server = dgram.createSocket('udp4'); const resolver = new dnsPromises.Resolver(); -const addMessageListener = () => { - server.removeAllListeners('message'); - - server.once('message', () => { - server.once('message', common.mustNotCall); - - resolver.cancel(); - }); -}; - server.bind(0, common.mustCall(async () => { resolver.setServers([`127.0.0.1:${server.address().port}`]); - addMessageListener(); - // Single promise { + server.once('message', () => { + resolver.cancel(); + }); + const hostname = 'example0.org'; await assert.rejects( @@ -36,10 +28,12 @@ server.bind(0, common.mustCall(async () => { ); } - addMessageListener(); - // Multiple promises { + server.once('message', () => { + resolver.cancel(); + }); + const assertions = []; const assertionCount = 10; diff --git a/test/parallel/test-dns-channel-cancel.js b/test/parallel/test-dns-channel-cancel.js index f92fb2e30a4d12..405b31e4cc1913 100644 --- a/test/parallel/test-dns-channel-cancel.js +++ b/test/parallel/test-dns-channel-cancel.js @@ -10,16 +10,6 @@ const resolver = new Resolver(); const desiredQueries = 11; let finishedQueries = 0; -const addMessageListener = () => { - server.removeAllListeners('message'); - - server.once('message', () => { - server.once('message', common.mustNotCall); - - resolver.cancel(); - }); -}; - server.bind(0, common.mustCall(async () => { resolver.setServers([`127.0.0.1:${server.address().port}`]); @@ -37,7 +27,9 @@ server.bind(0, common.mustCall(async () => { const next = (...args) => { callback(...args); - addMessageListener(); + server.once('message', () => { + resolver.cancel(); + }); // Multiple queries for (let i = 1; i < desiredQueries; i++) { @@ -45,7 +37,10 @@ server.bind(0, common.mustCall(async () => { } }; + server.once('message', () => { + resolver.cancel(); + }); + // Single query - addMessageListener(); resolver.resolve4('example0.org', next); }));