|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const net = require('net'); |
| 6 | + |
| 7 | +// The keep-alive delays are given in milliseconds but the underlying socket |
| 8 | +// options are configured in whole seconds. A positive value below 1000 ms is |
| 9 | +// truncated to 0 seconds, which leaves the system default in place instead of |
| 10 | +// applying the requested timing. Verifies that this emits a warning rather |
| 11 | +// than being silently ignored. |
| 12 | + |
| 13 | +// Warnings are emitted on the process, so the cases run one at a time to keep |
| 14 | +// each one's warnings from being observed by the others. |
| 15 | +const cases = [ |
| 16 | + // A delay below 1000 ms is truncated to 0 and warns. |
| 17 | + { |
| 18 | + configure: (client) => client.setKeepAlive(true, 400), |
| 19 | + check: common.mustCall((messages) => { |
| 20 | + assert.strictEqual(messages.length, 1); |
| 21 | + assert.match(messages[0], /initialDelay of 400 ms/); |
| 22 | + assert.match(messages[0], /at least 1000 ms/); |
| 23 | + }), |
| 24 | + }, |
| 25 | + // The interval is truncated the same way and warns independently. |
| 26 | + { |
| 27 | + configure: (client) => client.setKeepAlive(true, 5000, 500), |
| 28 | + check: common.mustCall((messages) => { |
| 29 | + assert.strictEqual(messages.length, 1); |
| 30 | + assert.match(messages[0], /interval of 500 ms/); |
| 31 | + }), |
| 32 | + }, |
| 33 | + // Both delays can be truncated by the same call. |
| 34 | + { |
| 35 | + configure: (client) => client.setKeepAlive(true, 400, 500), |
| 36 | + check: common.mustCall((messages) => { |
| 37 | + assert.strictEqual(messages.length, 2); |
| 38 | + assert.match(messages[0], /initialDelay of 400 ms/); |
| 39 | + assert.match(messages[1], /interval of 500 ms/); |
| 40 | + }), |
| 41 | + }, |
| 42 | + // The options object form warns as well. |
| 43 | + { |
| 44 | + configure: (client) => client.setKeepAlive({ |
| 45 | + enable: true, |
| 46 | + initialDelay: 999, |
| 47 | + }), |
| 48 | + check: common.mustCall((messages) => { |
| 49 | + assert.strictEqual(messages.length, 1); |
| 50 | + assert.match(messages[0], /initialDelay of 999 ms/); |
| 51 | + }), |
| 52 | + }, |
| 53 | + // A delay of at least 1000 ms is applied as requested and does not warn. |
| 54 | + { |
| 55 | + configure: (client) => client.setKeepAlive(true, 1000), |
| 56 | + check: common.mustCall((messages) => assert.deepStrictEqual(messages, [])), |
| 57 | + }, |
| 58 | + // 0 means "leave the current value unchanged" and is not a truncation. |
| 59 | + { |
| 60 | + configure: (client) => client.setKeepAlive(true, 0), |
| 61 | + check: common.mustCall((messages) => assert.deepStrictEqual(messages, [])), |
| 62 | + }, |
| 63 | + // Omitting the delay does not warn. |
| 64 | + { |
| 65 | + configure: (client) => client.setKeepAlive(true), |
| 66 | + check: common.mustCall((messages) => assert.deepStrictEqual(messages, [])), |
| 67 | + }, |
| 68 | + // Nothing is configured when keep-alive is disabled, so there is nothing to |
| 69 | + // warn about. |
| 70 | + { |
| 71 | + configure: (client) => client.setKeepAlive(false, 400), |
| 72 | + check: common.mustCall((messages) => assert.deepStrictEqual(messages, [])), |
| 73 | + }, |
| 74 | +]; |
| 75 | + |
| 76 | +function runCase({ configure, check }, done) { |
| 77 | + const messages = []; |
| 78 | + const onWarning = (warning) => { |
| 79 | + if (warning.name === 'KeepAliveWarning') messages.push(warning.message); |
| 80 | + }; |
| 81 | + process.on('warning', onWarning); |
| 82 | + |
| 83 | + const server = net.createServer(); |
| 84 | + server.listen(0, common.mustCall(() => { |
| 85 | + const client = net.connect( |
| 86 | + { port: server.address().port }, |
| 87 | + common.mustCall(() => { |
| 88 | + configure(client); |
| 89 | + client.end(); |
| 90 | + })); |
| 91 | + |
| 92 | + client.on('end', common.mustCall(() => { |
| 93 | + server.close(common.mustCall(() => { |
| 94 | + // Warnings are emitted on the next tick. |
| 95 | + setImmediate(() => { |
| 96 | + process.removeListener('warning', onWarning); |
| 97 | + check(messages); |
| 98 | + done(); |
| 99 | + }); |
| 100 | + })); |
| 101 | + })); |
| 102 | + })); |
| 103 | +} |
| 104 | + |
| 105 | +(function next(i) { |
| 106 | + if (i === cases.length) return; |
| 107 | + runCase(cases[i], common.mustCall(() => next(i + 1))); |
| 108 | +})(0); |
0 commit comments