-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dgram: improve "address" parameter behavior in Socket.prototype.send #10473
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
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); |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, these two files are copypasta from |
||
assert.ifError(err); | ||
assert.notStrictEqual(bytes, buf.length); | ||
assert.strictEqual(bytes, buf.length - offset); | ||
client.close(); | ||
}); | ||
|
||
client.send(buf, offset, len, common.PORT, onMessage); |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed this. Is there any chance that the socket would be unref'ed and the process exit before all six messages are received. I am able to make the test fail artificially by adding a short timeout around the last successful send. It doesn't seem to be a problem on the CI, but wouldn't want it to be a source of future flakiness either.