Skip to content

Commit 244d0c3

Browse files
Ethan-Arrowoodlpinca
authored andcommitted
test: deflake stream-readable-to-web test
Co-authored-by: Luigi Pinca <luigipinca@gmail.com> PR-URL: #58948Fixes: #58949 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mattias Buelens <mattias@buelens.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com> Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
1 parent c144d69 commit 244d0c3

2 files changed

Lines changed: 64 additions & 62 deletions

File tree

‎test/parallel/test-stream-readable-to-web.js‎

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import{mustCall}from'../common/index.mjs';
2+
import{Readable}from'node:stream';
3+
import{memoryUsage}from'node:process';
4+
importassertfrom'node:assert';
5+
import{setImmediate}from'node:timers/promises';
6+
7+
// Based on: https://github.com/nodejs/node/issues/46347#issuecomment-1413886707
8+
// edit: make it cross-platform as /dev/urandom is not available on Windows
9+
10+
constMAX_MEM=256*1024*1024;// 256 MiB
11+
12+
functioncheckMemoryUsage(){
13+
assert(memoryUsage().arrayBuffers<MAX_MEM);
14+
}
15+
16+
constMAX_BUFFERS=1000;
17+
letbuffersCreated=0;
18+
19+
constrandomNodeStream=newReadable({
20+
read(size){
21+
if(buffersCreated>=MAX_BUFFERS){
22+
this.push(null);
23+
return;
24+
}
25+
26+
this.push(Buffer.alloc(size));
27+
buffersCreated++;
28+
}
29+
});
30+
31+
randomNodeStream.on('error',(err)=>{
32+
assert.fail(err);
33+
});
34+
35+
// Before doing anything, make sure memory usage is okay
36+
checkMemoryUsage();
37+
38+
// Create stream and check memory usage remains okay
39+
40+
constrandomWebStream=Readable.toWeb(randomNodeStream);
41+
42+
checkMemoryUsage();
43+
44+
lettimeout;
45+
try{
46+
// Wait two seconds before consuming the stream to see if memory usage increases
47+
timeout=setTimeout(mustCall(async()=>{
48+
// Did the stream leak memory?
49+
checkMemoryUsage();
50+
// eslint-disable-next-line no-unused-vars
51+
forawait(const_ofrandomWebStream){
52+
// Yield event loop to allow garbage collection
53+
awaitsetImmediate();
54+
// consume the stream
55+
// check memory usage remains okay
56+
checkMemoryUsage();
57+
}
58+
}),2000);
59+
}catch(err){
60+
if(timeout){
61+
clearTimeout(timeout);
62+
}
63+
assert.fail(err);
64+
}

0 commit comments

Comments
 (0)