Skip to content

Commit 6c1d82c

Browse files
hiroppyMylesBorins
authored andcommitted
test: improving coverage for dgram
PR-URL: #10783 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 017afd4 commit 6c1d82c

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const dgram = require('dgram');
5+
const socket = dgram.createSocket('udp4');
6+
7+
socket.bind(0);
8+
socket.on('listening', common.mustCall(() => {
9+
const result = socket.setMulticastLoopback(16);
10+
assert.strictEqual(result, 16);
11+
socket.close();
12+
}));
+10-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const dgram = require('dgram');
55
const socket = dgram.createSocket('udp4');
6-
let thrown = false;
76

87
socket.bind(0);
9-
socket.on('listening', function() {
10-
socket.setMulticastTTL(16);
8+
socket.on('listening', common.mustCall(() => {
9+
const result = socket.setMulticastTTL(16);
10+
assert.strictEqual(result, 16);
1111

1212
//Try to set an invalid TTL (valid ttl is > 0 and < 256)
13-
try {
13+
assert.throws(() => {
1414
socket.setMulticastTTL(1000);
15-
} catch (e) {
16-
thrown = true;
17-
}
15+
}, /^Error: setMulticastTTL EINVAL$/);
1816

19-
assert(thrown, 'Setting an invalid multicast TTL should throw some error');
17+
assert.throws(() => {
18+
socket.setMulticastTTL('foo');
19+
}, /^TypeError: Argument must be a number$/);
2020

2121
//close the socket
2222
socket.close();
23-
});
23+
}));

test/parallel/test-dgram-setTTL.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const dgram = require('dgram');
55
const socket = dgram.createSocket('udp4');
66

77
socket.bind(0);
8-
socket.on('listening', function() {
9-
var result = socket.setTTL(16);
8+
socket.on('listening', common.mustCall(() => {
9+
const result = socket.setTTL(16);
1010
assert.strictEqual(result, 16);
1111

12-
assert.throws(function() {
12+
assert.throws(() => {
1313
socket.setTTL('foo');
14-
}, /Argument must be a number/);
14+
}, /^TypeError: Argument must be a number$/);
15+
16+
// TTL must be a number from > 0 to < 256
17+
assert.throws(() => {
18+
socket.setTTL(1000);
19+
}, /^Error: setTTL EINVAL$/);
1520

1621
socket.close();
17-
});
22+
}));

0 commit comments

Comments
 (0)