From e2131ec5278c5a46bae42e1ecbabf20e044d5c08 Mon Sep 17 00:00:00 2001 From: abouthiroppy Date: Fri, 13 Jan 2017 11:05:35 +0900 Subject: [PATCH] test: update coverage for dgram.js Create test-dgram-multicast-loopback.js for dgram.setMulticastLoopback. Add a test which has an argument of an invalid number to test-dgram-setTTL.js. https://github.com/nodejs/node/blob/master/lib/dgram.js#L464 Add a test which has an argument of a string to test-dgram-multicast-setTTL.js. https://github.com/nodejs/node/blob/master/lib/dgram.js#L473 --- .../parallel/test-dgram-multicast-loopback.js | 12 +++++++++++ test/parallel/test-dgram-multicast-setTTL.js | 20 +++++++++---------- test/parallel/test-dgram-setTTL.js | 15 +++++++++----- 3 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 test/parallel/test-dgram-multicast-loopback.js diff --git a/test/parallel/test-dgram-multicast-loopback.js b/test/parallel/test-dgram-multicast-loopback.js new file mode 100644 index 00000000000000..01def7e28160cd --- /dev/null +++ b/test/parallel/test-dgram-multicast-loopback.js @@ -0,0 +1,12 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const dgram = require('dgram'); +const socket = dgram.createSocket('udp4'); + +socket.bind(0); +socket.on('listening', common.mustCall(() => { + const result = socket.setMulticastLoopback(16); + assert.strictEqual(result, 16); + socket.close(); +})); diff --git a/test/parallel/test-dgram-multicast-setTTL.js b/test/parallel/test-dgram-multicast-setTTL.js index 83d482f426bcd4..11b5a0a7635857 100644 --- a/test/parallel/test-dgram-multicast-setTTL.js +++ b/test/parallel/test-dgram-multicast-setTTL.js @@ -1,23 +1,23 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const dgram = require('dgram'); const socket = dgram.createSocket('udp4'); -let thrown = false; socket.bind(0); -socket.on('listening', function() { - socket.setMulticastTTL(16); +socket.on('listening', common.mustCall(() => { + const result = socket.setMulticastTTL(16); + assert.strictEqual(result, 16); //Try to set an invalid TTL (valid ttl is > 0 and < 256) - try { + assert.throws(() => { socket.setMulticastTTL(1000); - } catch (e) { - thrown = true; - } + }, /^Error: setMulticastTTL EINVAL$/); - assert(thrown, 'Setting an invalid multicast TTL should throw some error'); + assert.throws(() => { + socket.setMulticastTTL('foo'); + }, /^TypeError: Argument must be a number$/); //close the socket socket.close(); -}); +})); diff --git a/test/parallel/test-dgram-setTTL.js b/test/parallel/test-dgram-setTTL.js index 3985297217d8b7..7da3975ad4c3fd 100644 --- a/test/parallel/test-dgram-setTTL.js +++ b/test/parallel/test-dgram-setTTL.js @@ -1,17 +1,22 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const dgram = require('dgram'); const socket = dgram.createSocket('udp4'); socket.bind(0); -socket.on('listening', function() { +socket.on('listening', common.mustCall(() => { const result = socket.setTTL(16); assert.strictEqual(result, 16); - assert.throws(function() { + assert.throws(() => { socket.setTTL('foo'); - }, /Argument must be a number/); + }, /^TypeError: Argument must be a number$/); + + // TTL must be a number from > 0 to < 256 + assert.throws(() => { + socket.setTTL(1000); + }, /^Error: setTTL EINVAL$/); socket.close(); -}); +}));