Skip to content

Commit bffd4ec

Browse files
joyeecheungmarco-ippolito
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 7c449ed commit bffd4ec

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,22 @@ const message = {
1818
code: 'ERR_STRING_TOO_LONG',
1919
name: 'Error',
2020
};
21-
assert.throws(() => Buffer(len).toString('utf8'), message);
22-
assert.throws(() => SlowBuffer(len).toString('utf8'), message);
23-
assert.throws(() => Buffer.alloc(len).toString('utf8'), message);
24-
assert.throws(() => Buffer.allocUnsafe(len).toString('utf8'), message);
25-
assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8'), message);
21+
22+
function test(getBuffer) {
23+
let buf;
24+
try {
25+
buf = getBuffer();
26+
} catch (e) {
27+
// If the buffer allocation fails, we skip the test.
28+
if (e.code === 'ERR_MEMORY_ALLOCATION_FAILED' || /Array buffer allocation failed/.test(e.message)) {
29+
return;
30+
}
31+
}
32+
assert.throws(() => { buf.toString('utf8'); }, message);
33+
}
34+
35+
test(() => Buffer(len));
36+
test(() => SlowBuffer(len));
37+
test(() => Buffer.alloc(len));
38+
test(() => Buffer.allocUnsafe(len));
39+
test(() => Buffer.allocUnsafeSlow(len));

0 commit comments

Comments
 (0)