• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3const common = require('../common');
4const assert = require('assert');
5const EventEmitter = require('events');
6const dgram = require('dgram');
7const dns = require('dns');
8const { kStateSymbol } = require('internal/dgram');
9const mockError = new Error('fake DNS');
10
11// Monkey patch dns.lookup() so that it always fails.
12dns.lookup = function(address, family, callback) {
13  process.nextTick(() => { callback(mockError); });
14};
15
16const socket = dgram.createSocket('udp4');
17
18socket.on(EventEmitter.errorMonitor, common.mustCall((err) => {
19  // The DNS lookup should fail since it is monkey patched. At that point in
20  // time, the send queue should be populated with the send() operation.
21  assert.strictEqual(err, mockError);
22  assert(Array.isArray(socket[kStateSymbol].queue));
23  assert.strictEqual(socket[kStateSymbol].queue.length, 1);
24}, 3));
25
26socket.on('error', common.mustCall((err) => {
27  assert.strictEqual(err, mockError);
28  assert.strictEqual(socket[kStateSymbol].queue, undefined);
29}, 3));
30
31// Initiate a few send() operations, which will fail.
32socket.send('foobar', common.PORT, 'localhost');
33
34process.nextTick(() => {
35  socket.send('foobar', common.PORT, 'localhost');
36});
37
38setImmediate(() => {
39  socket.send('foobar', common.PORT, 'localhost');
40});
41