Skip to content

Commit

Permalink
test: fix flakiness of stringbytes-external
Browse files Browse the repository at this point in the history
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: #5945
Ref: #6039
Ref: #6073

PR-URL: #6705
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
ofrobots authored and Myles Borins committed May 18, 2016
1 parent cc4c518 commit 7b60b8f
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 49 deletions.
4 changes: 3 additions & 1 deletion test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Tests for when the `--abort-on-uncaught-exception` flag is used.

### addons

Tests for [addon](https://nodejs.org/api/addons.html) functionality.
Tests for [addon](https://nodejs.org/api/addons.html) functionality along with
some tests that require an addon to function properly.


| Runs on CI |
|:----------:|
Expand Down
24 changes: 24 additions & 0 deletions test/addons/stringbytes-external-exceed-max/binding.cc
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions test/addons/stringbytes-external-exceed-max/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
'targets': [
{
'target_name': 'binding',
'sources': [ 'binding.cc' ]
}
]
}
Original file line number Diff line number Diff line change
@@ -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');

// v8 fails silently if string length > v8::String::kMaxLength
Expand All @@ -14,19 +14,21 @@ if (!common.enoughTestMem) {
console.log(skipMessage);
return;
}
assert(typeof gc === 'function', 'Run this test with --expose-gc');

try {
var buf = new Buffer(kStringMaxLength);
// Try to allocate memory first then force gc so future allocations succeed.
new Buffer(2 * kStringMaxLength);
gc();
} catch (e) {
// If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Invalid array buffer length') 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;
}

const maxString = buf.toString('binary');
assert.equal(maxString.length, kStringMaxLength);
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -10,24 +10,26 @@ 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
const kStringMaxLength = process.binding('buffer').kStringMaxLength;

try {
var buf = new Buffer(kStringMaxLength + 1);
// Try to allocate memory first then force gc so future allocations succeed.
new Buffer(2 * kStringMaxLength);
gc();
} catch (e) {
// If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Invalid array buffer length') 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('ascii');
}, /toString failed/);
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -10,24 +10,26 @@ 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
const kStringMaxLength = process.binding('buffer').kStringMaxLength;

try {
var buf = new Buffer(kStringMaxLength + 1);
// Try to allocate memory first then force gc so future allocations succeed.
new Buffer(2 * kStringMaxLength);
gc();
} catch (e) {
// If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Invalid array buffer length') 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('base64');
}, /toString failed/);
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -10,24 +10,26 @@ 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
const kStringMaxLength = process.binding('buffer').kStringMaxLength;

try {
var buf = new Buffer(kStringMaxLength + 1);
// Try to allocate memory first then force gc so future allocations succeed.
new Buffer(2 * kStringMaxLength);
gc();
} catch (e) {
// If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Invalid array buffer length') 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('binary');
}, /toString failed/);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -10,24 +10,26 @@ 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
const kStringMaxLength = process.binding('buffer').kStringMaxLength;

try {
var buf = new Buffer(kStringMaxLength + 1);
// Try to allocate memory first then force gc so future allocations succeed.
new Buffer(2 * kStringMaxLength);
gc();
} catch (e) {
// If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Invalid array buffer length') 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('hex');
}, /toString failed/);
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -10,24 +10,26 @@ 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
const kStringMaxLength = process.binding('buffer').kStringMaxLength;

try {
var buf = new Buffer(kStringMaxLength + 1);
// Try to allocate memory first then force gc so future allocations succeed.
new Buffer(2 * kStringMaxLength);
gc();
} catch (e) {
// If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Invalid array buffer length') 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|Invalid array buffer length/);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -10,23 +10,25 @@ 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
const kStringMaxLength = process.binding('buffer').kStringMaxLength;

try {
var buf = new Buffer(kStringMaxLength + 2);
// Try to allocate memory first then force gc so future allocations succeed.
new Buffer(2 * kStringMaxLength);
gc();
} catch (e) {
// If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Invalid array buffer length') 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;
}

const maxString = buf.toString('utf16le');
assert.equal(maxString.length, (kStringMaxLength + 2) / 2);
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -10,24 +10,26 @@ 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
const kStringMaxLength = process.binding('buffer').kStringMaxLength;

try {
var buf = new Buffer(kStringMaxLength * 2 + 2);
// Try to allocate memory first then force gc so future allocations succeed.
new Buffer(2 * kStringMaxLength);
gc();
} catch (e) {
// If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Invalid array buffer length') 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('utf16le');
}, /toString failed/);

0 comments on commit 7b60b8f

Please sign in to comment.