From 7ff32bf705ca92ac5aa72b23ba510618444689cb Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 8 Feb 2017 16:17:27 -0500 Subject: [PATCH] test: add coverage for dgram send() errors This commit adds code coverage for emitted and callback errors for dgram's Socket#send() method. PR-URL: https://github.com/nodejs/node/pull/11248 Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- test/parallel/test-dgram-send-error.js | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/parallel/test-dgram-send-error.js diff --git a/test/parallel/test-dgram-send-error.js b/test/parallel/test-dgram-send-error.js new file mode 100644 index 00000000000000..3cec72cca0718d --- /dev/null +++ b/test/parallel/test-dgram-send-error.js @@ -0,0 +1,37 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const dgram = require('dgram'); +const mockError = new Error('mock DNS error'); + +function getSocket(callback) { + const socket = dgram.createSocket('udp4'); + + socket.on('message', common.mustNotCall('Should not receive any messages.')); + socket.bind(common.mustCall(() => { + socket._handle.lookup = function(address, callback) { + process.nextTick(callback, mockError); + }; + + callback(socket); + })); + return socket; +} + +getSocket((socket) => { + socket.on('error', common.mustCall((err) => { + socket.close(); + assert.strictEqual(err, mockError); + })); + + socket.send('foo', socket.address().port, 'localhost'); +}); + +getSocket((socket) => { + const callback = common.mustCall((err) => { + socket.close(); + assert.strictEqual(err, mockError); + }); + + socket.send('foo', socket.address().port, 'localhost', callback); +});