Browse Source

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>
process-exit-stdio-flushing
Ali Ijaz Sheikh 9 years ago
parent
commit
f4ebd5989a
  1. 24
      test/addons/stringbytes-external-exceed-max/binding.cc
  2. 8
      test/addons/stringbytes-external-exceed-max/binding.gyp
  3. 14
      test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js
  4. 14
      test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js
  5. 14
      test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js
  6. 14
      test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js
  7. 14
      test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js
  8. 14
      test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-2.js
  9. 14
      test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js

24
test/addons/stringbytes-external-exceed-max/binding.cc

@ -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

@ -0,0 +1,8 @@
{
'targets': [
{
'target_name': 'binding',
'sources': [ 'binding.cc' ]
}
]
}

14
test/sequential/test-stringbytes-external-exceed-max-by-1-ascii.js → test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js

@ -1,7 +1,7 @@
'use strict'; 'use strict';
// Flags: --expose-gc
const common = require('../common'); const common = require('../../common');
const binding = require('./build/Release/binding');
const assert = require('assert'); const assert = require('assert');
const skipMessage = const skipMessage =
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
console.log(skipMessage); console.log(skipMessage);
return; return;
} }
assert(typeof gc === 'function', 'Run this test with --expose-gc');
// v8 fails silently if string length > v8::String::kMaxLength // v8 fails silently if string length > v8::String::kMaxLength
// v8::String::kMaxLength defined in v8.h // v8::String::kMaxLength defined in v8.h
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
try { try {
var buf = Buffer.allocUnsafe(kStringMaxLength + 1); 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) { } catch (e) {
// If the exception is not due to memory confinement then rethrow it. // If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Array buffer allocation failed') throw (e); if (e.message !== 'Array buffer allocation failed') throw (e);
@ -28,6 +24,12 @@ try {
return; return;
} }
// Ensure we have enough memory available for future allocations to succeed.
if (!binding.ensureAllocation(2 * kStringMaxLength)) {
console.log(skipMessage);
return;
}
assert.throws(function() { assert.throws(function() {
buf.toString('ascii'); buf.toString('ascii');
}, /"toString\(\)" failed/); }, /"toString\(\)" failed/);

14
test/sequential/test-stringbytes-external-exceed-max-by-1-base64.js → test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js

@ -1,7 +1,7 @@
'use strict'; 'use strict';
// Flags: --expose-gc
const common = require('../common'); const common = require('../../common');
const binding = require('./build/Release/binding');
const assert = require('assert'); const assert = require('assert');
const skipMessage = const skipMessage =
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
console.log(skipMessage); console.log(skipMessage);
return; return;
} }
assert(typeof gc === 'function', 'Run this test with --expose-gc');
// v8 fails silently if string length > v8::String::kMaxLength // v8 fails silently if string length > v8::String::kMaxLength
// v8::String::kMaxLength defined in v8.h // v8::String::kMaxLength defined in v8.h
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
try { try {
var buf = Buffer.allocUnsafe(kStringMaxLength + 1); 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) { } catch (e) {
// If the exception is not due to memory confinement then rethrow it. // If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Array buffer allocation failed') throw (e); if (e.message !== 'Array buffer allocation failed') throw (e);
@ -28,6 +24,12 @@ try {
return; return;
} }
// Ensure we have enough memory available for future allocations to succeed.
if (!binding.ensureAllocation(2 * kStringMaxLength)) {
console.log(skipMessage);
return;
}
assert.throws(function() { assert.throws(function() {
buf.toString('base64'); buf.toString('base64');
}, /"toString\(\)" failed/); }, /"toString\(\)" failed/);

14
test/sequential/test-stringbytes-external-exceed-max-by-1-binary.js → test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js

@ -1,7 +1,7 @@
'use strict'; 'use strict';
// Flags: --expose-gc
const common = require('../common'); const common = require('../../common');
const binding = require('./build/Release/binding');
const assert = require('assert'); const assert = require('assert');
const skipMessage = const skipMessage =
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
console.log(skipMessage); console.log(skipMessage);
return; return;
} }
assert(typeof gc === 'function', 'Run this test with --expose-gc');
// v8 fails silently if string length > v8::String::kMaxLength // v8 fails silently if string length > v8::String::kMaxLength
// v8::String::kMaxLength defined in v8.h // v8::String::kMaxLength defined in v8.h
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
try { try {
var buf = Buffer.allocUnsafe(kStringMaxLength + 1); 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) { } catch (e) {
// If the exception is not due to memory confinement then rethrow it. // If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Array buffer allocation failed') throw (e); if (e.message !== 'Array buffer allocation failed') throw (e);
@ -28,6 +24,12 @@ try {
return; return;
} }
// Ensure we have enough memory available for future allocations to succeed.
if (!binding.ensureAllocation(2 * kStringMaxLength)) {
console.log(skipMessage);
return;
}
assert.throws(function() { assert.throws(function() {
buf.toString('binary'); buf.toString('binary');
}, /"toString\(\)" failed/); }, /"toString\(\)" failed/);

14
test/sequential/test-stringbytes-external-exceed-max-by-1-hex.js → test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js

@ -1,7 +1,7 @@
'use strict'; 'use strict';
// Flags: --expose-gc
const common = require('../common'); const common = require('../../common');
const binding = require('./build/Release/binding');
const assert = require('assert'); const assert = require('assert');
const skipMessage = const skipMessage =
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
console.log(skipMessage); console.log(skipMessage);
return; return;
} }
assert(typeof gc === 'function', 'Run this test with --expose-gc');
// v8 fails silently if string length > v8::String::kMaxLength // v8 fails silently if string length > v8::String::kMaxLength
// v8::String::kMaxLength defined in v8.h // v8::String::kMaxLength defined in v8.h
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
try { try {
var buf = Buffer.allocUnsafe(kStringMaxLength + 1); 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) { } catch (e) {
// If the exception is not due to memory confinement then rethrow it. // If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Array buffer allocation failed') throw (e); if (e.message !== 'Array buffer allocation failed') throw (e);
@ -28,6 +24,12 @@ try {
return; return;
} }
// Ensure we have enough memory available for future allocations to succeed.
if (!binding.ensureAllocation(2 * kStringMaxLength)) {
console.log(skipMessage);
return;
}
assert.throws(function() { assert.throws(function() {
buf.toString('hex'); buf.toString('hex');
}, /"toString\(\)" failed/); }, /"toString\(\)" failed/);

14
test/sequential/test-stringbytes-external-exceed-max-by-1-utf8.js → test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js

@ -1,7 +1,7 @@
'use strict'; 'use strict';
// Flags: --expose-gc
const common = require('../common'); const common = require('../../common');
const binding = require('./build/Release/binding');
const assert = require('assert'); const assert = require('assert');
const skipMessage = const skipMessage =
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
console.log(skipMessage); console.log(skipMessage);
return; return;
} }
assert(typeof gc === 'function', 'Run this test with --expose-gc');
// v8 fails silently if string length > v8::String::kMaxLength // v8 fails silently if string length > v8::String::kMaxLength
// v8::String::kMaxLength defined in v8.h // v8::String::kMaxLength defined in v8.h
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
try { try {
var buf = Buffer.allocUnsafe(kStringMaxLength + 1); 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) { } catch (e) {
// If the exception is not due to memory confinement then rethrow it. // If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Array buffer allocation failed') throw (e); if (e.message !== 'Array buffer allocation failed') throw (e);
@ -28,6 +24,12 @@ try {
return; return;
} }
// Ensure we have enough memory available for future allocations to succeed.
if (!binding.ensureAllocation(2 * kStringMaxLength)) {
console.log(skipMessage);
return;
}
assert.throws(function() { assert.throws(function() {
buf.toString(); buf.toString();
}, /"toString\(\)" failed|Array buffer allocation failed/); }, /"toString\(\)" failed|Array buffer allocation failed/);

14
test/sequential/test-stringbytes-external-exceed-max-by-2.js → test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-2.js

@ -1,7 +1,7 @@
'use strict'; 'use strict';
// Flags: --expose-gc
const common = require('../common'); const common = require('../../common');
const binding = require('./build/Release/binding');
const assert = require('assert'); const assert = require('assert');
const skipMessage = const skipMessage =
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
console.log(skipMessage); console.log(skipMessage);
return; return;
} }
assert(typeof gc === 'function', 'Run this test with --expose-gc');
// v8 fails silently if string length > v8::String::kMaxLength // v8 fails silently if string length > v8::String::kMaxLength
// v8::String::kMaxLength defined in v8.h // v8::String::kMaxLength defined in v8.h
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
try { try {
var buf = Buffer.allocUnsafe(kStringMaxLength + 2); 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) { } catch (e) {
// If the exception is not due to memory confinement then rethrow it. // If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Array buffer allocation failed') throw (e); if (e.message !== 'Array buffer allocation failed') throw (e);
@ -28,5 +24,11 @@ try {
return; 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'); const maxString = buf.toString('utf16le');
assert.equal(maxString.length, (kStringMaxLength + 2) / 2); assert.equal(maxString.length, (kStringMaxLength + 2) / 2);

14
test/sequential/test-stringbytes-external-exceed-max.js → test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js

@ -1,7 +1,7 @@
'use strict'; 'use strict';
// Flags: --expose-gc
const common = require('../common'); const common = require('../../common');
const binding = require('./build/Release/binding');
const assert = require('assert'); const assert = require('assert');
const skipMessage = const skipMessage =
@ -10,7 +10,6 @@ if (!common.enoughTestMem) {
console.log(skipMessage); console.log(skipMessage);
return; return;
} }
assert(typeof gc === 'function', 'Run this test with --expose-gc');
// v8 fails silently if string length > v8::String::kMaxLength // v8 fails silently if string length > v8::String::kMaxLength
// v8::String::kMaxLength defined in v8.h // v8::String::kMaxLength defined in v8.h
@ -18,9 +17,6 @@ const kStringMaxLength = process.binding('buffer').kStringMaxLength;
try { try {
var buf = Buffer.allocUnsafe(kStringMaxLength * 2 + 2); 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) { } catch (e) {
// If the exception is not due to memory confinement then rethrow it. // If the exception is not due to memory confinement then rethrow it.
if (e.message !== 'Array buffer allocation failed') throw (e); if (e.message !== 'Array buffer allocation failed') throw (e);
@ -28,6 +24,12 @@ try {
return; return;
} }
// Ensure we have enough memory available for future allocations to succeed.
if (!binding.ensureAllocation(2 * kStringMaxLength)) {
console.log(skipMessage);
return;
}
assert.throws(function() { assert.throws(function() {
buf.toString('utf16le'); buf.toString('utf16le');
}, /"toString\(\)" failed/); }, /"toString\(\)" failed/);
Loading…
Cancel
Save