Skip to content

test: reduce memory use in test stringbytes

When I run the string bytes tests dealing with strings of length kStringMaxLength or more on a Windows test machine with 4GB memory I see the tests skipped or failed with the following outputs respectively.

  • 1..0 # Skipped: intensive toString tests due to memory confinements
  • RangeError: Invalid array buffer length

The code being used to determine if the test should be skipped is:

 try {
  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;
 }

A problem with this approach is that the garbage collector does not necessarily run between the tried allocation and the necessary allocations later. That means that even though no exception is thrown in the try block since the machine has enough memory, the later allocations fail since the memory is now taken.

To verify this, I ran the test with Release\node.exe --expose-gc test\parallel\test-stringbytes-external-at-max.js to expose global.gc() which I used after the try catch block. With this setup the test would either skip or succeed as desired.

To solve the issue, I removed the Buffer allocations that were used to check if the tests should be skipped and moved the actual Buffer allocations into the try block so the test can still be skipped if memory allocation fails here. The tests should now use less total memory and be less likely to be skipped and more importantly, less likely to fail due to a memory constraint when they are not skipped.

Merge request reports

Loading