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>
This commit is contained in:
parent
2d7e31614d
commit
f4ebd5989a
24
test/addons/stringbytes-external-exceed-max/binding.cc
Normal file
24
test/addons/stringbytes-external-exceed-max/binding.cc
Normal file
@ -0,0 +1,24 @@
|
||||
#include <stdlib.h>
|
||||
#include <node.h>
|
||||
#include <v8.h>
|
||||
|
||||
void EnsureAllocation(const v8::FunctionCallbackInfo<v8::Value> &args) {
|
||||
v8::Isolate* isolate = args.GetIsolate();
|
||||
uintptr_t size = args[0]->IntegerValue();
|
||||
v8::Local<v8::Boolean> success;
|
||||
|
||||
void* buffer = malloc(size);
|
||||
if (buffer) {
|
||||
success = v8::Boolean::New(isolate, true);
|
||||
free(buffer);
|
||||
} else {
|
||||
success = v8::Boolean::New(isolate, false);
|
||||
}
|
||||
args.GetReturnValue().Set(success);
|
||||
}
|
||||
|
||||
void init(v8::Local<v8::Object> target) {
|
||||
NODE_SET_METHOD(target, "ensureAllocation", EnsureAllocation);
|
||||
}
|
||||
|
||||
NODE_MODULE(binding, init);
|
8
test/addons/stringbytes-external-exceed-max/binding.gyp
Normal file
8
test/addons/stringbytes-external-exceed-max/binding.gyp
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
'targets': [
|
||||
{
|
||||
'target_name': 'binding',
|
||||
'sources': [ 'binding.cc' ]
|
||||
}
|
||||
]
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
// Flags: --expose-gc
|
||||
|
||||
const common = require('../common');
|
||||
const common = require('../../common');
|
||||
const binding = require('./build/Release/binding');
|
||||
const assert = require('assert');
|
||||
|
||||
const skipMessage =
|
||||
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
|
||||
console.log(skipMessage);
|
||||
return;
|
||||
}
|
||||
assert(typeof gc === 'function', 'Run this test with --expose-gc');
|
||||
|
||||
// v8 fails silently if string length > v8::String::kMaxLength
|
||||
// v8::String::kMaxLength defined in v8.h
|
||||
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
|
||||
|
||||
try {
|
||||
var buf = Buffer.allocUnsafe(kStringMaxLength + 1);
|
||||
// Try to allocate memory first then force gc so future allocations succeed.
|
||||
Buffer.allocUnsafe(2 * kStringMaxLength);
|
||||
gc();
|
||||
} catch (e) {
|
||||
// If the exception is not due to memory confinement then rethrow it.
|
||||
if (e.message !== 'Array buffer allocation failed') throw (e);
|
||||
@ -28,6 +24,12 @@ try {
|
||||
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('ascii');
|
||||
}, /"toString\(\)" failed/);
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
// Flags: --expose-gc
|
||||
|
||||
const common = require('../common');
|
||||
const common = require('../../common');
|
||||
const binding = require('./build/Release/binding');
|
||||
const assert = require('assert');
|
||||
|
||||
const skipMessage =
|
||||
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
|
||||
console.log(skipMessage);
|
||||
return;
|
||||
}
|
||||
assert(typeof gc === 'function', 'Run this test with --expose-gc');
|
||||
|
||||
// v8 fails silently if string length > v8::String::kMaxLength
|
||||
// v8::String::kMaxLength defined in v8.h
|
||||
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
|
||||
|
||||
try {
|
||||
var buf = Buffer.allocUnsafe(kStringMaxLength + 1);
|
||||
// Try to allocate memory first then force gc so future allocations succeed.
|
||||
Buffer.allocUnsafe(2 * kStringMaxLength);
|
||||
gc();
|
||||
} catch (e) {
|
||||
// If the exception is not due to memory confinement then rethrow it.
|
||||
if (e.message !== 'Array buffer allocation failed') throw (e);
|
||||
@ -28,6 +24,12 @@ try {
|
||||
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('base64');
|
||||
}, /"toString\(\)" failed/);
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
// Flags: --expose-gc
|
||||
|
||||
const common = require('../common');
|
||||
const common = require('../../common');
|
||||
const binding = require('./build/Release/binding');
|
||||
const assert = require('assert');
|
||||
|
||||
const skipMessage =
|
||||
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
|
||||
console.log(skipMessage);
|
||||
return;
|
||||
}
|
||||
assert(typeof gc === 'function', 'Run this test with --expose-gc');
|
||||
|
||||
// v8 fails silently if string length > v8::String::kMaxLength
|
||||
// v8::String::kMaxLength defined in v8.h
|
||||
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
|
||||
|
||||
try {
|
||||
var buf = Buffer.allocUnsafe(kStringMaxLength + 1);
|
||||
// Try to allocate memory first then force gc so future allocations succeed.
|
||||
Buffer.allocUnsafe(2 * kStringMaxLength);
|
||||
gc();
|
||||
} catch (e) {
|
||||
// If the exception is not due to memory confinement then rethrow it.
|
||||
if (e.message !== 'Array buffer allocation failed') throw (e);
|
||||
@ -28,6 +24,12 @@ try {
|
||||
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('binary');
|
||||
}, /"toString\(\)" failed/);
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
// Flags: --expose-gc
|
||||
|
||||
const common = require('../common');
|
||||
const common = require('../../common');
|
||||
const binding = require('./build/Release/binding');
|
||||
const assert = require('assert');
|
||||
|
||||
const skipMessage =
|
||||
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
|
||||
console.log(skipMessage);
|
||||
return;
|
||||
}
|
||||
assert(typeof gc === 'function', 'Run this test with --expose-gc');
|
||||
|
||||
// v8 fails silently if string length > v8::String::kMaxLength
|
||||
// v8::String::kMaxLength defined in v8.h
|
||||
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
|
||||
|
||||
try {
|
||||
var buf = Buffer.allocUnsafe(kStringMaxLength + 1);
|
||||
// Try to allocate memory first then force gc so future allocations succeed.
|
||||
Buffer.allocUnsafe(2 * kStringMaxLength);
|
||||
gc();
|
||||
} catch (e) {
|
||||
// If the exception is not due to memory confinement then rethrow it.
|
||||
if (e.message !== 'Array buffer allocation failed') throw (e);
|
||||
@ -28,6 +24,12 @@ try {
|
||||
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('hex');
|
||||
}, /"toString\(\)" failed/);
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
// Flags: --expose-gc
|
||||
|
||||
const common = require('../common');
|
||||
const common = require('../../common');
|
||||
const binding = require('./build/Release/binding');
|
||||
const assert = require('assert');
|
||||
|
||||
const skipMessage =
|
||||
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
|
||||
console.log(skipMessage);
|
||||
return;
|
||||
}
|
||||
assert(typeof gc === 'function', 'Run this test with --expose-gc');
|
||||
|
||||
// v8 fails silently if string length > v8::String::kMaxLength
|
||||
// v8::String::kMaxLength defined in v8.h
|
||||
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
|
||||
|
||||
try {
|
||||
var buf = Buffer.allocUnsafe(kStringMaxLength + 1);
|
||||
// Try to allocate memory first then force gc so future allocations succeed.
|
||||
Buffer.allocUnsafe(2 * kStringMaxLength);
|
||||
gc();
|
||||
} catch (e) {
|
||||
// If the exception is not due to memory confinement then rethrow it.
|
||||
if (e.message !== 'Array buffer allocation failed') throw (e);
|
||||
@ -28,6 +24,12 @@ try {
|
||||
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/);
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
// Flags: --expose-gc
|
||||
|
||||
const common = require('../common');
|
||||
const common = require('../../common');
|
||||
const binding = require('./build/Release/binding');
|
||||
const assert = require('assert');
|
||||
|
||||
const skipMessage =
|
||||
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
|
||||
console.log(skipMessage);
|
||||
return;
|
||||
}
|
||||
assert(typeof gc === 'function', 'Run this test with --expose-gc');
|
||||
|
||||
// v8 fails silently if string length > v8::String::kMaxLength
|
||||
// v8::String::kMaxLength defined in v8.h
|
||||
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
|
||||
|
||||
try {
|
||||
var buf = Buffer.allocUnsafe(kStringMaxLength + 2);
|
||||
// Try to allocate memory first then force gc so future allocations succeed.
|
||||
Buffer.allocUnsafe(2 * kStringMaxLength);
|
||||
gc();
|
||||
} catch (e) {
|
||||
// If the exception is not due to memory confinement then rethrow it.
|
||||
if (e.message !== 'Array buffer allocation failed') throw (e);
|
||||
@ -28,5 +24,11 @@ try {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure we have enough memory available for future allocations to succeed.
|
||||
if (!binding.ensureAllocation(2 * kStringMaxLength)) {
|
||||
console.log(skipMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
const maxString = buf.toString('utf16le');
|
||||
assert.equal(maxString.length, (kStringMaxLength + 2) / 2);
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
// Flags: --expose-gc
|
||||
|
||||
const common = require('../common');
|
||||
const common = require('../../common');
|
||||
const binding = require('./build/Release/binding');
|
||||
const assert = require('assert');
|
||||
|
||||
const skipMessage =
|
||||
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
|
||||
console.log(skipMessage);
|
||||
return;
|
||||
}
|
||||
assert(typeof gc === 'function', 'Run this test with --expose-gc');
|
||||
|
||||
// v8 fails silently if string length > v8::String::kMaxLength
|
||||
// v8::String::kMaxLength defined in v8.h
|
||||
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
|
||||
|
||||
try {
|
||||
var buf = Buffer.allocUnsafe(kStringMaxLength * 2 + 2);
|
||||
// Try to allocate memory first then force gc so future allocations succeed.
|
||||
Buffer.allocUnsafe(2 * kStringMaxLength);
|
||||
gc();
|
||||
} catch (e) {
|
||||
// If the exception is not due to memory confinement then rethrow it.
|
||||
if (e.message !== 'Array buffer allocation failed') throw (e);
|
||||
@ -28,6 +24,12 @@ try {
|
||||
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('utf16le');
|
||||
}, /"toString\(\)" failed/);
|
Loading…
x
Reference in New Issue
Block a user