node/test/pummel/test-fs-readfile-tostring-fail.js
Richard Lau 604ac4bc6c
test: add check to test-fs-readfile-tostring-fail
Check that all of the bytes were written to the temporary file before
reading it to catch the case where there is insufficient disk space.

PR-URL: https://github.com/nodejs/node/pull/43850
Refs: https://github.com/nodejs/node/issues/43833
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2022-07-18 12:24:48 +01:00

75 lines
2.0 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.enoughTestMem)
common.skip('intensive toString tests due to memory confinements');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const cp = require('child_process');
const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH;
if (common.isAIX && (Number(cp.execSync('ulimit -f')) * 512) < kStringMaxLength)
common.skip('intensive toString tests due to file size confinements');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const file = path.join(tmpdir.path, 'toobig.txt');
const stream = fs.createWriteStream(file, {
flags: 'a'
});
stream.on('error', (err) => { throw err; });
const size = kStringMaxLength / 200;
const a = Buffer.alloc(size, 'a');
let expectedSize = 0;
for (let i = 0; i < 201; i++) {
stream.write(a, (err) => { assert.ifError(err); });
expectedSize += a.length;
}
stream.end();
stream.on('finish', common.mustCall(function() {
assert.strictEqual(stream.bytesWritten, expectedSize,
`${stream.bytesWritten} bytes written (expected ${expectedSize} bytes).`);
fs.readFile(file, 'utf8', common.mustCall(function(err, buf) {
assert.ok(err instanceof Error);
if (err.message !== 'Array buffer allocation failed') {
const stringLengthHex = kStringMaxLength.toString(16);
common.expectsError({
message: 'Cannot create a string longer than ' +
`0x${stringLengthHex} characters`,
code: 'ERR_STRING_TOO_LONG',
name: 'Error'
})(err);
}
assert.strictEqual(buf, undefined);
}));
}));
function destroy() {
try {
fs.unlinkSync(file);
} catch {
// it may not exist
}
}
process.on('exit', destroy);
process.on('SIGINT', function() {
destroy();
process.exit();
});
// To make sure we don't leave a very large file
// on test machines in the event this test fails.
process.on('uncaughtException', function(err) {
destroy();
throw err;
});