11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst { Resolver } = require('dns');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst dgram = require('dgram');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst server = dgram.createSocket('udp4');
81cb0ef41Sopenharmony_ciconst resolver = new Resolver();
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst desiredQueries = 11;
111cb0ef41Sopenharmony_cilet finishedQueries = 0;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst addMessageListener = () => {
141cb0ef41Sopenharmony_ci  server.removeAllListeners('message');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  server.once('message', () => {
171cb0ef41Sopenharmony_ci    server.once('message', common.mustNotCall);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci    resolver.cancel();
201cb0ef41Sopenharmony_ci  });
211cb0ef41Sopenharmony_ci};
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciserver.bind(0, common.mustCall(async () => {
241cb0ef41Sopenharmony_ci  resolver.setServers([`127.0.0.1:${server.address().port}`]);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  const callback = common.mustCall((err, res) => {
271cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ECANCELLED');
281cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'queryA');
291cb0ef41Sopenharmony_ci    assert.strictEqual(err.hostname, `example${finishedQueries}.org`);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    finishedQueries++;
321cb0ef41Sopenharmony_ci    if (finishedQueries === desiredQueries) {
331cb0ef41Sopenharmony_ci      server.close();
341cb0ef41Sopenharmony_ci    }
351cb0ef41Sopenharmony_ci  }, desiredQueries);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  const next = (...args) => {
381cb0ef41Sopenharmony_ci    callback(...args);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    addMessageListener();
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    // Multiple queries
431cb0ef41Sopenharmony_ci    for (let i = 1; i < desiredQueries; i++) {
441cb0ef41Sopenharmony_ci      resolver.resolve4(`example${i}.org`, callback);
451cb0ef41Sopenharmony_ci    }
461cb0ef41Sopenharmony_ci  };
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  // Single query
491cb0ef41Sopenharmony_ci  addMessageListener();
501cb0ef41Sopenharmony_ci  resolver.resolve4('example0.org', next);
511cb0ef41Sopenharmony_ci}));
52