• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const { Resolver } = require('dns');
4const assert = require('assert');
5const dgram = require('dgram');
6
7const server = dgram.createSocket('udp4');
8const resolver = new Resolver();
9
10const desiredQueries = 11;
11let finishedQueries = 0;
12
13const addMessageListener = () => {
14  server.removeAllListeners('message');
15
16  server.once('message', () => {
17    server.once('message', common.mustNotCall);
18
19    resolver.cancel();
20  });
21};
22
23server.bind(0, common.mustCall(async () => {
24  resolver.setServers([`127.0.0.1:${server.address().port}`]);
25
26  const callback = common.mustCall((err, res) => {
27    assert.strictEqual(err.code, 'ECANCELLED');
28    assert.strictEqual(err.syscall, 'queryA');
29    assert.strictEqual(err.hostname, `example${finishedQueries}.org`);
30
31    finishedQueries++;
32    if (finishedQueries === desiredQueries) {
33      server.close();
34    }
35  }, desiredQueries);
36
37  const next = (...args) => {
38    callback(...args);
39
40    addMessageListener();
41
42    // Multiple queries
43    for (let i = 1; i < desiredQueries; i++) {
44      resolver.resolve4(`example${i}.org`, callback);
45    }
46  };
47
48  // Single query
49  addMessageListener();
50  resolver.resolve4('example0.org', next);
51}));
52