diff options
author | Ali Ijaz Sheikh <ofrobots@google.com> | 2016-04-04 09:09:52 -0700 |
---|---|---|
committer | Ali Ijaz Sheikh <ofrobots@google.com> | 2016-04-05 16:08:45 -0700 |
commit | f4ebd5989a76c1986d0ca5a5476ca285081000ee (patch) | |
tree | d15addfe0eafc479b40f7e516ee99c7b7f839535 /test | |
parent | 2d7e31614dbc61b4fd5a594649c12d65398a815e (diff) | |
download | android-node-v8-f4ebd5989a76c1986d0ca5a5476ca285081000ee.tar.gz android-node-v8-f4ebd5989a76c1986d0ca5a5476ca285081000ee.tar.bz2 android-node-v8-f4ebd5989a76c1986d0ca5a5476ca285081000ee.zip |
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>
Diffstat (limited to 'test')
9 files changed, 88 insertions, 42 deletions
diff --git a/test/addons/stringbytes-external-exceed-max/binding.cc b/test/addons/stringbytes-external-exceed-max/binding.cc new file mode 100644 index 0000000000..65390852c2 --- /dev/null +++ b/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); diff --git a/test/addons/stringbytes-external-exceed-max/binding.gyp b/test/addons/stringbytes-external-exceed-max/binding.gyp new file mode 100644 index 0000000000..3bfb84493f --- /dev/null +++ b/test/addons/stringbytes-external-exceed-max/binding.gyp @@ -0,0 +1,8 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'sources': [ 'binding.cc' ] + } + ] +} diff --git a/test/sequential/test-stringbytes-external-exceed-max-by-1-ascii.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js index 058bc435fc..1a636a5c7f 100644 --- a/test/sequential/test-stringbytes-external-exceed-max-by-1-ascii.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js @@ -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/); diff --git a/test/sequential/test-stringbytes-external-exceed-max-by-1-base64.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js index cc65206b4e..82268bc5cd 100644 --- a/test/sequential/test-stringbytes-external-exceed-max-by-1-base64.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js @@ -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/); diff --git a/test/sequential/test-stringbytes-external-exceed-max-by-1-binary.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js index 8689745528..4cd4ca9dd9 100644 --- a/test/sequential/test-stringbytes-external-exceed-max-by-1-binary.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js @@ -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/); diff --git a/test/sequential/test-stringbytes-external-exceed-max-by-1-hex.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js index 0ec46fd686..f339f1ad5c 100644 --- a/test/sequential/test-stringbytes-external-exceed-max-by-1-hex.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js @@ -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/); diff --git a/test/sequential/test-stringbytes-external-exceed-max-by-1-utf8.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js index 4ea46a2cc2..5337836e2f 100644 --- a/test/sequential/test-stringbytes-external-exceed-max-by-1-utf8.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js @@ -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/); diff --git a/test/sequential/test-stringbytes-external-exceed-max-by-2.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-2.js index a75b5059b9..957a8f464f 100644 --- a/test/sequential/test-stringbytes-external-exceed-max-by-2.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-2.js @@ -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); diff --git a/test/sequential/test-stringbytes-external-exceed-max.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js index 51c36c5521..38a2edff25 100644 --- a/test/sequential/test-stringbytes-external-exceed-max.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js @@ -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/); |