|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +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, and uv_tcp_keepalive() rejects a |
| 9 | +// delay outside [1, 32767]. Verifies that a delay which cannot be applied as |
| 10 | +// requested is rejected instead of being silently altered. |
| 11 | + |
| 12 | +const MAX_DELAY = 32767 * 1000; |
| 13 | + |
| 14 | +// A positive delay below 1000 ms would be truncated to 0 seconds, which leaves |
| 15 | +// the system default in place instead of applying the requested timing. |
| 16 | +for (const initialDelay of [1, 400, 999]) { |
| 17 | + assert.throws(() => net.Socket.prototype.setKeepAlive.call( |
| 18 | + {}, true, initialDelay), { |
| 19 | + code: 'ERR_OUT_OF_RANGE', |
| 20 | + name: 'RangeError', |
| 21 | + message: /The value of "initialDelay" is out of range/, |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +// The interval is validated the same way. |
| 26 | +assert.throws(() => net.Socket.prototype.setKeepAlive.call( |
| 27 | + {}, true, 5000, 500), { |
| 28 | + code: 'ERR_OUT_OF_RANGE', |
| 29 | + message: /The value of "interval" is out of range/, |
| 30 | +}); |
| 31 | + |
| 32 | +// A delay above the maximum cannot be carried by the socket options. |
| 33 | +assert.throws(() => net.Socket.prototype.setKeepAlive.call( |
| 34 | + {}, true, MAX_DELAY + 1000), { |
| 35 | + code: 'ERR_OUT_OF_RANGE', |
| 36 | + message: /The value of "initialDelay" is out of range/, |
| 37 | +}); |
| 38 | + |
| 39 | +// The options object form is validated as well. |
| 40 | +assert.throws(() => net.Socket.prototype.setKeepAlive.call( |
| 41 | + {}, { enable: true, initialDelay: 999 }), { |
| 42 | + code: 'ERR_OUT_OF_RANGE', |
| 43 | + message: /The value of "initialDelay" is out of range/, |
| 44 | +}); |
| 45 | + |
| 46 | +// A non-numeric delay is rejected by the same check. |
| 47 | +assert.throws(() => net.Socket.prototype.setKeepAlive.call( |
| 48 | + {}, true, '1000'), { |
| 49 | + code: 'ERR_INVALID_ARG_TYPE', |
| 50 | +}); |
| 51 | + |
| 52 | +// Delays that can be applied as requested are accepted. A non-positive value |
| 53 | +// keeps its documented meaning of leaving the current setting unchanged, and |
| 54 | +// so does omitting it entirely. |
| 55 | +const server = net.createServer(); |
| 56 | +server.listen(0, () => { |
| 57 | + const client = net.connect({ port: server.address().port }, () => { |
| 58 | + for (const args of [ |
| 59 | + [true, 1000], |
| 60 | + [true, MAX_DELAY], |
| 61 | + [true, 5000, 1000], |
| 62 | + [true, 0], |
| 63 | + [true, -1], |
| 64 | + [true], |
| 65 | + // Agent passes Infinity to mean "no timeout". |
| 66 | + [true, Infinity], |
| 67 | + [{ enable: true, initialDelay: 1000 }], |
| 68 | + // Nothing is configured while keep-alive is disabled, so a delay that |
| 69 | + // could not be applied is not rejected either. |
| 70 | + [false, 400], |
| 71 | + ]) { |
| 72 | + client.setKeepAlive(...args); |
| 73 | + } |
| 74 | + |
| 75 | + client.end(); |
| 76 | + }); |
| 77 | + |
| 78 | + client.on('end', () => server.close()); |
| 79 | +}); |
0 commit comments