diff --git a/test/async-hooks/test-crypto-pbkdf2.js b/test/async-hooks/test-crypto-pbkdf2.js index ed7270d191e903..4788ce4a580656 100644 --- a/test/async-hooks/test-crypto-pbkdf2.js +++ b/test/async-hooks/test-crypto-pbkdf2.js @@ -7,7 +7,7 @@ if (!common.isMainThread) common.skip('Worker bootstrapping works differently -> different async IDs'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const crypto = require('crypto'); diff --git a/test/async-hooks/test-crypto-randomBytes.js b/test/async-hooks/test-crypto-randomBytes.js index 67f5221a5e0b30..88cd4643ab6638 100644 --- a/test/async-hooks/test-crypto-randomBytes.js +++ b/test/async-hooks/test-crypto-randomBytes.js @@ -7,7 +7,7 @@ if (!common.isMainThread) common.skip('Worker bootstrapping works differently -> different async IDs'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const crypto = require('crypto'); diff --git a/test/async-hooks/test-embedder.api.async-resource.js b/test/async-hooks/test-embedder.api.async-resource.js index 89a889b20192e3..6f71a3e98cbf0f 100644 --- a/test/async-hooks/test-embedder.api.async-resource.js +++ b/test/async-hooks/test-embedder.api.async-resource.js @@ -2,7 +2,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const async_hooks = require('async_hooks'); const { AsyncResource } = async_hooks; diff --git a/test/async-hooks/test-enable-disable.js b/test/async-hooks/test-enable-disable.js index df093da608c29c..c14a125688d0f4 100644 --- a/test/async-hooks/test-enable-disable.js +++ b/test/async-hooks/test-enable-disable.js @@ -85,7 +85,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); diff --git a/test/async-hooks/test-fseventwrap.js b/test/async-hooks/test-fseventwrap.js index 56ada05b760eb6..8608075c822ded 100644 --- a/test/async-hooks/test-fseventwrap.js +++ b/test/async-hooks/test-fseventwrap.js @@ -3,7 +3,7 @@ const common = require('../common'); const assert = require('assert'); const initHooks = require('./init-hooks'); -const tick = require('./tick'); +const tick = require('../common/tick'); const { checkInvocations } = require('./hook-checks'); const fs = require('fs'); diff --git a/test/async-hooks/test-fsreqcallback-access.js b/test/async-hooks/test-fsreqcallback-access.js index 8983a3dcdf243f..b1b75f5693050d 100644 --- a/test/async-hooks/test-fsreqcallback-access.js +++ b/test/async-hooks/test-fsreqcallback-access.js @@ -2,7 +2,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const fs = require('fs'); diff --git a/test/async-hooks/test-fsreqcallback-readFile.js b/test/async-hooks/test-fsreqcallback-readFile.js index 8301835ec8153f..3ae31378cd3199 100644 --- a/test/async-hooks/test-fsreqcallback-readFile.js +++ b/test/async-hooks/test-fsreqcallback-readFile.js @@ -2,7 +2,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const fs = require('fs'); diff --git a/test/async-hooks/test-getaddrinforeqwrap.js b/test/async-hooks/test-getaddrinforeqwrap.js index 298c4931cce74e..0cd6ff39676424 100644 --- a/test/async-hooks/test-getaddrinforeqwrap.js +++ b/test/async-hooks/test-getaddrinforeqwrap.js @@ -2,7 +2,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const dns = require('dns'); diff --git a/test/async-hooks/test-getnameinforeqwrap.js b/test/async-hooks/test-getnameinforeqwrap.js index a32814e5b5df4a..50aa623458f41c 100644 --- a/test/async-hooks/test-getnameinforeqwrap.js +++ b/test/async-hooks/test-getnameinforeqwrap.js @@ -2,7 +2,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const dns = require('dns'); diff --git a/test/async-hooks/test-httpparser.request.js b/test/async-hooks/test-httpparser.request.js index 71fccc0a858921..cb56419137163a 100644 --- a/test/async-hooks/test-httpparser.request.js +++ b/test/async-hooks/test-httpparser.request.js @@ -3,7 +3,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); diff --git a/test/async-hooks/test-httpparser.response.js b/test/async-hooks/test-httpparser.response.js index eea40b14d16b68..b1f804ef7d42a9 100644 --- a/test/async-hooks/test-httpparser.response.js +++ b/test/async-hooks/test-httpparser.response.js @@ -3,7 +3,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); diff --git a/test/async-hooks/test-immediate.js b/test/async-hooks/test-immediate.js index 3ef782eb5fa66c..7b51902f77cbeb 100644 --- a/test/async-hooks/test-immediate.js +++ b/test/async-hooks/test-immediate.js @@ -2,7 +2,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); diff --git a/test/async-hooks/test-pipeconnectwrap.js b/test/async-hooks/test-pipeconnectwrap.js index df4b8110e67ec4..6c68186e2f239f 100644 --- a/test/async-hooks/test-pipeconnectwrap.js +++ b/test/async-hooks/test-pipeconnectwrap.js @@ -2,7 +2,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); diff --git a/test/async-hooks/test-pipewrap.js b/test/async-hooks/test-pipewrap.js index f2b5bf985225b9..f04f900606a3de 100644 --- a/test/async-hooks/test-pipewrap.js +++ b/test/async-hooks/test-pipewrap.js @@ -5,7 +5,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const spawn = require('child_process').spawn; diff --git a/test/async-hooks/test-querywrap.js b/test/async-hooks/test-querywrap.js index 7d71a183130881..f6e7e12e81e9c0 100644 --- a/test/async-hooks/test-querywrap.js +++ b/test/async-hooks/test-querywrap.js @@ -3,7 +3,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const dns = require('dns'); diff --git a/test/async-hooks/test-tcpwrap.js b/test/async-hooks/test-tcpwrap.js index f3100aba0f0aaa..a01a1fc4fe21d4 100644 --- a/test/async-hooks/test-tcpwrap.js +++ b/test/async-hooks/test-tcpwrap.js @@ -6,7 +6,7 @@ if (!common.hasIPv6) common.skip('IPv6 support required'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const net = require('net'); diff --git a/test/async-hooks/test-timers.setTimeout.js b/test/async-hooks/test-timers.setTimeout.js index 8f1d3222ddf662..748fa5565eee64 100644 --- a/test/async-hooks/test-timers.setTimeout.js +++ b/test/async-hooks/test-timers.setTimeout.js @@ -2,7 +2,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const TIMEOUT = common.platformTimeout(100); diff --git a/test/async-hooks/test-tlswrap.js b/test/async-hooks/test-tlswrap.js index 79cf1ab689e997..354cd7ad0cd91d 100644 --- a/test/async-hooks/test-tlswrap.js +++ b/test/async-hooks/test-tlswrap.js @@ -8,7 +8,7 @@ const assert = require('assert'); const fixtures = require('../common/fixtures'); const tls = require('tls'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); diff --git a/test/async-hooks/test-ttywrap.readstream.js b/test/async-hooks/test-ttywrap.readstream.js index 65853da1f7ab17..bf81a3266c1fb6 100644 --- a/test/async-hooks/test-ttywrap.readstream.js +++ b/test/async-hooks/test-ttywrap.readstream.js @@ -4,7 +4,7 @@ const common = require('../common'); const assert = require('assert'); // general hook test setup -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); diff --git a/test/async-hooks/test-ttywrap.writestream.js b/test/async-hooks/test-ttywrap.writestream.js index d8a453e7be2700..44f32e80f015c2 100644 --- a/test/async-hooks/test-ttywrap.writestream.js +++ b/test/async-hooks/test-ttywrap.writestream.js @@ -4,7 +4,7 @@ const common = require('../common'); const assert = require('assert'); // general hook test setup -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); diff --git a/test/async-hooks/test-udpwrap.js b/test/async-hooks/test-udpwrap.js index 5770ac060edce8..0a0e03c5d5148d 100644 --- a/test/async-hooks/test-udpwrap.js +++ b/test/async-hooks/test-udpwrap.js @@ -2,7 +2,7 @@ const common = require('../common'); const assert = require('assert'); -const tick = require('./tick'); +const tick = require('../common/tick'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); const dgram = require('dgram'); diff --git a/test/common/README.md b/test/common/README.md index 63b7905113f577..afc267ba7cb16c 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -13,6 +13,7 @@ This directory contains modules used to test the Node.js implementation. * [Heap dump checker module](#heap-dump-checker-module) * [HTTP2 module](#http2-module) * [Internet module](#internet-module) +* [tick module](#tick-module) * [tmpdir module](#tmpdir-module) * [WPT module](#wpt-module) @@ -744,6 +745,16 @@ a full `setImmediate()` invocation passes. should not be in scope when `listener.ongc()` is created. +## tick Module + +The `tick` module provides a helper function that can be used to call a callback +after a given number of event loop "ticks". + +### tick(x, cb) + +* `x` [<number>] Number of event loop "ticks". +* `cb` [<Function>] A callback function. + ## tmpdir Module The `tmpdir` module supports the use of a temporary directory for testing. diff --git a/test/async-hooks/tick.js b/test/common/tick.js similarity index 100% rename from test/async-hooks/tick.js rename to test/common/tick.js diff --git a/test/parallel/test-http2-session-gc-while-write-scheduled.js b/test/parallel/test-http2-session-gc-while-write-scheduled.js index bb23760cebf967..aef9d9571d5559 100644 --- a/test/parallel/test-http2-session-gc-while-write-scheduled.js +++ b/test/parallel/test-http2-session-gc-while-write-scheduled.js @@ -6,6 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const http2 = require('http2'); const makeDuplexPair = require('../common/duplexpair'); +const tick = require('../common/tick'); // This tests that running garbage collection while an Http2Session has // a write *scheduled*, it will survive that garbage collection. @@ -25,8 +26,3 @@ const makeDuplexPair = require('../common/duplexpair'); global.gc(); }); } - -function tick(n, cb) { - if (n--) setImmediate(tick, n, cb); - else cb(); -} diff --git a/test/parallel/test-worker-message-port-transfer-self.js b/test/parallel/test-worker-message-port-transfer-self.js index c6f29163dfc643..6b8addf0229eac 100644 --- a/test/parallel/test-worker-message-port-transfer-self.js +++ b/test/parallel/test-worker-message-port-transfer-self.js @@ -5,6 +5,7 @@ const common = require('../common'); const assert = require('assert'); const util = require('util'); const { MessageChannel } = require('worker_threads'); +const tick = require('../common/tick'); const { port1, port2 } = new MessageChannel(); @@ -42,10 +43,3 @@ port2.onmessage = common.mustCall((message) => { }); }); port1.postMessage(2); - -function tick(n, cb) { - if (n > 0) - setImmediate(() => tick(n - 1, cb)); - else - cb(); -}