11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Tests that calling unref() on Http2Session:
31cb0ef41Sopenharmony_ci// (1) Prevents it from keeping the process alive
41cb0ef41Sopenharmony_ci// (2) Doesn't crash
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ciif (!common.hasCrypto)
81cb0ef41Sopenharmony_ci  common.skip('missing crypto');
91cb0ef41Sopenharmony_ciconst http2 = require('http2');
101cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
111cb0ef41Sopenharmony_ciconst makeDuplexPair = require('../common/duplexpair');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst server = http2.createServer();
141cb0ef41Sopenharmony_ciconst { clientSide, serverSide } = makeDuplexPair();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst counter = new Countdown(3, () => server.unref());
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// 'session' event should be emitted 3 times:
191cb0ef41Sopenharmony_ci// - the vanilla client
201cb0ef41Sopenharmony_ci// - the destroyed client
211cb0ef41Sopenharmony_ci// - manual 'connection' event emission with generic Duplex stream
221cb0ef41Sopenharmony_ciserver.on('session', common.mustCallAtLeast((session) => {
231cb0ef41Sopenharmony_ci  counter.dec();
241cb0ef41Sopenharmony_ci  session.unref();
251cb0ef41Sopenharmony_ci}, 3));
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
281cb0ef41Sopenharmony_ci  const port = server.address().port;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  // unref new client
311cb0ef41Sopenharmony_ci  {
321cb0ef41Sopenharmony_ci    const client = http2.connect(`http://localhost:${port}`);
331cb0ef41Sopenharmony_ci    client.unref();
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  // Unref destroyed client
371cb0ef41Sopenharmony_ci  {
381cb0ef41Sopenharmony_ci    const client = http2.connect(`http://localhost:${port}`);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    client.on('connect', common.mustCall(() => {
411cb0ef41Sopenharmony_ci      client.destroy();
421cb0ef41Sopenharmony_ci      client.unref();
431cb0ef41Sopenharmony_ci    }));
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  // Unref destroyed client
471cb0ef41Sopenharmony_ci  {
481cb0ef41Sopenharmony_ci    const client = http2.connect(`http://localhost:${port}`, {
491cb0ef41Sopenharmony_ci      createConnection: common.mustCall(() => clientSide)
501cb0ef41Sopenharmony_ci    });
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci    client.on('connect', common.mustCall(() => {
531cb0ef41Sopenharmony_ci      client.destroy();
541cb0ef41Sopenharmony_ci      client.unref();
551cb0ef41Sopenharmony_ci    }));
561cb0ef41Sopenharmony_ci  }
571cb0ef41Sopenharmony_ci}));
581cb0ef41Sopenharmony_ciserver.emit('connection', serverSide);
59