11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst { promises: dnsPromises } = require('dns');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst dgram = require('dgram');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst server = dgram.createSocket('udp4');
81cb0ef41Sopenharmony_ciconst resolver = new dnsPromises.Resolver();
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst addMessageListener = () => {
111cb0ef41Sopenharmony_ci  server.removeAllListeners('message');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  server.once('message', () => {
141cb0ef41Sopenharmony_ci    server.once('message', common.mustNotCall);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci    resolver.cancel();
171cb0ef41Sopenharmony_ci  });
181cb0ef41Sopenharmony_ci};
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciserver.bind(0, common.mustCall(async () => {
211cb0ef41Sopenharmony_ci  resolver.setServers([`127.0.0.1:${server.address().port}`]);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  addMessageListener();
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  // Single promise
261cb0ef41Sopenharmony_ci  {
271cb0ef41Sopenharmony_ci    const hostname = 'example0.org';
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    await assert.rejects(
301cb0ef41Sopenharmony_ci      resolver.resolve4(hostname),
311cb0ef41Sopenharmony_ci      {
321cb0ef41Sopenharmony_ci        code: 'ECANCELLED',
331cb0ef41Sopenharmony_ci        syscall: 'queryA',
341cb0ef41Sopenharmony_ci        hostname
351cb0ef41Sopenharmony_ci      }
361cb0ef41Sopenharmony_ci    );
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  addMessageListener();
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  // Multiple promises
421cb0ef41Sopenharmony_ci  {
431cb0ef41Sopenharmony_ci    const assertions = [];
441cb0ef41Sopenharmony_ci    const assertionCount = 10;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    for (let i = 1; i <= assertionCount; i++) {
471cb0ef41Sopenharmony_ci      const hostname = `example${i}.org`;
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci      assertions.push(
501cb0ef41Sopenharmony_ci        assert.rejects(
511cb0ef41Sopenharmony_ci          resolver.resolve4(hostname),
521cb0ef41Sopenharmony_ci          {
531cb0ef41Sopenharmony_ci            code: 'ECANCELLED',
541cb0ef41Sopenharmony_ci            syscall: 'queryA',
551cb0ef41Sopenharmony_ci            hostname: hostname
561cb0ef41Sopenharmony_ci          }
571cb0ef41Sopenharmony_ci        )
581cb0ef41Sopenharmony_ci      );
591cb0ef41Sopenharmony_ci    }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci    await Promise.all(assertions);
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  server.close();
651cb0ef41Sopenharmony_ci}));
66