• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const http = require('http');
6
7{
8  // Ensure reuse of successful sockets.
9
10  const agent = new http.Agent({ keepAlive: true });
11
12  const server = http.createServer((req, res) => {
13    res.end();
14  });
15
16  server.listen(0, common.mustCall(() => {
17    let socket;
18    http.get({ port: server.address().port, agent })
19      .on('response', common.mustCall((res) => {
20        socket = res.socket;
21        assert(socket);
22        res.resume();
23        socket.on('free', common.mustCall(() => {
24          http.get({ port: server.address().port, agent })
25            .on('response', common.mustCall((res) => {
26              assert.strictEqual(socket, res.socket);
27              assert(socket);
28              agent.destroy();
29              server.close();
30            }));
31        }));
32      }));
33  }));
34}
35
36{
37  // Ensure that timeouted sockets are not reused.
38
39  const agent = new http.Agent({ keepAlive: true, timeout: 50 });
40
41  const server = http.createServer((req, res) => {
42    res.end();
43  });
44
45  server.listen(0, common.mustCall(() => {
46    http.get({ port: server.address().port, agent })
47      .on('response', common.mustCall((res) => {
48        const socket = res.socket;
49        assert(socket);
50        res.resume();
51        socket.on('free', common.mustCall(() => {
52          socket.on('timeout', common.mustCall(() => {
53            http.get({ port: server.address().port, agent })
54              .on('response', common.mustCall((res) => {
55                assert.notStrictEqual(socket, res.socket);
56                assert.strictEqual(socket.destroyed, true);
57                agent.destroy();
58                server.close();
59              }));
60          }));
61        }));
62      }));
63  }));
64}
65
66{
67  // Ensure that destroyed sockets are not reused.
68
69  const agent = new http.Agent({ keepAlive: true });
70
71  const server = http.createServer((req, res) => {
72    res.end();
73  });
74
75  server.listen(0, common.mustCall(() => {
76    let socket;
77    http.get({ port: server.address().port, agent })
78      .on('response', common.mustCall((res) => {
79        socket = res.socket;
80        assert(socket);
81        res.resume();
82        socket.on('free', common.mustCall(() => {
83          socket.destroy();
84          http.get({ port: server.address().port, agent })
85            .on('response', common.mustCall((res) => {
86              assert.notStrictEqual(socket, res.socket);
87              assert(socket);
88              agent.destroy();
89              server.close();
90            }));
91        }));
92      }));
93  }));
94}
95
96{
97  // Ensure custom keepSocketAlive timeout is respected
98
99  const CUSTOM_TIMEOUT = 60;
100  const AGENT_TIMEOUT = 50;
101
102  class CustomAgent extends http.Agent {
103    keepSocketAlive(socket) {
104      if (!super.keepSocketAlive(socket)) {
105        return false;
106      }
107
108      socket.setTimeout(CUSTOM_TIMEOUT);
109      return true;
110    }
111  }
112
113  const agent = new CustomAgent({ keepAlive: true, timeout: AGENT_TIMEOUT });
114
115  const server = http.createServer((req, res) => {
116    res.end();
117  });
118
119  server.listen(0, common.mustCall(() => {
120    http.get({ port: server.address().port, agent })
121      .on('response', common.mustCall((res) => {
122        const socket = res.socket;
123        assert(socket);
124        res.resume();
125        socket.on('free', common.mustCall(() => {
126          socket.on('timeout', common.mustCall(() => {
127            assert.strictEqual(socket.timeout, CUSTOM_TIMEOUT);
128            agent.destroy();
129            server.close();
130          }));
131        }));
132      }));
133  }));
134}
135