• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const http = require('http');
5const net = require('net');
6
7const tests = [];
8
9function test(fn) {
10  if (!tests.length) {
11    process.nextTick(run);
12  }
13  tests.push(fn);
14}
15
16function run() {
17  const fn = tests.shift();
18  if (fn) fn(run);
19}
20
21function done(server, socket, cb) {
22  socket.destroy();
23  server.close(cb);
24}
25
26function serverTest(withPipeline, cb) {
27  let gotAll = false;
28  let timedout = false;
29  const server = http.createServer(common.mustCall((req, res) => {
30    if (withPipeline)
31      res.end();
32    if (req.url === '/3') {
33      gotAll = true;
34      if (timedout)
35        done(server, req.socket, cb);
36    }
37  }, 3));
38  server.setTimeout(500, common.mustCallAtLeast((socket) => {
39    // End this test and call `run()` for the next test (if any).
40    timedout = true;
41    if (gotAll)
42      done(server, socket, cb);
43  }));
44  server.keepAliveTimeout = 50;
45  server.listen(0, common.mustCall(() => {
46    const options = {
47      port: server.address().port,
48      allowHalfOpen: true
49    };
50    const c = net.connect(options, () => {
51      c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
52      c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
53      c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
54    });
55  }));
56}
57
58test(function serverEndKeepAliveTimeoutWithPipeline(cb) {
59  serverTest(true, cb);
60});
61
62test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
63  serverTest(false, cb);
64});
65