|
4 | 4 | 'use strict';
|
5 | 5 | const common = require('../common');
|
6 | 6 | const assert = require('assert');
|
| 7 | +const httpCommon = require('_http_common'); |
| 8 | +const { HTTPParser } = require('_http_common'); |
7 | 9 | const net = require('net');
|
8 | 10 |
|
9 |
| -const COUNT = 1000 + 1; |
| 11 | +const COUNT = httpCommon.parsers.max + 1; |
| 12 | + |
| 13 | +const parsers = new Array(COUNT); |
| 14 | +for (let i = 0; i < parsers.length; i++) |
| 15 | + parsers[i] = httpCommon.parsers.alloc(); |
10 | 16 |
|
11 | 17 | let gotRequests = 0;
|
12 | 18 | let gotResponses = 0;
|
13 | 19 |
|
14 |
| -const response = Buffer.from('HTTP/1.1 200 OK\r\n\r\n'); |
15 |
| - |
16 | 20 | function execAndClose() {
|
| 21 | + if (parsers.length === 0) |
| 22 | + return; |
17 | 23 | process.stdout.write('.');
|
18 | 24 |
|
19 |
| - const socket = net.connect(common.PORT, common.localhostIPv4); |
20 |
| - socket.on('end', socket.end); |
21 |
| - socket.on('data', function(chunk) { |
22 |
| - process.stdout.write('+'); |
23 |
| - assert.deepStrictEqual(chunk, response); |
24 |
| - if (++gotResponses === COUNT) return; |
| 25 | + const parser = parsers.pop(); |
| 26 | + parser.initialize(HTTPParser.RESPONSE, {}); |
25 | 27 |
|
26 |
| - socket.on('close', execAndClose); |
| 28 | + const socket = net.connect(common.PORT, common.localhostIPv4); |
| 29 | + socket.on('error', (e) => { |
| 30 | + // If SmartOS and ECONNREFUSED, then retry. See |
| 31 | + // https://github.com./nodejs/node/issues/2663. |
| 32 | + if (common.isSunOS && e.code === 'ECONNREFUSED') { |
| 33 | + parsers.push(parser); |
| 34 | + parser.reused = true; |
| 35 | + socket.destroy(); |
| 36 | + setImmediate(execAndClose); |
| 37 | + return; |
| 38 | + } |
| 39 | + throw e; |
27 | 40 | });
|
| 41 | + |
| 42 | + parser.consume(socket._handle); |
| 43 | + |
| 44 | + parser.onIncoming = function onIncoming() { |
| 45 | + process.stdout.write('+'); |
| 46 | + gotResponses++; |
| 47 | + parser.unconsume(); |
| 48 | + httpCommon.freeParser(parser); |
| 49 | + socket.destroy(); |
| 50 | + setImmediate(execAndClose); |
| 51 | + }; |
28 | 52 | }
|
29 | 53 |
|
30 | 54 | const server = net.createServer(function(c) {
|
31 | 55 | if (++gotRequests === COUNT)
|
32 | 56 | server.close();
|
33 |
| - c.end(response); |
34 |
| - c.resume(); |
| 57 | + c.end('HTTP/1.1 200 OK\r\n\r\n', function() { |
| 58 | + c.destroySoon(); |
| 59 | + }); |
35 | 60 | }).listen(common.PORT, common.localhostIPv4, execAndClose);
|
36 | 61 |
|
37 | 62 | process.on('exit', function() {
|
|
0 commit comments