package-rebuilds/pkgs/node-undici/node-undici-5.28.4+dfsg1+~cs23.12.11/test/socket-back-pressure.js
Fabian Grünbichler afee63929c add node-undici for libgit2 1.8.1
Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
2024-09-10 11:23:24 +02:00

55 lines
1.3 KiB
JavaScript

'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()
})
})
})
})