diff options
Diffstat (limited to '')
-rw-r--r-- | test/socket-back-pressure.js | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/test/socket-back-pressure.js b/test/socket-back-pressure.js new file mode 100644 index 0000000..9e774b3 --- /dev/null +++ b/test/socket-back-pressure.js @@ -0,0 +1,54 @@ +'use strict' + +const { Client } = require('..') +const { createServer } = require('http') +const { Readable } = require('stream') +const { test } = require('tap') + +test('socket back-pressure', (t) => { + t.plan(3) + + const server = createServer() + let bytesWritten = 0 + + const buf = Buffer.allocUnsafe(16384) + const src = new Readable({ + read () { + bytesWritten += buf.length + this.push(buf) + if (bytesWritten >= 1e6) { + this.push(null) + } + } + }) + + server.on('request', (req, res) => { + src.pipe(res) + }) + t.teardown(server.close.bind(server)) + + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + pipelining: 1 + }) + t.teardown(client.destroy.bind(client)) + + client.request({ path: '/', method: 'GET', opaque: 'asd' }, (err, data) => { + t.error(err) + data.body + .resume() + .once('data', () => { + data.body.pause() + // TODO: Try to avoid timeout. + setTimeout(() => { + t.ok(data.body._readableState.length < bytesWritten - data.body._readableState.highWaterMark) + src.push(null) + data.body.resume() + }, 1e3) + }) + .on('end', () => { + t.pass() + }) + }) + }) +}) |