Skip to content

Commit 6e0ee39

Browse files
lpincaRafaelGSS
authored andcommitted
test: skip the test if the buffer allocation fails
Use the error message as another condition to skip the test when the buffer allocation fails. Refs: 795dd8eb7988ae38553e Refs: e9c6004a2d580008082b PR-URL: #58738 Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 68671f4 commit 6e0ee39

4 files changed

+48
-20
lines changed

test/pummel/test-buffer-large-size-buffer-alloc-unsafe-slow.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ common.skipIf32Bits();
77
const assert = require('node:assert');
88
const size = 2 ** 31;
99

10+
let largeBuffer;
11+
1012
// Test Buffer.allocUnsafe with size larger than integer range
1113
try {
12-
assert.throws(() => Buffer.allocUnsafeSlow(size).toString('utf8'), { code: 'ERR_STRING_TOO_LONG' });
14+
largeBuffer = Buffer.allocUnsafeSlow(size);
1315
} catch (e) {
14-
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
15-
throw e;
16+
if (
17+
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
18+
/Array buffer allocation failed/.test(e.message)
19+
) {
20+
common.skip('insufficient space for Buffer.allocUnsafeSlow');
1621
}
17-
common.skip('insufficient space for Buffer.allocUnsafeSlow');
22+
23+
throw e;
1824
}
25+
26+
assert.throws(() => largeBuffer.toString('utf8'), {
27+
code: 'ERR_STRING_TOO_LONG',
28+
});

test/pummel/test-buffer-large-size-buffer-write.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@ const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH;
99

1010
const size = 2 ** 31;
1111

12-
// Test Buffer.write with size larger than integer range
12+
let largeBuffer;
13+
1314
try {
14-
const buf = Buffer.alloc(size);
15-
assert.strictEqual(buf.write('a', 2, kStringMaxLength), 1);
16-
assert.strictEqual(buf.write('a', 2, size), 1);
15+
largeBuffer = Buffer.alloc(size);
1716
} catch (e) {
18-
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
19-
throw e;
17+
if (
18+
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
19+
/Array buffer allocation failed/.test(e.message)
20+
) {
21+
common.skip('insufficient space for Buffer.alloc');
2022
}
21-
common.skip('insufficient space for Buffer.alloc');
23+
24+
throw e;
2225
}
26+
27+
// Test Buffer.write with size larger than integer range
28+
assert.strictEqual(largeBuffer.write('a', 2, kStringMaxLength), 1);
29+
assert.strictEqual(largeBuffer.write('a', 2, size), 1);

test/pummel/test-buffer-large-size-slowbuffer.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ const size = 2 ** 31;
1515
try {
1616
assert.throws(() => SlowBuffer(size).toString('utf8'), { code: 'ERR_STRING_TOO_LONG' });
1717
} catch (e) {
18-
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
19-
throw e;
18+
if (
19+
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
20+
/Array buffer allocation failed/.test(e.message)
21+
) {
22+
common.skip('insufficient space for slow Buffer allocation');
2023
}
21-
common.skip('insufficient space for SlowBuffer');
24+
25+
throw e;
2226
}

test/pummel/test-string-decoder-large-buffer.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ const stringTooLongError = {
1717
name: 'Error',
1818
};
1919

20+
let largeBuffer;
21+
2022
try {
21-
const buf = Buffer.allocUnsafe(size);
22-
const decoder = new StringDecoder('utf8');
23-
assert.throws(() => decoder.write(buf), stringTooLongError);
23+
largeBuffer = Buffer.allocUnsafe(size);
2424
} catch (e) {
25-
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
26-
throw e;
25+
if (
26+
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
27+
/Array buffer allocation failed/.test(e.message)
28+
) {
29+
common.skip('insufficient space for Buffer.allocUnsafe');
2730
}
28-
common.skip('insufficient space for Buffer.alloc');
31+
32+
throw e;
2933
}
34+
35+
const decoder = new StringDecoder('utf8');
36+
assert.throws(() => decoder.write(largeBuffer), stringTooLongError);

0 commit comments

Comments
 (0)