node/test/parallel/test-dns-channel-cancel.js
Szymon Marczak 8c0ec0f85f
test: fix DNS cancel tests
PR-URL: https://github.com/nodejs/node/pull/44432
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2024-05-11 20:05:59 +02:00

47 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
const { Resolver } = require('dns');
const assert = require('assert');
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
const resolver = new Resolver();
const desiredQueries = 11;
let finishedQueries = 0;
server.bind(0, common.mustCall(async () => {
resolver.setServers([`127.0.0.1:${server.address().port}`]);
const callback = common.mustCall((err, res) => {
assert.strictEqual(err.code, 'ECANCELLED');
assert.strictEqual(err.syscall, 'queryA');
assert.strictEqual(err.hostname, `example${finishedQueries}.org`);
finishedQueries++;
if (finishedQueries === desiredQueries) {
server.close();
}
}, desiredQueries);
const next = (...args) => {
callback(...args);
server.once('message', () => {
resolver.cancel();
});
// Multiple queries
for (let i = 1; i < desiredQueries; i++) {
resolver.resolve4(`example${i}.org`, callback);
}
};
server.once('message', () => {
resolver.cancel();
});
// Single query
resolver.resolve4('example0.org', next);
}));