Skip to content

Commit

Permalink
test: fix flaky test-worker-message-port-transfer-self
Browse files Browse the repository at this point in the history
Look at the status of the `MessagePort` rather than
relying on a timeout.

PR-URL: #22658
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
addaleax authored and targos committed Sep 5, 2018
1 parent 1daa82a commit c26747d
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions test/parallel/test-worker-message-port-transfer-self.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const common = require('../common');
const assert = require('assert');
const util = require('util');
const { MessageChannel } = require('worker_threads');

const { port1, port2 } = new MessageChannel();
Expand All @@ -25,9 +26,22 @@ assert.throws(common.mustCall(() => {
// The failed transfer should not affect the ports in anyway.
port2.onmessage = common.mustCall((message) => {
assert.strictEqual(message, 2);

assert(util.inspect(port1).includes('active: true'), util.inspect(port1));
assert(util.inspect(port2).includes('active: true'), util.inspect(port2));

port1.close();

setTimeout(common.mustNotCall('The communication channel is still open'),
common.platformTimeout(1000)).unref();
tick(10, () => {
assert(util.inspect(port1).includes('active: false'), util.inspect(port1));
assert(util.inspect(port2).includes('active: false'), util.inspect(port2));
});
});
port1.postMessage(2);

function tick(n, cb) {
if (n > 0)
setImmediate(() => tick(n - 1, cb));
else
cb();
}

0 comments on commit c26747d

Please sign in to comment.