Skip to content

Commit 920046f

Browse files
committed
test: deflake test-buffer-large-size
1 parent 77f88b9 commit 920046f

5 files changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Buffer with size > INT32_MAX
5+
common.skipIf32Bits();
6+
7+
// Test Buffer size larger than integer range
8+
const { test } = require('node:test');
9+
const assert = require('assert');
10+
const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH;
11+
12+
const stringTooLongError = {
13+
message: `Cannot create a string longer than 0x${kStringMaxLength.toString(16)}` +
14+
' characters',
15+
code: 'ERR_STRING_TOO_LONG',
16+
name: 'Error',
17+
};
18+
19+
const size = 2 ** 31;
20+
21+
// Test Buffer.toString
22+
test('Buffer.allocUnsafeSlow with too long size', () => {
23+
try {
24+
assert.throws(() => Buffer.allocUnsafeSlow(size).toString('utf8'), stringTooLongError);
25+
} catch (e) {
26+
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
27+
throw e;
28+
}
29+
common.skip('insufficient space for Buffer.alloc');
30+
}
31+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Buffer with size > INT32_MAX
5+
common.skipIf32Bits();
6+
7+
// Test Buffer size larger than integer range
8+
const { test } = require('node:test');
9+
const assert = require('assert');
10+
const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH;
11+
12+
const stringTooLongError = {
13+
message: `Cannot create a string longer than 0x${kStringMaxLength.toString(16)}` +
14+
' characters',
15+
code: 'ERR_STRING_TOO_LONG',
16+
name: 'Error',
17+
};
18+
19+
const size = 2 ** 31;
20+
21+
// Test Buffer.toString
22+
test('Buffer.allocUnsafe with too long size', () => {
23+
try {
24+
assert.throws(() => Buffer.allocUnsafe(size).toString('utf8'), stringTooLongError);
25+
} catch (e) {
26+
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
27+
throw e;
28+
}
29+
common.skip('insufficient space for Buffer.alloc');
30+
}
31+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Buffer with size > INT32_MAX
5+
common.skipIf32Bits();
6+
7+
// Test Buffer size larger than integer range
8+
const { test } = require('node:test');
9+
const assert = require('assert');
10+
const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH;
11+
12+
const stringTooLongError = {
13+
message: `Cannot create a string longer than 0x${kStringMaxLength.toString(16)}` +
14+
' characters',
15+
code: 'ERR_STRING_TOO_LONG',
16+
name: 'Error',
17+
};
18+
19+
const size = 2 ** 31;
20+
21+
// Test Buffer.toString
22+
test('Buffer.allocwith too long size', () => {
23+
try {
24+
assert.throws(() => Buffer.alloc(size).toString('utf8'), stringTooLongError);
25+
} catch (e) {
26+
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
27+
throw e;
28+
}
29+
common.skip('insufficient space for Buffer.alloc');
30+
}
31+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Buffer with size > INT32_MAX
5+
common.skipIf32Bits();
6+
7+
// Test Buffer size larger than integer range
8+
const { test } = require('node:test');
9+
const assert = require('assert');
10+
const {
11+
SlowBuffer,
12+
} = require('buffer');
13+
const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH;
14+
15+
const stringTooLongError = {
16+
message: `Cannot create a string longer than 0x${kStringMaxLength.toString(16)}` +
17+
' characters',
18+
code: 'ERR_STRING_TOO_LONG',
19+
name: 'Error',
20+
};
21+
22+
const size = 2 ** 31;
23+
24+
// Test Buffer.toString
25+
test('Buffer.toString with too long size', () => {
26+
try {
27+
assert.throws(() => SlowBuffer(size).toString('utf8'), stringTooLongError);
28+
} catch (e) {
29+
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
30+
throw e;
31+
}
32+
common.skip('insufficient space for Buffer.alloc');
33+
}
34+
});

0 commit comments

Comments
 (0)