test: deflake test-buffer-large-size-buffer-alloc-unsafe

Use the error message as another condition to skip the test when the
buffer allocation fails.

Refs: https://github.com/nodejs/node/pull/58738
PR-URL: https://github.com/nodejs/node/pull/58771
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Dario Piotrowicz <dario.piotrowicz@gmail.com>
This commit is contained in:
Luigi Pinca 2025-06-22 11:43:35 +02:00 committed by GitHub
parent dfcb824ae3
commit d7becc5875
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,12 +7,22 @@ common.skipIf32Bits();
const assert = require('node:assert');
const size = 2 ** 31;
let largeBuffer;
// Test Buffer.allocUnsafe with size larger than integer range
try {
// Test Buffer.allocUnsafe with size larger than integer range
assert.throws(() => Buffer.allocUnsafe(size).toString('utf8'), { code: 'ERR_STRING_TOO_LONG' });
largeBuffer = Buffer.allocUnsafe(size);
} catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e;
}
if (
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)
) {
common.skip('insufficient space for Buffer.allocUnsafe');
}
throw e;
}
assert.throws(() => largeBuffer.toString('utf8'), {
code: 'ERR_STRING_TOO_LONG',
});