1// This test is designed to fail with a segmentation fault in Node.js 4.1.0 and 2// execute without issues in Node.js 4.1.1 and up. 3 4'use strict'; 5const common = require('../common'); 6const assert = require('assert'); 7const httpCommon = require('_http_common'); 8const { HTTPParser } = require('_http_common'); 9const net = require('net'); 10 11const COUNT = httpCommon.parsers.max + 1; 12 13const parsers = new Array(COUNT); 14for (let i = 0; i < parsers.length; i++) 15 parsers[i] = httpCommon.parsers.alloc(); 16 17let gotRequests = 0; 18let gotResponses = 0; 19 20function execAndClose() { 21 if (parsers.length === 0) 22 return; 23 process.stdout.write('.'); 24 25 const parser = parsers.pop(); 26 parser.initialize(HTTPParser.RESPONSE, {}); 27 28 const socket = net.connect(common.PORT); 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; 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 }; 52} 53 54const server = net.createServer(function(c) { 55 if (++gotRequests === COUNT) 56 server.close(); 57 c.end('HTTP/1.1 200 OK\r\n\r\n', function() { 58 c.destroySoon(); 59 }); 60}).listen(common.PORT, execAndClose); 61 62process.on('exit', function() { 63 assert.strictEqual(gotResponses, COUNT); 64}); 65