forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull-dont-push.js
More file actions
54 lines (43 loc) · 1.29 KB
/
pull-dont-push.js
File metadata and controls
54 lines (43 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict'
const { test } = require('node:test')
const assert = require('node:assert')
const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')
const { Readable, pipeline } = require('stream')
const { setTimeout: sleep } = require('timers/promises')
const { closeServerAsPromise } = require('../utils/node-http')
test('pull dont\'t push', async (t) => {
let count = 0
let socket
const max = 1_000_000
const server = createServer((req, res) => {
res.statusCode = 200
socket = res.socket
// infinite stream
const stream = new Readable({
read () {
this.push('a')
if (count++ > max) {
this.push(null)
}
}
})
pipeline(stream, res, () => {})
})
t.after(closeServerAsPromise(server))
server.listen(0)
await once(server, 'listening')
const res = await fetch(`http://localhost:${server.address().port}`)
// Some time is needed to fill the buffer
await sleep(1000)
socket.destroy()
assert.strictEqual(count < max, true) // the stream should be closed before the max
// consume the stream
try {
/* eslint-disable-next-line no-empty, no-unused-vars */
for await (const chunk of res.body) {
// process._rawDebug('chunk', chunk)
}
} catch {}
})