• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Tests that calling unref() on Http2Session:
3// (1) Prevents it from keeping the process alive
4// (2) Doesn't crash
5
6const common = require('../common');
7if (!common.hasCrypto)
8  common.skip('missing crypto');
9const http2 = require('http2');
10const Countdown = require('../common/countdown');
11const makeDuplexPair = require('../common/duplexpair');
12
13const server = http2.createServer();
14const { clientSide, serverSide } = makeDuplexPair();
15
16const counter = new Countdown(3, () => server.unref());
17
18// 'session' event should be emitted 3 times:
19// - the vanilla client
20// - the destroyed client
21// - manual 'connection' event emission with generic Duplex stream
22server.on('session', common.mustCallAtLeast((session) => {
23  counter.dec();
24  session.unref();
25}, 3));
26
27server.listen(0, common.mustCall(() => {
28  const port = server.address().port;
29
30  // unref new client
31  {
32    const client = http2.connect(`http://localhost:${port}`);
33    client.unref();
34  }
35
36  // Unref destroyed client
37  {
38    const client = http2.connect(`http://localhost:${port}`);
39
40    client.on('connect', common.mustCall(() => {
41      client.destroy();
42      client.unref();
43    }));
44  }
45
46  // Unref destroyed client
47  {
48    const client = http2.connect(`http://localhost:${port}`, {
49      createConnection: common.mustCall(() => clientSide)
50    });
51
52    client.on('connect', common.mustCall(() => {
53      client.destroy();
54      client.unref();
55    }));
56  }
57}));
58server.emit('connection', serverSide);
59