1'use strict'; 2const common = require('../common'); 3const { promises: dnsPromises } = require('dns'); 4const assert = require('assert'); 5const dgram = require('dgram'); 6 7const server = dgram.createSocket('udp4'); 8const resolver = new dnsPromises.Resolver(); 9 10const addMessageListener = () => { 11 server.removeAllListeners('message'); 12 13 server.once('message', () => { 14 server.once('message', common.mustNotCall); 15 16 resolver.cancel(); 17 }); 18}; 19 20server.bind(0, common.mustCall(async () => { 21 resolver.setServers([`127.0.0.1:${server.address().port}`]); 22 23 addMessageListener(); 24 25 // Single promise 26 { 27 const hostname = 'example0.org'; 28 29 await assert.rejects( 30 resolver.resolve4(hostname), 31 { 32 code: 'ECANCELLED', 33 syscall: 'queryA', 34 hostname 35 } 36 ); 37 } 38 39 addMessageListener(); 40 41 // Multiple promises 42 { 43 const assertions = []; 44 const assertionCount = 10; 45 46 for (let i = 1; i <= assertionCount; i++) { 47 const hostname = `example${i}.org`; 48 49 assertions.push( 50 assert.rejects( 51 resolver.resolve4(hostname), 52 { 53 code: 'ECANCELLED', 54 syscall: 'queryA', 55 hostname: hostname 56 } 57 ) 58 ); 59 } 60 61 await Promise.all(assertions); 62 } 63 64 server.close(); 65})); 66