11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst EventEmitter = require('events');
61cb0ef41Sopenharmony_ciconst dgram = require('dgram');
71cb0ef41Sopenharmony_ciconst dns = require('dns');
81cb0ef41Sopenharmony_ciconst { kStateSymbol } = require('internal/dgram');
91cb0ef41Sopenharmony_ciconst mockError = new Error('fake DNS');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// Monkey patch dns.lookup() so that it always fails.
121cb0ef41Sopenharmony_cidns.lookup = function(address, family, callback) {
131cb0ef41Sopenharmony_ci  process.nextTick(() => { callback(mockError); });
141cb0ef41Sopenharmony_ci};
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst socket = dgram.createSocket('udp4');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cisocket.on(EventEmitter.errorMonitor, common.mustCall((err) => {
191cb0ef41Sopenharmony_ci  // The DNS lookup should fail since it is monkey patched. At that point in
201cb0ef41Sopenharmony_ci  // time, the send queue should be populated with the send() operation.
211cb0ef41Sopenharmony_ci  assert.strictEqual(err, mockError);
221cb0ef41Sopenharmony_ci  assert(Array.isArray(socket[kStateSymbol].queue));
231cb0ef41Sopenharmony_ci  assert.strictEqual(socket[kStateSymbol].queue.length, 1);
241cb0ef41Sopenharmony_ci}, 3));
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cisocket.on('error', common.mustCall((err) => {
271cb0ef41Sopenharmony_ci  assert.strictEqual(err, mockError);
281cb0ef41Sopenharmony_ci  assert.strictEqual(socket[kStateSymbol].queue, undefined);
291cb0ef41Sopenharmony_ci}, 3));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci// Initiate a few send() operations, which will fail.
321cb0ef41Sopenharmony_cisocket.send('foobar', common.PORT, 'localhost');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciprocess.nextTick(() => {
351cb0ef41Sopenharmony_ci  socket.send('foobar', common.PORT, 'localhost');
361cb0ef41Sopenharmony_ci});
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cisetImmediate(() => {
391cb0ef41Sopenharmony_ci  socket.send('foobar', common.PORT, 'localhost');
401cb0ef41Sopenharmony_ci});
41