summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-session-unref.js
blob: dcfbccf215aec8d9ac81fd9a4b1fe93f1e12ccf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
// Flags: --expose-internals

// 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 makeDuplexPair = require('../common/duplexpair');

const server = http2.createServer();
const { clientSide, serverSide } = makeDuplexPair();

const counter = new Countdown(3, () => server.unref());

// 'session' event should be emitted 3 times:
// - the vanilla client
// - 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);