0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-dns-channel-cancel-promise.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

60 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../common');
const { promises: dnsPromises } = require('dns');
const assert = require('assert');
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
const resolver = new dnsPromises.Resolver();
server.bind(0, common.mustCall(async () => {
resolver.setServers([`127.0.0.1:${server.address().port}`]);
// Single promise
{
server.once('message', () => {
resolver.cancel();
});
const hostname = 'example0.org';
await assert.rejects(
resolver.resolve4(hostname),
{
code: 'ECANCELLED',
syscall: 'queryA',
hostname
}
);
}
// Multiple promises
{
server.once('message', () => {
resolver.cancel();
});
const assertions = [];
const assertionCount = 10;
for (let i = 1; i <= assertionCount; i++) {
const hostname = `example${i}.org`;
assertions.push(
assert.rejects(
resolver.resolve4(hostname),
{
code: 'ECANCELLED',
syscall: 'queryA',
hostname: hostname
}
)
);
}
await Promise.all(assertions);
}
server.close();
}));