Skip to content

Commit

Permalink
test: speed up stringbytes-external test
Browse files Browse the repository at this point in the history
test-stringbytes-external tends to take quite a while on slower
hardware. A lot of the time is taken by creating a new buffer that is
very large. The improvements come from reusing the same buffer.

PR-URL: #3005
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
  • Loading branch information
evanlucas committed Oct 6, 2015
1 parent 81e98e9 commit 7d66749
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions test/parallel/test-stringbytes-external.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,63 +113,66 @@ var PRE_3OF4_APEX = Math.ceil((EXTERN_APEX / 4) * 3) - RADIOS;
// v8::String::kMaxLength defined in v8.h
const kStringMaxLength = process.binding('buffer').kStringMaxLength;

assert.throws(function() {
new Buffer(kStringMaxLength + 1).toString();
}, /toString failed|Invalid array buffer length/);

try {
new Buffer(kStringMaxLength * 4);
new Buffer(kStringMaxLength * 3);
} catch(e) {
assert.equal(e.message, 'Invalid array buffer length');
console.log(
'1..0 # Skipped: intensive toString tests due to memory confinements');
return;
}

const buf0 = new Buffer(kStringMaxLength * 2 + 2);
const buf1 = buf0.slice(0, kStringMaxLength + 1);
const buf2 = buf0.slice(0, kStringMaxLength);
const buf3 = buf0.slice(0, kStringMaxLength + 2);

assert.throws(function() {
buf1.toString();
}, /toString failed|Invalid array buffer length/);

assert.throws(function() {
new Buffer(kStringMaxLength + 1).toString('ascii');
buf1.toString('ascii');
}, /toString failed/);

assert.throws(function() {
new Buffer(kStringMaxLength + 1).toString('utf8');
buf1.toString('utf8');
}, /toString failed/);

assert.throws(function() {
new Buffer(kStringMaxLength * 2 + 2).toString('utf16le');
buf0.toString('utf16le');
}, /toString failed/);

assert.throws(function() {
new Buffer(kStringMaxLength + 1).toString('binary');
buf1.toString('binary');
}, /toString failed/);

assert.throws(function() {
new Buffer(kStringMaxLength + 1).toString('base64');
buf1.toString('base64');
}, /toString failed/);

assert.throws(function() {
new Buffer(kStringMaxLength + 1).toString('hex');
buf1.toString('hex');
}, /toString failed/);

var maxString = new Buffer(kStringMaxLength).toString();
var maxString = buf2.toString();
assert.equal(maxString.length, kStringMaxLength);
// Free the memory early instead of at the end of the next assignment
maxString = undefined;

maxString = new Buffer(kStringMaxLength).toString('binary');
maxString = buf2.toString('binary');
assert.equal(maxString.length, kStringMaxLength);
maxString = undefined;

maxString =
new Buffer(kStringMaxLength + 1).toString('binary', 1);
maxString = buf1.toString('binary', 1);
assert.equal(maxString.length, kStringMaxLength);
maxString = undefined;

maxString =
new Buffer(kStringMaxLength + 1).toString('binary', 0, kStringMaxLength);
maxString = buf1.toString('binary', 0, kStringMaxLength);
assert.equal(maxString.length, kStringMaxLength);
maxString = undefined;

maxString = new Buffer(kStringMaxLength + 2).toString('utf16le');
maxString = buf3.toString('utf16le');
assert.equal(maxString.length, (kStringMaxLength + 2) / 2);
maxString = undefined;
})();

0 comments on commit 7d66749

Please sign in to comment.