11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst mustCall = common.mustCall;
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst dgram = require('dgram');
61cb0ef41Sopenharmony_ciconst dns = require('dns');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst socket = dgram.createSocket('udp4');
91cb0ef41Sopenharmony_ciconst buffer = Buffer.from('gary busey');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cidns.setServers([]);
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cisocket.once('error', onEvent);
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// assert that:
161cb0ef41Sopenharmony_ci// * callbacks act as "error" listeners if given.
171cb0ef41Sopenharmony_ci// * error is never emitter for missing dns entries
181cb0ef41Sopenharmony_ci//   if a callback that handles error is present
191cb0ef41Sopenharmony_ci// * error is emitted if a callback with no argument is passed
201cb0ef41Sopenharmony_cisocket.send(buffer, 0, buffer.length, 100,
211cb0ef41Sopenharmony_ci            'dne.example.com', mustCall(callbackOnly));
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cifunction callbackOnly(err) {
241cb0ef41Sopenharmony_ci  assert.ok(err);
251cb0ef41Sopenharmony_ci  socket.removeListener('error', onEvent);
261cb0ef41Sopenharmony_ci  socket.on('error', mustCall(onError));
271cb0ef41Sopenharmony_ci  socket.send(buffer, 0, buffer.length, 100, 'dne.invalid');
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cifunction onEvent(err) {
311cb0ef41Sopenharmony_ci  assert.fail(`Error should not be emitted if there is callback: ${err}`);
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cifunction onError(err) {
351cb0ef41Sopenharmony_ci  assert.ok(err);
361cb0ef41Sopenharmony_ci  socket.close();
371cb0ef41Sopenharmony_ci}
38