diff options
author | cjihrig <cjihrig@gmail.com> | 2020-02-25 20:18:35 -0500 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2020-02-27 21:01:32 -0500 |
commit | 1b2e2944bc069abd1dd7cbf90d3badad4289235d (patch) | |
tree | 4718ec4f0f9450208738d99dd33a3075ebe6b3c3 /test/sequential/test-dgram-implicit-bind-failure.js | |
parent | fb26b136238240eeac1b3ede098e097a343aef0b (diff) | |
download | node-new-1b2e2944bc069abd1dd7cbf90d3badad4289235d.tar.gz |
dgram: don't hide implicit bind errors
When dgram socket implicit binding fails, an attempt is made to
clean up the send queue. This was originally implemented using
an 'error' handler that performed cleanup and then emitted a
fake error, which concealed the original error. This was done
to prevent cases where the same error was emitted twice. Now
that the errorMonitor event is available, use that to perform
the cleanup without impacting the actual error handling.
PR-URL: https://github.com/nodejs/node/pull/31958
Refs: https://github.com/nodejs/help/issues/2484
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/sequential/test-dgram-implicit-bind-failure.js')
-rw-r--r-- | test/sequential/test-dgram-implicit-bind-failure.js | 47 |
1 files changed, 15 insertions, 32 deletions
diff --git a/test/sequential/test-dgram-implicit-bind-failure.js b/test/sequential/test-dgram-implicit-bind-failure.js index d77db12618..89da00d576 100644 --- a/test/sequential/test-dgram-implicit-bind-failure.js +++ b/test/sequential/test-dgram-implicit-bind-failure.js @@ -2,48 +2,31 @@ 'use strict'; const common = require('../common'); const assert = require('assert'); +const EventEmitter = require('events'); const dgram = require('dgram'); const dns = require('dns'); const { kStateSymbol } = require('internal/dgram'); +const mockError = new Error('fake DNS'); // Monkey patch dns.lookup() so that it always fails. dns.lookup = function(address, family, callback) { - process.nextTick(() => { callback(new Error('fake DNS')); }); + process.nextTick(() => { callback(mockError); }); }; const socket = dgram.createSocket('udp4'); -let dnsFailures = 0; -let sendFailures = 0; -process.on('exit', () => { - assert.strictEqual(dnsFailures, 3); - assert.strictEqual(sendFailures, 3); -}); - -socket.on('error', (err) => { - if (/^Error: fake DNS$/.test(err)) { - // The DNS lookup should fail since it is monkey patched. At that point in - // time, the send queue should be populated with the send() operation. There - // should also be two listeners - this function and the dgram internal one - // time error handler. - dnsFailures++; - assert(Array.isArray(socket[kStateSymbol].queue)); - assert.strictEqual(socket[kStateSymbol].queue.length, 1); - assert.strictEqual(socket.listenerCount('error'), 2); - return; - } - - if (err.code === 'ERR_SOCKET_CANNOT_SEND') { - // On error, the queue should be destroyed and this function should be - // the only listener. - sendFailures++; - assert.strictEqual(socket[kStateSymbol].queue, undefined); - assert.strictEqual(socket.listenerCount('error'), 1); - return; - } - - assert.fail(`Unexpected error: ${err}`); -}); +socket.on(EventEmitter.errorMonitor, common.mustCall((err) => { + // The DNS lookup should fail since it is monkey patched. At that point in + // time, the send queue should be populated with the send() operation. + assert.strictEqual(err, mockError); + assert(Array.isArray(socket[kStateSymbol].queue)); + assert.strictEqual(socket[kStateSymbol].queue.length, 1); +}, 3)); + +socket.on('error', common.mustCall((err) => { + assert.strictEqual(err, mockError); + assert.strictEqual(socket[kStateSymbol].queue, undefined); +}, 3)); // Initiate a few send() operations, which will fail. socket.send('foobar', common.PORT, 'localhost'); |