forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-blob-stream-gc.js
More file actions
56 lines (45 loc) Β· 1.8 KB
/
Copy pathtest-blob-stream-gc.js
File metadata and controls
56 lines (45 loc) Β· 1.8 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
55
56
// Flags: --expose-gc --no-concurrent-array-buffer-sweeping
'use strict';
const common = require('../common');
const assert = require('assert');
const { setImmediate: setImmediatePromise } = require('timers/promises');
const MiB = 1024 * 1024;
const iterations = 64;
const maxRetained = 16 * MiB;
async function collectArrayBuffers() {
for (let i = 0; i < 3; i++) {
global.gc();
await setImmediatePromise();
}
}
async function assertNoBlobStreamRetention(name, fn) {
const buffer = Buffer.alloc(MiB);
await collectArrayBuffers();
const before = process.memoryUsage().arrayBuffers;
for (let i = 0; i < iterations; i++) {
await fn(buffer);
}
await collectArrayBuffers();
const retained = process.memoryUsage().arrayBuffers - before;
assert(
retained < maxRetained,
`${name} retained ${retained} bytes in arrayBuffers`,
);
}
(async () => {
await assertNoBlobStreamRetention('unused Blob streams',
common.mustCall(async (buffer) => {
new Blob([buffer]).stream();
}, iterations));
await assertNoBlobStreamRetention('cancelled Blob streams',
common.mustCall(async (buffer) => {
await new Blob([buffer]).stream()
.cancel();
}, iterations));
await assertNoBlobStreamRetention('drained Blob streams',
common.mustCall(async (buffer) => {
await new Response(
new Blob([buffer]).stream(),
).arrayBuffer();
}, iterations));
})().then(common.mustCall());