Skip to content

Commit 9d16729

Browse files
mcornacrvagg
authored andcommitted
test: skip instead of fail when mem constrained
The current implementation of tests for strings with length at or exceeding kStringMaxLength allocate a temporary buffer inside a try block to skip the test if there is insufficient memory. This commit adds an invocation of the garbage collector after the temporary buffer is allocated so that memory is freed for later allocations. Change the corresponding catch block to rethrow the original exception instead of asserting the exception message to provide more information about the exception. Add an additional check before trying to allocate memory to immediately skip the test on machines with insufficient total memory. PR-URL: #3697 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent d969c09 commit 9d16729

9 files changed

+140
-58
lines changed

‎test/common.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ exports.isLinuxPPCBE = (process.platform === 'linux') &&
2222
exports.isSunOS=process.platform==='sunos';
2323
exports.isFreeBSD=process.platform==='freebsd';
2424

25+
exports.enoughTestMem=os.totalmem()>0x20000000;/* 512MB */
26+
2527
functionrimrafSync(p){
2628
try{
2729
varst=fs.lstatSync(p);
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
'use strict';
2+
// Flags: --expose-gc
23

3-
require('../common');
4+
constcommon=require('../common');
45
constassert=require('assert');
56

67
// v8 fails silently if string length > v8::String::kMaxLength
78
// v8::String::kMaxLength defined in v8.h
89
constkStringMaxLength=process.binding('buffer').kStringMaxLength;
910

11+
constskipMessage=
12+
'1..0 # Skipped: intensive toString tests due to memory confinements';
13+
if(!common.enoughTestMem){
14+
console.log(skipMessage);
15+
return;
16+
}
17+
assert(typeofgc==='function','Run this test with --expose-gc');
18+
1019
try{
11-
newBuffer(kStringMaxLength*3);
20+
varbuf=newBuffer(kStringMaxLength);
21+
// Try to allocate memory first then force gc so future allocations succeed.
22+
newBuffer(2*kStringMaxLength);
23+
gc();
1224
}catch(e){
13-
assert.equal(e.message,'Invalid array buffer length');
14-
console.log(
15-
'1..0 # Skipped: intensive toString tests due to memory confinements');
25+
// If the exception is not due to memory confinement then rethrow it.
26+
if(e.message!=='Invalid array buffer length')throw(e);
27+
console.log(skipMessage);
1628
return;
1729
}
1830

19-
constbuf=newBuffer(kStringMaxLength);
20-
2131
constmaxString=buf.toString('binary');
2232
assert.equal(maxString.length,kStringMaxLength);
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
'use strict';
2+
// Flags: --expose-gc
23

3-
require('../common');
4+
constcommon=require('../common');
45
constassert=require('assert');
56

7+
constskipMessage=
8+
'1..0 # Skipped: intensive toString tests due to memory confinements';
9+
if(!common.enoughTestMem){
10+
console.log(skipMessage);
11+
return;
12+
}
13+
assert(typeofgc==='function','Run this test with --expose-gc');
14+
615
// v8 fails silently if string length > v8::String::kMaxLength
716
// v8::String::kMaxLength defined in v8.h
817
constkStringMaxLength=process.binding('buffer').kStringMaxLength;
918

1019
try{
11-
newBuffer(kStringMaxLength*3);
20+
varbuf=newBuffer(kStringMaxLength+1);
21+
// Try to allocate memory first then force gc so future allocations succeed.
22+
newBuffer(2*kStringMaxLength);
23+
gc();
1224
}catch(e){
13-
assert.equal(e.message,'Invalid array buffer length');
14-
console.log(
15-
'1..0 # Skipped: intensive toString tests due to memory confinements');
25+
// If the exception is not due to memory confinement then rethrow it.
26+
if(e.message!=='Invalid array buffer length')throw(e);
27+
console.log(skipMessage);
1628
return;
1729
}
1830

19-
constbuf=newBuffer(kStringMaxLength+1);
20-
2131
assert.throws(function(){
2232
buf.toString('ascii');
2333
},/toStringfailed/);
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
'use strict';
2+
// Flags: --expose-gc
23

3-
require('../common');
4+
constcommon=require('../common');
45
constassert=require('assert');
56

7+
constskipMessage=
8+
'1..0 # Skipped: intensive toString tests due to memory confinements';
9+
if(!common.enoughTestMem){
10+
console.log(skipMessage);
11+
return;
12+
}
13+
assert(typeofgc==='function','Run this test with --expose-gc');
14+
615
// v8 fails silently if string length > v8::String::kMaxLength
716
// v8::String::kMaxLength defined in v8.h
817
constkStringMaxLength=process.binding('buffer').kStringMaxLength;
918

1019
try{
11-
newBuffer(kStringMaxLength*3);
20+
varbuf=newBuffer(kStringMaxLength+1);
21+
// Try to allocate memory first then force gc so future allocations succeed.
22+
newBuffer(2*kStringMaxLength);
23+
gc();
1224
}catch(e){
13-
assert.equal(e.message,'Invalid array buffer length');
14-
console.log(
15-
'1..0 # Skipped: intensive toString tests due to memory confinements');
25+
// If the exception is not due to memory confinement then rethrow it.
26+
if(e.message!=='Invalid array buffer length')throw(e);
27+
console.log(skipMessage);
1628
return;
1729
}
1830

19-
constbuf=newBuffer(kStringMaxLength+1);
20-
2131
assert.throws(function(){
2232
buf.toString('base64');
2333
},/toStringfailed/);

‎test/parallel/test-stringbytes-external-exceed-max-by-1-binary.js‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
'use strict';
2+
// Flags: --expose-gc
23

3-
require('../common');
4+
constcommon=require('../common');
45
constassert=require('assert');
56

7+
constskipMessage=
8+
'1..0 # Skipped: intensive toString tests due to memory confinements';
9+
if(!common.enoughTestMem){
10+
console.log(skipMessage);
11+
return;
12+
}
13+
assert(typeofgc==='function','Run this test with --expose-gc');
14+
615
// v8 fails silently if string length > v8::String::kMaxLength
716
// v8::String::kMaxLength defined in v8.h
817
constkStringMaxLength=process.binding('buffer').kStringMaxLength;
918

1019
try{
11-
newBuffer(kStringMaxLength*3);
20+
varbuf=newBuffer(kStringMaxLength+1);
21+
// Try to allocate memory first then force gc so future allocations succeed.
22+
newBuffer(2*kStringMaxLength);
23+
gc();
1224
}catch(e){
13-
assert.equal(e.message,'Invalid array buffer length');
14-
console.log(
15-
'1..0 # Skipped: intensive toString tests due to memory confinements');
25+
// If the exception is not due to memory confinement then rethrow it.
26+
if(e.message!=='Invalid array buffer length')throw(e);
27+
console.log(skipMessage);
1628
return;
1729
}
1830

19-
constbuf=newBuffer(kStringMaxLength+1);
20-
2131
assert.throws(function(){
2232
buf.toString('binary');
2333
},/toStringfailed/);
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
'use strict';
2+
// Flags: --expose-gc
23

3-
require('../common');
4+
constcommon=require('../common');
45
constassert=require('assert');
56

7+
constskipMessage=
8+
'1..0 # Skipped: intensive toString tests due to memory confinements';
9+
if(!common.enoughTestMem){
10+
console.log(skipMessage);
11+
return;
12+
}
13+
assert(typeofgc==='function','Run this test with --expose-gc');
14+
615
// v8 fails silently if string length > v8::String::kMaxLength
716
// v8::String::kMaxLength defined in v8.h
817
constkStringMaxLength=process.binding('buffer').kStringMaxLength;
918

1019
try{
11-
newBuffer(kStringMaxLength*3);
20+
varbuf=newBuffer(kStringMaxLength+1);
21+
// Try to allocate memory first then force gc so future allocations succeed.
22+
newBuffer(2*kStringMaxLength);
23+
gc();
1224
}catch(e){
13-
assert.equal(e.message,'Invalid array buffer length');
14-
console.log(
15-
'1..0 # Skipped: intensive toString tests due to memory confinements');
25+
// If the exception is not due to memory confinement then rethrow it.
26+
if(e.message!=='Invalid array buffer length')throw(e);
27+
console.log(skipMessage);
1628
return;
1729
}
1830

19-
constbuf=newBuffer(kStringMaxLength+1);
20-
2131
assert.throws(function(){
2232
buf.toString('hex');
2333
},/toStringfailed/);

‎test/parallel/test-stringbytes-external-exceed-max-by-1-utf8.js‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
'use strict';
2+
// Flags: --expose-gc
23

3-
require('../common');
4+
constcommon=require('../common');
45
constassert=require('assert');
56

7+
constskipMessage=
8+
'1..0 # Skipped: intensive toString tests due to memory confinements';
9+
if(!common.enoughTestMem){
10+
console.log(skipMessage);
11+
return;
12+
}
13+
assert(typeofgc==='function','Run this test with --expose-gc');
14+
615
// v8 fails silently if string length > v8::String::kMaxLength
716
// v8::String::kMaxLength defined in v8.h
817
constkStringMaxLength=process.binding('buffer').kStringMaxLength;
918

1019
try{
11-
newBuffer(kStringMaxLength*3);
20+
varbuf=newBuffer(kStringMaxLength+1);
21+
// Try to allocate memory first then force gc so future allocations succeed.
22+
newBuffer(2*kStringMaxLength);
23+
gc();
1224
}catch(e){
13-
assert.equal(e.message,'Invalid array buffer length');
14-
console.log(
15-
'1..0 # Skipped: intensive toString tests due to memory confinements');
25+
// If the exception is not due to memory confinement then rethrow it.
26+
if(e.message!=='Invalid array buffer length')throw(e);
27+
console.log(skipMessage);
1628
return;
1729
}
1830

19-
constbuf=newBuffer(kStringMaxLength+1);
20-
2131
assert.throws(function(){
2232
buf.toString();
2333
},/toStringfailed|Invalidarraybufferlength/);
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
'use strict';
2+
// Flags: --expose-gc
23

3-
require('../common');
4+
constcommon=require('../common');
45
constassert=require('assert');
56

7+
constskipMessage=
8+
'1..0 # Skipped: intensive toString tests due to memory confinements';
9+
if(!common.enoughTestMem){
10+
console.log(skipMessage);
11+
return;
12+
}
13+
assert(typeofgc==='function','Run this test with --expose-gc');
14+
615
// v8 fails silently if string length > v8::String::kMaxLength
716
// v8::String::kMaxLength defined in v8.h
817
constkStringMaxLength=process.binding('buffer').kStringMaxLength;
918

1019
try{
11-
newBuffer(kStringMaxLength*3);
20+
varbuf=newBuffer(kStringMaxLength+2);
21+
// Try to allocate memory first then force gc so future allocations succeed.
22+
newBuffer(2*kStringMaxLength);
23+
gc();
1224
}catch(e){
13-
assert.equal(e.message,'Invalid array buffer length');
14-
console.log(
15-
'1..0 # Skipped: intensive toString tests due to memory confinements');
25+
// If the exception is not due to memory confinement then rethrow it.
26+
if(e.message!=='Invalid array buffer length')throw(e);
27+
console.log(skipMessage);
1628
return;
1729
}
1830

19-
constbuf2=newBuffer(kStringMaxLength+2);
20-
21-
constmaxString=buf2.toString('utf16le');
31+
constmaxString=buf.toString('utf16le');
2232
assert.equal(maxString.length,(kStringMaxLength+2)/2);
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
'use strict';
2+
// Flags: --expose-gc
23

3-
require('../common');
4+
constcommon=require('../common');
45
constassert=require('assert');
56

7+
constskipMessage=
8+
'1..0 # Skipped: intensive toString tests due to memory confinements';
9+
if(!common.enoughTestMem){
10+
console.log(skipMessage);
11+
return;
12+
}
13+
assert(typeofgc==='function','Run this test with --expose-gc');
14+
615
// v8 fails silently if string length > v8::String::kMaxLength
716
// v8::String::kMaxLength defined in v8.h
817
constkStringMaxLength=process.binding('buffer').kStringMaxLength;
918

1019
try{
11-
newBuffer(kStringMaxLength*3);
20+
varbuf=newBuffer(kStringMaxLength*2+2);
21+
// Try to allocate memory first then force gc so future allocations succeed.
22+
newBuffer(2*kStringMaxLength);
23+
gc();
1224
}catch(e){
13-
assert.equal(e.message,'Invalid array buffer length');
14-
console.log(
15-
'1..0 # Skipped: intensive toString tests due to memory confinements');
25+
// If the exception is not due to memory confinement then rethrow it.
26+
if(e.message!=='Invalid array buffer length')throw(e);
27+
console.log(skipMessage);
1628
return;
1729
}
1830

19-
constbuf0=newBuffer(kStringMaxLength*2+2);
20-
2131
assert.throws(function(){
22-
buf0.toString('utf16le');
32+
buf.toString('utf16le');
2333
},/toStringfailed/);

0 commit comments

Comments
(0)