11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst net = require('net');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst truthyValues = [true, 1, 'true', {}, []];
81cb0ef41Sopenharmony_ciconst delays = [[123, 0], [456123, 456], [-123000, 0], [undefined, 0]];
91cb0ef41Sopenharmony_ciconst falseyValues = [false, 0, ''];
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst genSetKeepAlive = (desiredEnable, desiredDelay) => (enable, delay) => {
121cb0ef41Sopenharmony_ci  assert.strictEqual(enable, desiredEnable);
131cb0ef41Sopenharmony_ci  assert.strictEqual(delay, desiredDelay);
141cb0ef41Sopenharmony_ci};
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cifor (const value of truthyValues) {
171cb0ef41Sopenharmony_ci  for (const delay of delays) {
181cb0ef41Sopenharmony_ci    const server = net.createServer();
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    server.listen(0, common.mustCall(function() {
211cb0ef41Sopenharmony_ci      const port = server.address().port;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci      const client = net.connect(
241cb0ef41Sopenharmony_ci        { port, keepAlive: value, keepAliveInitialDelay: delay[0] },
251cb0ef41Sopenharmony_ci        common.mustCall(() => client.end())
261cb0ef41Sopenharmony_ci      );
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci      client._handle.setKeepAlive = common.mustCall(
291cb0ef41Sopenharmony_ci        genSetKeepAlive(true, delay[1])
301cb0ef41Sopenharmony_ci      );
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci      client.on('end', common.mustCall(function() {
331cb0ef41Sopenharmony_ci        server.close();
341cb0ef41Sopenharmony_ci      }));
351cb0ef41Sopenharmony_ci    }));
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cifor (const value of falseyValues) {
401cb0ef41Sopenharmony_ci  const server = net.createServer();
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(function() {
431cb0ef41Sopenharmony_ci    const port = server.address().port;
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    const client = net.connect(
461cb0ef41Sopenharmony_ci      { port, keepAlive: value },
471cb0ef41Sopenharmony_ci      common.mustCall(() => client.end())
481cb0ef41Sopenharmony_ci    );
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    client._handle.setKeepAlive = common.mustNotCall();
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci    client.on('end', common.mustCall(function() {
531cb0ef41Sopenharmony_ci      server.close();
541cb0ef41Sopenharmony_ci    }));
551cb0ef41Sopenharmony_ci  }));
561cb0ef41Sopenharmony_ci}
57