Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dns: fix uv_timer_start usage #39723

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,8 @@ void ChannelWrap::StartTimer() {
}
int timeout = timeout_;
if (timeout == 0) timeout = 1;
if (timeout < 0 || timeout > 1000) timeout = 1000;
uv_timer_start(timer_handle_, AresTimeout, timeout, timeout);
if (timeout < 0 || timeout >= 500) timeout = 500;
uv_timer_start(timer_handle_, AresTimeout, 0, timeout);
}

void ChannelWrap::CloseTimer() {
Expand Down
80 changes: 80 additions & 0 deletions test/parallel/test-dns-channel-tries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const dns = require('dns');

const TRIES = 1;
const IMPRECISION_MS = 500;

// Tests for dns.Resolver option `tries`.
// This will roughly test if a single try fails after the set `timeout`.

for (const ctor of [dns.Resolver, dns.promises.Resolver]) {
for (const tries of [null, true, false, '', '2']) {
assert.throws(() => new ctor({ tries }), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
}

for (const tries of [-2, 0, 4.2, 2 ** 31]) {
assert.throws(() => new ctor({ tries }), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
});
}

for (const tries of [2, 1, 5]) new ctor({ tries });
}

for (let timeout of [-1, 1000, 1500, 50]) {
const server = dgram.createSocket('udp4');
server.bind(0, '127.0.0.1', common.mustCall(() => {
const resolver = new dns.Resolver({ tries: TRIES, timeout });
resolver.setServers([`127.0.0.1:${server.address().port}`]);

const timeStart = performance.now();
resolver.resolve4('nodejs.org', common.mustCall((err) => {
assert.throws(() => { throw err; }, {
code: 'ETIMEOUT',
name: 'Error',
});
const timeEnd = performance.now();

// c-ares default (-1): 5000ms
if (timeout === -1) timeout = 5000;

// Adding 500ms due to imprecisions
const elapsedMs = (timeEnd - timeStart);
assert(elapsedMs <= timeout + IMPRECISION_MS, `The expected timeout did not occur. Expecting: ${timeout + IMPRECISION_MS} But got: ${elapsedMs}`);

server.close();
}));
}));
}

for (let timeout of [-1, 1000, 1500, 50]) {
const server = dgram.createSocket('udp4');
server.bind(0, '127.0.0.1', common.mustCall(() => {
const resolver = new dns.promises.Resolver({ tries: TRIES, timeout });
resolver.setServers([`127.0.0.1:${server.address().port}`]);
const timeStart = performance.now();
resolver.resolve4('nodejs.org').catch(common.mustCall((err) => {
assert.throws(() => { throw err; }, {
code: 'ETIMEOUT',
name: 'Error',
});
const timeEnd = performance.now();

// c-ares default (-1): 5000ms
if (timeout === -1) timeout = 5000;

// Adding 500ms due to imprecisions
const elapsedMs = (timeEnd - timeStart);
assert(elapsedMs <= timeout + IMPRECISION_MS, `The expected timeout did not occur. Expecting: ${timeout + IMPRECISION_MS} But got: ${elapsedMs}`);

server.close();
}));
}));
}