From d121f76fdb130ce07acbea67c044a3392c70af2b Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Wed, 23 Apr 2025 21:53:09 -0700 Subject: [PATCH] one more --- .../test/parallel/test-http2-session-unref.js | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 test/js/node/test/parallel/test-http2-session-unref.js diff --git a/test/js/node/test/parallel/test-http2-session-unref.js b/test/js/node/test/parallel/test-http2-session-unref.js new file mode 100644 index 0000000000..a5596e37c2 --- /dev/null +++ b/test/js/node/test/parallel/test-http2-session-unref.js @@ -0,0 +1,58 @@ +'use strict'; +// Tests that calling unref() on Http2Session: +// (1) Prevents it from keeping the process alive +// (2) Doesn't crash + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); +const http2 = require('http2'); +const Countdown = require('../common/countdown'); +const { duplexPair } = require('stream'); + +const server = http2.createServer(); +const [ clientSide, serverSide ] = duplexPair(); + +const counter = new Countdown(3, () => server.unref()); + +// 'session' event should be emitted 3 times: +// - the vanilla clientgit +// - the destroyed client +// - manual 'connection' event emission with generic Duplex stream +server.on('session', common.mustCallAtLeast((session) => { + counter.dec(); + session.unref(); +}, 3)); + +server.listen(0, common.mustCall(() => { + const port = server.address().port; + + // unref new client + { + const client = http2.connect(`http://localhost:${port}`); + client.unref(); + } + + // Unref destroyed client + { + const client = http2.connect(`http://localhost:${port}`); + + client.on('connect', common.mustCall(() => { + client.destroy(); + client.unref(); + })); + } + + // Unref destroyed client + { + const client = http2.connect(`http://localhost:${port}`, { + createConnection: common.mustCall(() => clientSide) + }); + + client.on('connect', common.mustCall(() => { + client.destroy(); + client.unref(); + })); + } +})); +server.emit('connection', serverSide);