-
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.
This commit adds `bufferSize` for `Http2Stream`. Refs: #21631 PR-URL: #23711 Reviewed-By: James M Snell <[email protected]>
- Loading branch information
Showing
4 changed files
with
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const { mustCall, hasCrypto, skip } = require('../common'); | ||
if (!hasCrypto) | ||
skip('missing crypto'); | ||
const assert = require('assert'); | ||
const { createServer, connect } = require('http2'); | ||
const Countdown = require('../common/countdown'); | ||
|
||
// This test ensures that `bufferSize` of Http2Session and Http2Stream work | ||
// as expected. | ||
{ | ||
const SOCKETS = 2; | ||
const TIMES = 10; | ||
const BUFFER_SIZE = 30; | ||
const server = createServer(); | ||
|
||
// Other `bufferSize` tests for net module and tls module | ||
// don't assert `bufferSize` of server-side sockets. | ||
server.on('stream', mustCall((stream) => { | ||
stream.on('data', mustCall()); | ||
stream.on('end', mustCall()); | ||
}, SOCKETS)); | ||
|
||
server.listen(0, mustCall(() => { | ||
const authority = `http://localhost:${server.address().port}`; | ||
const client = connect(authority); | ||
const countdown = new Countdown(SOCKETS, () => { | ||
client.close(); | ||
server.close(); | ||
}); | ||
|
||
client.once('connect', mustCall()); | ||
|
||
for (let j = 0; j < SOCKETS; j += 1) { | ||
const stream = client.request({ ':method': 'POST' }); | ||
stream.on('data', () => {}); | ||
stream.on('close', mustCall(() => { | ||
countdown.dec(); | ||
})); | ||
|
||
for (let i = 0; i < TIMES; i += 1) { | ||
stream.write(Buffer.allocUnsafe(BUFFER_SIZE), mustCall()); | ||
const expectedSocketBufferSize = BUFFER_SIZE * (i + 1); | ||
assert.strictEqual(stream.bufferSize, expectedSocketBufferSize); | ||
} | ||
stream.end(); | ||
stream.close(); | ||
} | ||
})); | ||
} |
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,72 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
const assert = require('assert'); | ||
const fixtures = require('../common/fixtures'); | ||
const makeDuplexPair = require('../common/duplexpair'); | ||
const tls = require('tls'); | ||
const net = require('net'); | ||
|
||
// This test ensures that `bufferSize` also works for those tlsSockets | ||
// created from `socket` of `Duplex`, with which, TLSSocket will wrap | ||
// sockets in `StreamWrap`. | ||
{ | ||
const iter = 10; | ||
|
||
function createDuplex(port) { | ||
const { clientSide, serverSide } = makeDuplexPair(); | ||
|
||
return new Promise((resolve, reject) => { | ||
const socket = net.connect({ | ||
port, | ||
}, common.mustCall(() => { | ||
clientSide.pipe(socket); | ||
socket.pipe(clientSide); | ||
clientSide.on('close', common.mustCall(() => socket.destroy())); | ||
socket.on('close', common.mustCall(() => clientSide.destroy())); | ||
|
||
resolve(serverSide); | ||
})); | ||
}); | ||
} | ||
|
||
const server = tls.createServer({ | ||
key: fixtures.readKey('agent2-key.pem'), | ||
cert: fixtures.readKey('agent2-cert.pem') | ||
}, common.mustCall((socket) => { | ||
let str = ''; | ||
socket.setEncoding('utf-8'); | ||
socket.on('data', (chunk) => { str += chunk; }); | ||
|
||
socket.on('end', common.mustCall(() => { | ||
assert.strictEqual(str, 'a'.repeat(iter - 1)); | ||
server.close(); | ||
})); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const { port } = server.address(); | ||
createDuplex(port).then((socket) => { | ||
const client = tls.connect({ | ||
socket, | ||
rejectUnauthorized: false, | ||
}, common.mustCall(() => { | ||
assert.strictEqual(client.bufferSize, 0); | ||
|
||
for (let i = 1; i < iter; i++) { | ||
client.write('a'); | ||
assert.strictEqual(client.bufferSize, i + 1); | ||
} | ||
|
||
// It seems that tlsSockets created from sockets of `Duplex` emit no | ||
// "finish" events. We use "end" event instead. | ||
client.on('end', common.mustCall(() => { | ||
assert.strictEqual(client.bufferSize, undefined); | ||
})); | ||
|
||
client.end(); | ||
})); | ||
}); | ||
})); | ||
} |