summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-unix-socket-keep-alive.js
blob: 11b3d9b39264a4e362392a240e476de7d6d3881a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer((req, res) => res.end());

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

server.listen(common.PIPE, common.mustCall(() =>
  asyncLoop(makeKeepAliveRequest, 10, common.mustCall(() =>
    server.getConnections(common.mustCall((err, conns) => {
      assert.ifError(err);
      assert.strictEqual(conns, 1);
      server.close();
    }))
  ))
));

function asyncLoop(fn, times, cb) {
  fn(function handler() {
    if (--times) {
      fn(handler);
    } else {
      cb();
    }
  });
}
function makeKeepAliveRequest(cb) {
  http.get({
    socketPath: common.PIPE,
    headers: { connection: 'keep-alive' }
  }, (res) => res.on('data', common.mustNotCall())
    .on('error', assert.fail)
    .on('end', cb)
  );
}