11cb0ef41Sopenharmony_ci// This test is designed to fail with a segmentation fault in Node.js 4.1.0 and
21cb0ef41Sopenharmony_ci// execute without issues in Node.js 4.1.1 and up.
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci'use strict';
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst httpCommon = require('_http_common');
81cb0ef41Sopenharmony_ciconst { HTTPParser } = require('_http_common');
91cb0ef41Sopenharmony_ciconst net = require('net');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst COUNT = httpCommon.parsers.max + 1;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst parsers = new Array(COUNT);
141cb0ef41Sopenharmony_cifor (let i = 0; i < parsers.length; i++)
151cb0ef41Sopenharmony_ci  parsers[i] = httpCommon.parsers.alloc();
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cilet gotRequests = 0;
181cb0ef41Sopenharmony_cilet gotResponses = 0;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cifunction execAndClose() {
211cb0ef41Sopenharmony_ci  if (parsers.length === 0)
221cb0ef41Sopenharmony_ci    return;
231cb0ef41Sopenharmony_ci  process.stdout.write('.');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  const parser = parsers.pop();
261cb0ef41Sopenharmony_ci  parser.initialize(HTTPParser.RESPONSE, {});
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const socket = net.connect(common.PORT);
291cb0ef41Sopenharmony_ci  socket.on('error', (e) => {
301cb0ef41Sopenharmony_ci    // If SmartOS and ECONNREFUSED, then retry. See
311cb0ef41Sopenharmony_ci    // https://github.com/nodejs/node/issues/2663.
321cb0ef41Sopenharmony_ci    if (common.isSunOS && e.code === 'ECONNREFUSED') {
331cb0ef41Sopenharmony_ci      parsers.push(parser);
341cb0ef41Sopenharmony_ci      parser.reused = true;
351cb0ef41Sopenharmony_ci      socket.destroy();
361cb0ef41Sopenharmony_ci      setImmediate(execAndClose);
371cb0ef41Sopenharmony_ci      return;
381cb0ef41Sopenharmony_ci    }
391cb0ef41Sopenharmony_ci    throw e;
401cb0ef41Sopenharmony_ci  });
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  parser.consume(socket._handle);
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  parser.onIncoming = function onIncoming() {
451cb0ef41Sopenharmony_ci    process.stdout.write('+');
461cb0ef41Sopenharmony_ci    gotResponses++;
471cb0ef41Sopenharmony_ci    parser.unconsume();
481cb0ef41Sopenharmony_ci    httpCommon.freeParser(parser);
491cb0ef41Sopenharmony_ci    socket.destroy();
501cb0ef41Sopenharmony_ci    setImmediate(execAndClose);
511cb0ef41Sopenharmony_ci  };
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciconst server = net.createServer(function(c) {
551cb0ef41Sopenharmony_ci  if (++gotRequests === COUNT)
561cb0ef41Sopenharmony_ci    server.close();
571cb0ef41Sopenharmony_ci  c.end('HTTP/1.1 200 OK\r\n\r\n', function() {
581cb0ef41Sopenharmony_ci    c.destroySoon();
591cb0ef41Sopenharmony_ci  });
601cb0ef41Sopenharmony_ci}).listen(common.PORT, execAndClose);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ciprocess.on('exit', function() {
631cb0ef41Sopenharmony_ci  assert.strictEqual(gotResponses, COUNT);
641cb0ef41Sopenharmony_ci});
65