node/test/parallel/test-dns-channel-timeout.js
Ben Noordhuis 1904e7372d
dns: make dns.Resolver timeout configurable
PR-URL: https://github.com/nodejs/node/pull/33472
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
2020-06-01 08:07:59 -07:00

54 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const dns = require('dns');
for (const ctor of [dns.Resolver, dns.promises.Resolver]) {
for (const timeout of [null, true, false, '', '2']) {
assert.throws(() => new ctor({ timeout }), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
}
for (const timeout of [-2, 4.2, 2 ** 31]) {
assert.throws(() => new ctor({ timeout }), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
});
}
for (const timeout of [-1, 0, 1]) new ctor({ timeout }); // OK
}
for (const timeout of [0, 1, 2]) {
const server = dgram.createSocket('udp4');
server.bind(0, '127.0.0.1', common.mustCall(() => {
const resolver = new dns.Resolver({ timeout });
resolver.setServers([`127.0.0.1:${server.address().port}`]);
resolver.resolve4('nodejs.org', common.mustCall((err) => {
assert.throws(() => { throw err; }, {
code: 'ETIMEOUT',
name: 'Error',
});
server.close();
}));
}));
}
for (const timeout of [0, 1, 2]) {
const server = dgram.createSocket('udp4');
server.bind(0, '127.0.0.1', common.mustCall(() => {
const resolver = new dns.promises.Resolver({ timeout });
resolver.setServers([`127.0.0.1:${server.address().port}`]);
resolver.resolve4('nodejs.org').catch(common.mustCall((err) => {
assert.throws(() => { throw err; }, {
code: 'ETIMEOUT',
name: 'Error',
});
server.close();
}));
}));
}