|
2 | 2 | 'use strict';
|
3 | 3 | const common = require('../common');
|
4 | 4 | const assert = require('assert');
|
| 5 | +const EventEmitter = require('events'); |
5 | 6 | const dgram = require('dgram');
|
6 | 7 | const dns = require('dns');
|
7 | 8 | const { kStateSymbol } = require('internal/dgram');
|
| 9 | +const mockError = new Error('fake DNS'); |
8 | 10 |
|
9 | 11 | // Monkey patch dns.lookup() so that it always fails.
|
10 | 12 | dns.lookup = function(address, family, callback) {
|
11 |
| - process.nextTick(() => { callback(new Error('fake DNS')); }); |
| 13 | + process.nextTick(() => { callback(mockError); }); |
12 | 14 | };
|
13 | 15 |
|
14 | 16 | const socket = dgram.createSocket('udp4');
|
15 |
| -let dnsFailures = 0; |
16 |
| -let sendFailures = 0; |
17 | 17 |
|
18 |
| -process.on('exit', () => { |
19 |
| - assert.strictEqual(dnsFailures, 3); |
20 |
| - assert.strictEqual(sendFailures, 3); |
21 |
| -}); |
22 |
| - |
23 |
| -socket.on('error', (err) => { |
24 |
| - if (/^Error: fake DNS$/.test(err)) { |
25 |
| - // The DNS lookup should fail since it is monkey patched. At that point in |
26 |
| - // time, the send queue should be populated with the send() operation. There |
27 |
| - // should also be two listeners - this function and the dgram internal one |
28 |
| - // time error handler. |
29 |
| - dnsFailures++; |
30 |
| - assert(Array.isArray(socket[kStateSymbol].queue)); |
31 |
| - assert.strictEqual(socket[kStateSymbol].queue.length, 1); |
32 |
| - assert.strictEqual(socket.listenerCount('error'), 2); |
33 |
| - return; |
34 |
| - } |
35 |
| - |
36 |
| - if (err.code === 'ERR_SOCKET_CANNOT_SEND') { |
37 |
| - // On error, the queue should be destroyed and this function should be |
38 |
| - // the only listener. |
39 |
| - sendFailures++; |
40 |
| - assert.strictEqual(socket[kStateSymbol].queue, undefined); |
41 |
| - assert.strictEqual(socket.listenerCount('error'), 1); |
42 |
| - return; |
43 |
| - } |
44 |
| - |
45 |
| - assert.fail(`Unexpected error: ${err}`); |
46 |
| -}); |
| 18 | +socket.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 | + |
| 26 | +socket.on('error', common.mustCall((err) => { |
| 27 | + assert.strictEqual(err, mockError); |
| 28 | + assert.strictEqual(socket[kStateSymbol].queue, undefined); |
| 29 | +}, 3)); |
47 | 30 |
|
48 | 31 | // Initiate a few send() operations, which will fail.
|
49 | 32 | socket.send('foobar', common.PORT, 'localhost');
|
|
0 commit comments