|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const http = require('http'); |
| 5 | + |
| 6 | +const server = http.createServer(common.mustCallAtLeast((req, res) => { |
| 7 | + const content = 'Hello Agent'; |
| 8 | + res.writeHead(200, { |
| 9 | + 'Content-Length': content.length.toString(), |
| 10 | + }); |
| 11 | + res.write(content); |
| 12 | + res.end(); |
| 13 | +}, 2)); |
| 14 | + |
| 15 | +server.listen(0, common.mustCall(() => { |
| 16 | + const agent = new http.Agent({ timeout: 100, keepAlive: true }); |
| 17 | + const req = http.get({ |
| 18 | + path: '/', |
| 19 | + port: server.address().port, |
| 20 | + agent |
| 21 | + }, common.mustCall((res) => { |
| 22 | + assert.strictEqual(res.statusCode, 200); |
| 23 | + res.resume(); |
| 24 | + res.on('end', common.mustCall()); |
| 25 | + })); |
| 26 | + |
| 27 | + const timer = setTimeout(() => { |
| 28 | + assert.fail('should not timeout here'); |
| 29 | + req.abort(); |
| 30 | + }, 1000); |
| 31 | + |
| 32 | + req.on('socket', common.mustCall((socket) => { |
| 33 | + // wait free socket become free and timeout |
| 34 | + socket.on('timeout', common.mustCall(() => { |
| 35 | + // free socket should be destroyed |
| 36 | + assert.strictEqual(socket.writable, false); |
| 37 | + // send new request will be fails |
| 38 | + clearTimeout(timer); |
| 39 | + const newReq = http.get({ |
| 40 | + path: '/', |
| 41 | + port: server.address().port, |
| 42 | + agent |
| 43 | + }, common.mustCall((res) => { |
| 44 | + // agent must create a new socket to handle request |
| 45 | + assert.notStrictEqual(newReq.socket, socket); |
| 46 | + assert.strictEqual(res.statusCode, 200); |
| 47 | + res.resume(); |
| 48 | + res.on('end', common.mustCall(() => server.close())); |
| 49 | + })); |
| 50 | + })); |
| 51 | + })); |
| 52 | +})); |
0 commit comments