Skip to content

Commit 4c445a8

Browse files
joyeecheungtargos
authored andcommitted
test: skip in test-buffer-tostring-rangeerror on allocation failure
If the buffer allocation fails due to insufficient memory, there is no point continue testing toString(). PR-URL: #58415 Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 53cb298 commit 4c445a8

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

‎test/parallel/test-buffer-tostring-rangeerror.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,22 @@ const message = {
2323
code: 'ERR_STRING_TOO_LONG',
2424
name: 'Error',
2525
};
26-
assert.throws(()=>Buffer(len).toString('utf8'),message);
27-
assert.throws(()=>SlowBuffer(len).toString('utf8'),message);
28-
assert.throws(()=>Buffer.alloc(len).toString('utf8'),message);
29-
assert.throws(()=>Buffer.allocUnsafe(len).toString('utf8'),message);
30-
assert.throws(()=>Buffer.allocUnsafeSlow(len).toString('utf8'),message);
26+
27+
functiontest(getBuffer){
28+
letbuf;
29+
try{
30+
buf=getBuffer();
31+
}catch(e){
32+
// If the buffer allocation fails, we skip the test.
33+
if(e.code==='ERR_MEMORY_ALLOCATION_FAILED'||/Arraybufferallocationfailed/.test(e.message)){
34+
return;
35+
}
36+
}
37+
assert.throws(()=>{buf.toString('utf8');},message);
38+
}
39+
40+
test(()=>Buffer(len));
41+
test(()=>SlowBuffer(len));
42+
test(()=>Buffer.alloc(len));
43+
test(()=>Buffer.allocUnsafe(len));
44+
test(()=>Buffer.allocUnsafeSlow(len));

0 commit comments

Comments
 (0)