summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-client-abort-keep-alive-queued-tcp-socket.js
blob: c9614f01c3db3fdad14233514334846c3b330380 (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
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');


for (const destroyer of ['destroy', 'abort']) {
  let socketsCreated = 0;

  class Agent extends http.Agent {
    createConnection(options, oncreate) {
      const socket = super.createConnection(options, oncreate);
      socketsCreated++;
      return socket;
    }
  }

  const server = http.createServer((req, res) => res.end());

  server.listen(0, common.mustCall(() => {
    const port = server.address().port;
    const agent = new Agent({
      keepAlive: true,
      maxSockets: 1
    });

    http.get({ agent, port }, (res) => res.resume());

    const req = http.get({ agent, port }, common.mustNotCall());
    req[destroyer]();

    if (destroyer === 'destroy') {
      req.on('error', common.mustCall((err) => {
        assert.strictEqual(err.code, 'ECONNRESET');
      }));
    } else {
      req.on('error', common.mustNotCall());
    }

    http.get({ agent, port }, common.mustCall((res) => {
      res.resume();
      assert.strictEqual(socketsCreated, 1);
      agent.destroy();
      server.close();
    }));
  }));
}