summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-client-destroy.js
blob: 56cfec5d65a223df3a55ff5503353082c90ce61d (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
// Flags: --expose-http2
'use strict';

const common = require('../common');
const assert = require('assert');
const h2 = require('http2');

const server = h2.createServer();
server.listen(0);

server.on('listening', common.mustCall(function() {
  const port = this.address().port;

  const destroyCallbacks = [
    (client) => client.destroy(),
    (client) => client.socket.destroy()
  ];

  let remaining = destroyCallbacks.length;

  destroyCallbacks.forEach((destroyCallback) => {
    const client = h2.connect(`http://localhost:${port}`);
    client.on('connect', common.mustCall(() => {
      const socket = client.socket;

      assert(client.socket, 'client session has associated socket');
      assert(!client.destroyed,
             'client has not been destroyed before destroy is called');
      assert(!socket.destroyed,
             'socket has not been destroyed before destroy is called');

      // Ensure that 'close' event is emitted
      client.on('close', common.mustCall());

      destroyCallback(client);

      assert(!client.socket, 'client.socket undefined after destroy is called');

      // Must must be closed
      client.on('close', common.mustCall(() => {
        assert(client.destroyed);
      }));

      // socket will close on process.nextTick
      socket.on('close', common.mustCall(() => {
        assert(socket.destroyed);
      }));

      if (--remaining === 0) {
        server.close();
      }
    }));
  });
}));