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 +#include +#include + +void EnsureAllocation(const v8::FunctionCallbackInfo &args) { + v8::Isolate* isolate = args.GetIsolate(); + uintptr_t size = args[0]->IntegerValue(); + v8::Local 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 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 similarity index 73% rename from test/sequential/test-stringbytes-external-exceed-max-by-1-ascii.js rename to 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 similarity index 73% rename from test/sequential/test-stringbytes-external-exceed-max-by-1-base64.js rename to 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 similarity index 79% rename from test/sequential/test-stringbytes-external-exceed-max-by-1-binary.js rename to 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 similarity index 73% rename from test/sequential/test-stringbytes-external-exceed-max-by-1-hex.js rename to 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 similarity index 75% rename from test/sequential/test-stringbytes-external-exceed-max-by-1-utf8.js rename to 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 similarity index 73% rename from test/sequential/test-stringbytes-external-exceed-max-by-2.js rename to 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 similarity index 73% rename from test/sequential/test-stringbytes-external-exceed-max.js rename to 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/);