-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dns: add a cancel() method to the promise Resolver
PR-URL: #33099 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
- Loading branch information
Showing
4 changed files
with
110 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'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(); | ||
|
||
const addMessageListener = () => { | ||
server.removeAllListeners('message'); | ||
|
||
server.once('message', () => { | ||
server.once('message', common.mustNotCall); | ||
|
||
resolver.cancel(); | ||
}); | ||
}; | ||
|
||
server.bind(0, common.mustCall(async () => { | ||
resolver.setServers([`127.0.0.1:${server.address().port}`]); | ||
|
||
addMessageListener(); | ||
|
||
// Single promise | ||
{ | ||
const hostname = 'example0.org'; | ||
|
||
await assert.rejects( | ||
resolver.resolve4(hostname), | ||
{ | ||
code: 'ECANCELLED', | ||
syscall: 'queryA', | ||
hostname | ||
} | ||
); | ||
} | ||
|
||
addMessageListener(); | ||
|
||
// Multiple promises | ||
{ | ||
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(); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,51 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const dnstools = require('../common/dns'); | ||
const { Resolver } = require('dns'); | ||
const assert = require('assert'); | ||
const dgram = require('dgram'); | ||
|
||
const server = dgram.createSocket('udp4'); | ||
const resolver = new Resolver(); | ||
|
||
server.bind(0, common.mustCall(() => { | ||
const desiredQueries = 11; | ||
let finishedQueries = 0; | ||
|
||
const addMessageListener = () => { | ||
server.removeAllListeners('message'); | ||
|
||
server.once('message', () => { | ||
server.once('message', common.mustNotCall); | ||
|
||
resolver.cancel(); | ||
}); | ||
}; | ||
|
||
server.bind(0, common.mustCall(async () => { | ||
resolver.setServers([`127.0.0.1:${server.address().port}`]); | ||
resolver.resolve4('example.org', common.mustCall((err, res) => { | ||
|
||
const callback = common.mustCall((err, res) => { | ||
assert.strictEqual(err.code, 'ECANCELLED'); | ||
assert.strictEqual(err.syscall, 'queryA'); | ||
assert.strictEqual(err.hostname, 'example.org'); | ||
server.close(); | ||
})); | ||
})); | ||
assert.strictEqual(err.hostname, `example${finishedQueries}.org`); | ||
|
||
finishedQueries++; | ||
if (finishedQueries === desiredQueries) { | ||
server.close(); | ||
} | ||
}, desiredQueries); | ||
|
||
const next = (...args) => { | ||
callback(...args); | ||
|
||
addMessageListener(); | ||
|
||
server.on('message', common.mustCall((msg, { address, port }) => { | ||
const parsed = dnstools.parseDNSPacket(msg); | ||
const domain = parsed.questions[0].domain; | ||
assert.strictEqual(domain, 'example.org'); | ||
// Multiple queries | ||
for (let i = 1; i < desiredQueries; i++) { | ||
resolver.resolve4(`example${i}.org`, callback); | ||
} | ||
}; | ||
|
||
// Do not send a reply. | ||
resolver.cancel(); | ||
// Single query | ||
addMessageListener(); | ||
resolver.resolve4('example0.org', next); | ||
})); |