summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-agent-error-on-idle.js
blob: 8edfa24811417a5f041af0d98c2bf2b402911578 (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
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');
const Agent = http.Agent;

const server = http.createServer(common.mustCall((req, res) => {
  res.end('hello world');
}, 2));

server.listen(0, () => {
  const agent = new Agent({ keepAlive: true });

  const requestParams = {
    host: 'localhost',
    port: server.address().port,
    agent: agent,
    path: '/'
  };

  const socketKey = agent.getName(requestParams);

  http.get(requestParams, common.mustCall((res) => {
    assert.strictEqual(res.statusCode, 200);
    res.resume();
    res.on('end', common.mustCall(() => {
      process.nextTick(common.mustCall(() => {
        const freeSockets = agent.freeSockets[socketKey];
        // Expect a free socket on socketKey
        assert.strictEqual(freeSockets.length, 1);

        // Generate a random error on the free socket
        const freeSocket = freeSockets[0];
        freeSocket.emit('error', new Error('ECONNRESET: test'));

        http.get(requestParams, done);
      }));
    }));
  }));

  function done() {
    // Expect the freeSockets pool to be empty
    assert.strictEqual(Object.keys(agent.freeSockets).length, 0);

    agent.destroy();
    server.close();
  }
});