-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dgram: support for setting socket buffer size
* setRecvBufferSize(int) and setSendBufferSize(int) * added docs for send/receive buffer sizes * Added options support to set buffer sizes in dgram.createSocket(). PR-URL: #13623 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
- Loading branch information
1 parent
f8805c4
commit e365814
Showing
6 changed files
with
218 additions
and
0 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
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,74 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const dgram = require('dgram'); | ||
|
||
{ | ||
// Should throw error if the socket is never bound. | ||
const errorObj = { | ||
code: 'ERR_SOCKET_BUFFER_SIZE', | ||
type: Error, | ||
message: /^Could not get or set buffer size:.*$/ | ||
}; | ||
|
||
const socket = dgram.createSocket('udp4'); | ||
|
||
assert.throws(() => { | ||
socket.setRecvBufferSize(8192); | ||
}, common.expectsError(errorObj)); | ||
|
||
assert.throws(() => { | ||
socket.setSendBufferSize(8192); | ||
}, common.expectsError(errorObj)); | ||
|
||
assert.throws(() => { | ||
socket.getRecvBufferSize(); | ||
}, common.expectsError(errorObj)); | ||
|
||
assert.throws(() => { | ||
socket.getSendBufferSize(); | ||
}, common.expectsError(errorObj)); | ||
} | ||
|
||
{ | ||
// Should throw error if invalid buffer size is specified | ||
const errorObj = { | ||
code: 'ERR_SOCKET_BAD_BUFFER_SIZE', | ||
type: TypeError, | ||
message: /^Buffer size must be a positive integer$/ | ||
}; | ||
|
||
const badBufferSizes = [-1, Infinity, 'Doh!']; | ||
|
||
const socket = dgram.createSocket('udp4'); | ||
|
||
socket.bind(common.mustCall(() => { | ||
badBufferSizes.forEach((badBufferSize) => { | ||
assert.throws(() => { | ||
socket.setRecvBufferSize(badBufferSize); | ||
}, common.expectsError(errorObj)); | ||
|
||
assert.throws(() => { | ||
socket.setSendBufferSize(badBufferSize); | ||
}, common.expectsError(errorObj)); | ||
}); | ||
socket.close(); | ||
})); | ||
} | ||
|
||
{ | ||
// Can set and get buffer sizes after binding the socket. | ||
const socket = dgram.createSocket('udp4'); | ||
|
||
socket.bind(common.mustCall(() => { | ||
socket.setRecvBufferSize(10000); | ||
socket.setSendBufferSize(10000); | ||
|
||
// note: linux will double the buffer size | ||
const expectedBufferSize = common.isLinux ? 20000 : 10000; | ||
assert.strictEqual(socket.getRecvBufferSize(), expectedBufferSize); | ||
assert.strictEqual(socket.getSendBufferSize(), expectedBufferSize); | ||
socket.close(); | ||
})); | ||
} |