• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const net = require('net');
6
7const truthyValues = [true, 1, 'true', {}, []];
8const delays = [[123, 0], [456123, 456], [-123000, 0], [undefined, 0]];
9const falseyValues = [false, 0, ''];
10
11const genSetKeepAlive = (desiredEnable, desiredDelay) => (enable, delay) => {
12  assert.strictEqual(enable, desiredEnable);
13  assert.strictEqual(delay, desiredDelay);
14};
15
16for (const value of truthyValues) {
17  for (const delay of delays) {
18    const server = net.createServer();
19
20    server.listen(0, common.mustCall(function() {
21      const port = server.address().port;
22
23      const client = net.connect(
24        { port, keepAlive: value, keepAliveInitialDelay: delay[0] },
25        common.mustCall(() => client.end())
26      );
27
28      client._handle.setKeepAlive = common.mustCall(
29        genSetKeepAlive(true, delay[1])
30      );
31
32      client.on('end', common.mustCall(function() {
33        server.close();
34      }));
35    }));
36  }
37}
38
39for (const value of falseyValues) {
40  const server = net.createServer();
41
42  server.listen(0, common.mustCall(function() {
43    const port = server.address().port;
44
45    const client = net.connect(
46      { port, keepAlive: value },
47      common.mustCall(() => client.end())
48    );
49
50    client._handle.setKeepAlive = common.mustNotCall();
51
52    client.on('end', common.mustCall(function() {
53      server.close();
54    }));
55  }));
56}
57