|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const net = require('net'); |
| 6 | + |
| 7 | +// Verifies that setKeepAlive() forwards the keepalive probe interval |
| 8 | +// (TCP_KEEPINTVL) and probe count (TCP_KEEPCNT) to the handle. The interval is |
| 9 | +// converted from milliseconds to whole seconds, mirroring the initialDelay |
| 10 | +// handling and uv_tcp_keepalive_ex(). |
| 11 | + |
| 12 | +// Explicit setKeepAlive(enable, initialDelay, interval, count) forwards all |
| 13 | +// four values, converting milliseconds to seconds for the delays. |
| 14 | +{ |
| 15 | + const server = net.createServer(); |
| 16 | + server.listen(0, common.mustCall(() => { |
| 17 | + const client = net.connect( |
| 18 | + { port: server.address().port }, |
| 19 | + common.mustCall(() => { |
| 20 | + client._handle.setKeepAlive = common.mustCall( |
| 21 | + (enable, delay, interval, count) => { |
| 22 | + assert.strictEqual(enable, true); |
| 23 | + assert.strictEqual(delay, 5); |
| 24 | + assert.strictEqual(interval, 10); |
| 25 | + assert.strictEqual(count, 9); |
| 26 | + }); |
| 27 | + client.setKeepAlive(true, 5000, 10000, 9); |
| 28 | + client.end(); |
| 29 | + })); |
| 30 | + |
| 31 | + client.on('end', common.mustCall(() => server.close())); |
| 32 | + })); |
| 33 | +} |
| 34 | + |
| 35 | +// Omitting interval/count passes undefined through; the handle applies the |
| 36 | +// libuv defaults (1s interval, 10 probes). |
| 37 | +{ |
| 38 | + const server = net.createServer(); |
| 39 | + server.listen(0, common.mustCall(() => { |
| 40 | + const client = net.connect( |
| 41 | + { port: server.address().port }, |
| 42 | + common.mustCall(() => { |
| 43 | + client._handle.setKeepAlive = common.mustCall( |
| 44 | + (enable, delay, interval, count) => { |
| 45 | + assert.strictEqual(enable, true); |
| 46 | + assert.strictEqual(delay, 5); |
| 47 | + assert.strictEqual(interval, undefined); |
| 48 | + assert.strictEqual(count, undefined); |
| 49 | + }); |
| 50 | + client.setKeepAlive(true, 5000); |
| 51 | + client.end(); |
| 52 | + })); |
| 53 | + |
| 54 | + client.on('end', common.mustCall(() => server.close())); |
| 55 | + })); |
| 56 | +} |
| 57 | + |
| 58 | +// An options object as the first argument is equivalent to the positional |
| 59 | +// form, forwarding enable/initialDelay/interval/count to the handle. |
| 60 | +{ |
| 61 | + const server = net.createServer(); |
| 62 | + server.listen(0, common.mustCall(() => { |
| 63 | + const client = net.connect( |
| 64 | + { port: server.address().port }, |
| 65 | + common.mustCall(() => { |
| 66 | + client._handle.setKeepAlive = common.mustCall( |
| 67 | + (enable, delay, interval, count) => { |
| 68 | + assert.strictEqual(enable, true); |
| 69 | + assert.strictEqual(delay, 5); |
| 70 | + assert.strictEqual(interval, 10); |
| 71 | + assert.strictEqual(count, 9); |
| 72 | + }); |
| 73 | + client.setKeepAlive({ |
| 74 | + enable: true, |
| 75 | + initialDelay: 5000, |
| 76 | + interval: 10000, |
| 77 | + count: 9, |
| 78 | + }); |
| 79 | + client.end(); |
| 80 | + })); |
| 81 | + |
| 82 | + client.on('end', common.mustCall(() => server.close())); |
| 83 | + })); |
| 84 | +} |
| 85 | + |
| 86 | +// Omitted options properties behave like omitted positional arguments. |
| 87 | +{ |
| 88 | + const server = net.createServer(); |
| 89 | + server.listen(0, common.mustCall(() => { |
| 90 | + const client = net.connect( |
| 91 | + { port: server.address().port }, |
| 92 | + common.mustCall(() => { |
| 93 | + client._handle.setKeepAlive = common.mustCall( |
| 94 | + (enable, delay, interval, count) => { |
| 95 | + assert.strictEqual(enable, true); |
| 96 | + assert.strictEqual(delay, 5); |
| 97 | + assert.strictEqual(interval, undefined); |
| 98 | + assert.strictEqual(count, undefined); |
| 99 | + }); |
| 100 | + client.setKeepAlive({ enable: true, initialDelay: 5000 }); |
| 101 | + client.end(); |
| 102 | + })); |
| 103 | + |
| 104 | + client.on('end', common.mustCall(() => server.close())); |
| 105 | + })); |
| 106 | +} |
0 commit comments