Files
node/test/parallel/test-dns-resolver-max-timeout.js
T
Shelley Vohr fab81d15c3 test: widen the gap in the resolver maxTimeout comparison
test-dns-resolver-max-timeout times a query with `{ timeout: 500,
tries: 3 }` against one that also sets `maxTimeout: 500` and asserts
the first took longer. c-ares only expires a try when cares_wrap's
timer fires, every `timeout` ms, so each try costs one or two ticks
depending on sub-millisecond ordering, and uncapped retries also get
0.5-1x jitter. That leaves the capped run anywhere in 1500-3000 ms and
the uncapped one in 3000-4500 ms; on a busy rhel10-ppc64le host both
came out at 3005 ms. With `timeout: 100, tries: 5` the ranges (about
600-1000 ms and 5000-7500 ms measured under load) cannot meet.

Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65780
Fixes: https://github.com/nodejs/node/issues/64226
Refs: https://github.com/nodejs/node/issues/59146
Refs: https://github.com/nodejs/node/issues/54534
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
2026-09-08 22:07:35 +02:00

77 lines
2.4 KiB
JavaScript

'use strict';
const common = require('../common');
const dnstools = require('../common/dns');
const dns = require('dns');
const assert = require('assert');
const dgram = require('dgram');
[
-1,
1.1,
NaN,
{},
[],
null,
function() {},
Symbol(),
true,
Infinity,
].forEach((maxTimeout) => {
assert.throws(() => {
new dns.Resolver({ maxTimeout });
}, /ERR_OUT_OF_RANGE|ERR_INVALID_ARG_TYPE/i);
});
const server = dgram.createSocket('udp4');
const nxdomain = 'nxdomain.org';
const domain = 'example.org';
const answers = [{ type: 'A', address: '1.2.3.4', ttl: 123, domain }];
server.on('message', common.mustCallAtLeast((msg, { address, port }) => {
const parsed = dnstools.parseDNSPacket(msg);
if (parsed.questions[0].domain === nxdomain) {
return;
}
assert.strictEqual(parsed.questions[0].domain, domain);
server.send(dnstools.writeDNSPacket({
id: parsed.id,
questions: parsed.questions,
answers: answers,
}), port, address);
}), 1);
server.bind(0, common.mustCall(async () => {
const address = server.address();
// Test if the Resolver works as before.
const resolver = new dns.promises.Resolver({ timeout: 1000, tries: 1, maxTimeout: 1000 });
resolver.setServers([`127.0.0.1:${address.port}`]);
const res = await resolver.resolveAny('example.org');
assert.strictEqual(res.length, 1);
assert.strictEqual(res.length, answers.length);
assert.strictEqual(res[0].address, answers[0].address);
// Test that maxTimeout is effective.
// Without maxTimeout, the timeout will keep increasing when retrying.
// Expired tries are only noticed on the resolver's `timeout` ms timer tick,
// so use enough tries that the doubling run cannot overlap the capped one.
const timeout1 = await timeout(address, { timeout: 100, tries: 5 });
// With maxTimeout, the timeout will always be 100 when retrying.
const timeout2 = await timeout(address, { timeout: 100, tries: 5, maxTimeout: 100 });
console.log(`timeout1: ${timeout1}, timeout2: ${timeout2}`);
assert.strictEqual(timeout1 !== undefined && timeout2 !== undefined, true);
assert.strictEqual(timeout1 > timeout2, true);
server.close();
}));
async function timeout(address, options) {
const start = Date.now();
const resolver = new dns.promises.Resolver(options);
resolver.setServers([`127.0.0.1:${address.port}`]);
try {
await resolver.resolveAny(nxdomain);
} catch (e) {
assert.strictEqual(e.code, 'ETIMEOUT');
return Date.now() - start;
}
}