-
Notifications
You must be signed in to change notification settings - Fork 29.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dgram: improve signature of Socket.prototype.send
- Do not require presence of `address` parameter to use `callback` parameter; `address` is *always* optional - Improve exception messaging if `address` is invalid type - If `address` is an invalid type, guarantee a synchronously thrown exception - Update documentation to reflect signature changes - Add coverage around valid, undocumented types for `address` parameter. - Add coverage around known invalid, but uncovered, types for `address` parameter. PR-URL: #10473 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
- Loading branch information
Showing
6 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const dgram = require('dgram'); | ||
|
||
const port = common.PORT; | ||
const buf = Buffer.from('test'); | ||
const client = dgram.createSocket('udp4'); | ||
|
||
const onMessage = common.mustCall((err, bytes) => { | ||
assert.ifError(err); | ||
assert.strictEqual(bytes, buf.length); | ||
}, 6); | ||
|
||
// valid address: false | ||
client.send(buf, port, false, onMessage); | ||
|
||
// valid address: empty string | ||
client.send(buf, port, '', onMessage); | ||
|
||
// valid address: null | ||
client.send(buf, port, null, onMessage); | ||
|
||
// valid address: 0 | ||
client.send(buf, port, 0, onMessage); | ||
|
||
// valid address: undefined | ||
client.send(buf, port, undefined, onMessage); | ||
|
||
// valid address: not provided | ||
client.send(buf, port, onMessage); | ||
|
||
const expectedError = new RegExp('^TypeError: Invalid arguments: address ' + | ||
'must be a nonempty string or falsy$'); | ||
|
||
// invalid address: object | ||
assert.throws(() => { | ||
client.send(buf, port, []); | ||
}, expectedError); | ||
|
||
// invalid address: nonzero number | ||
assert.throws(() => { | ||
client.send(buf, port, 1); | ||
}, expectedError); | ||
|
||
// invalid address: true | ||
assert.throws(() => { | ||
client.send(buf, port, true); | ||
}, expectedError); | ||
|
||
client.unref(); |
17 changes: 17 additions & 0 deletions
17
test/parallel/test-dgram-send-callback-buffer-empty-address.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const dgram = require('dgram'); | ||
|
||
const client = dgram.createSocket('udp4'); | ||
|
||
const buf = Buffer.alloc(256, 'x'); | ||
|
||
const onMessage = common.mustCall(function(err, bytes) { | ||
assert.ifError(err); | ||
assert.strictEqual(bytes, buf.length); | ||
client.close(); | ||
}); | ||
|
||
client.send(buf, common.PORT, onMessage); |
20 changes: 20 additions & 0 deletions
20
test/parallel/test-dgram-send-callback-buffer-length-empty-address.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const dgram = require('dgram'); | ||
const client = dgram.createSocket('udp4'); | ||
|
||
const buf = Buffer.alloc(256, 'x'); | ||
const offset = 20; | ||
const len = buf.length - offset; | ||
|
||
const onMessage = common.mustCall(function messageSent(err, bytes) { | ||
assert.ifError(err); | ||
assert.notStrictEqual(bytes, buf.length); | ||
assert.strictEqual(bytes, buf.length - offset); | ||
client.close(); | ||
}); | ||
|
||
client.send(buf, offset, len, common.PORT, onMessage); |
28 changes: 28 additions & 0 deletions
28
test/parallel/test-dgram-send-callback-multi-buffer-empty-address.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const dgram = require('dgram'); | ||
|
||
const client = dgram.createSocket('udp4'); | ||
|
||
const messageSent = common.mustCall(function messageSent(err, bytes) { | ||
assert.ifError(err); | ||
assert.strictEqual(bytes, buf1.length + buf2.length); | ||
}); | ||
|
||
const buf1 = Buffer.alloc(256, 'x'); | ||
const buf2 = Buffer.alloc(256, 'y'); | ||
|
||
client.on('listening', function() { | ||
const port = this.address().port; | ||
client.send([buf1, buf2], port, messageSent); | ||
}); | ||
|
||
client.on('message', common.mustCall(function onMessage(buf) { | ||
const expected = Buffer.concat([buf1, buf2]); | ||
assert.ok(buf.equals(expected), 'message was received correctly'); | ||
client.close(); | ||
})); | ||
|
||
client.bind(0); |