Browse Source

test: fix another flaky stringbytes test

Avoid depending on precise timing of when an object will be collected
by GC. This test was missed by #6039 as it happened to be in a
different directory than the rest.

Ref: https://github.com/nodejs/node/pull/6039
PR-URL: https://github.com/nodejs/node/pull/6073
Reviewed-By: Trott - Rich Trott <rtrott@gmail.com>
process-exit-stdio-flushing
Ali Ijaz Sheikh 9 years ago
parent
commit
b73e1b3c5a
  1. 4
      test/README.md
  2. 14
      test/addons/stringbytes-external-exceed-max/test-stringbytes-external-at-max.js

4
test/README.md

@ -10,7 +10,9 @@ Tests for when the `--abort-on-uncaught-exception` flag is used.
### addons ### 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 | | Runs on CI |
|:----------:| |:----------:|

14
test/parallel/test-stringbytes-external-at-max.js → test/addons/stringbytes-external-exceed-max/test-stringbytes-external-at-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');
// v8 fails silently if string length > v8::String::kMaxLength // v8 fails silently if string length > v8::String::kMaxLength
@ -14,13 +14,9 @@ if (!common.enoughTestMem) {
console.log(skipMessage); console.log(skipMessage);
return; return;
} }
assert(typeof gc === 'function', 'Run this test with --expose-gc');
try { try {
var buf = Buffer.allocUnsafe(kStringMaxLength); var buf = Buffer.allocUnsafe(kStringMaxLength);
// 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('binary'); const maxString = buf.toString('binary');
assert.equal(maxString.length, kStringMaxLength); assert.equal(maxString.length, kStringMaxLength);
Loading…
Cancel
Save