node/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js
Ali Ijaz Sheikh f4ebd5989a test: fix flakiness of stringbytes-external
The tests used to rely on precise timing of when a JavaScript object
would be garbage collected to ensure that there is enough memory
available on the system. Switch the test to use a malloc/free pair
instead.

Ref: https://github.com/nodejs/node/pull/5945
PR-URL: https://github.com/nodejs/node/pull/6039
Reviewed-By: jasnell - James M Snell <jasnell@gmail.com>
Reviewed-By: evanlucas - Evan Lucas <evanlucas@me.com>
Reviewed-By: Trott - Rich Trott <rtrott@gmail.com>
2016-04-05 16:08:45 -07:00

40 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../../common');
const binding = require('./build/Release/binding');
const assert = require('assert');
const skipMessage =
'1..0 # Skipped: intensive toString tests due to memory confinements';
if (!common.enoughTestMem) {
console.log(skipMessage);
return;
}
// v8 fails silently if string length > v8::String::kMaxLength
// v8::String::kMaxLength defined in v8.h
const kStringMaxLength = process.binding('buffer').kStringMaxLength;
try {
var buf = Buffer.allocUnsafe(kStringMaxLength + 1);
} catch (e) {
// If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Array buffer allocation failed') throw (e);
console.log(skipMessage);
return;
}
// Ensure we have enough memory available for future allocations to succeed.
if (!binding.ensureAllocation(2 * kStringMaxLength)) {
console.log(skipMessage);
return;
}
assert.throws(function() {
buf.toString();
}, /"toString\(\)" failed|Array buffer allocation failed/);
assert.throws(function() {
buf.toString('utf8');
}, /"toString\(\)" failed/);