mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 18:25:13 +08:00
Both tests read the destination file with no ordering guarantee against the fs.write() that Utf8Stream still has in flight, so under load the read can observe an empty file. In test-fastutf8stream-destroy the read is issued right after destroy(). In test-fastutf8stream-reopen it is ordered on 'drain', documented as emitted when the buffer has drained enough to allow continued writing, which says nothing about the bytes being observable in the file. The reopen path also emits a 'drain' of its own from a nextTick before the write has landed. Order both reads on 'write' instead, documented as emitted when a write operation has completed and emitted from #release() once the underlying write returned. In sync mode it is emitted from within write(), so the listener is attached before the write call. No data is lost by Utf8Stream here: re-reading the file after a failed assertion shows the expected content. This corrects an expectation of the tests, not the runtime. Signed-off-by: Christian Aurich <christian.aurichzm@gmail.com> PR-URL: https://github.com/nodejs/node/pull/65554 Refs: https://github.com/nodejs/reliability/blob/main/reports/2026-08-20.md Refs: https://github.com/nodejs/reliability/blob/main/reports/2026-08-26.md Reviewed-By: Shelley Vohr <shelley.vohr@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const tmpdir = require('../common/tmpdir');
|
|
const assert = require('node:assert');
|
|
const {
|
|
openSync,
|
|
readFile,
|
|
readFileSync,
|
|
} = require('node:fs');
|
|
const { Utf8Stream } = require('node:fs');
|
|
const { isMainThread } = require('node:worker_threads');
|
|
const { join } = require('node:path');
|
|
|
|
let fileCounter = 0;
|
|
tmpdir.refresh();
|
|
|
|
if (isMainThread) {
|
|
process.umask(0o000);
|
|
}
|
|
|
|
function getTempFile() {
|
|
return join(tmpdir.path, `fastutf8stream-${process.pid}-${Date.now()}-${fileCounter++}.log`);
|
|
}
|
|
|
|
{
|
|
const dest = getTempFile();
|
|
const fd = openSync(dest, 'w');
|
|
const stream = new Utf8Stream({ fd, sync: false });
|
|
|
|
// Test successful write
|
|
assert.ok(stream.write('hello world\n'));
|
|
stream.destroy();
|
|
|
|
assert.throws(() => stream.write('hello world\n'), Error);
|
|
|
|
// Reading now would race the fs.write() still in flight.
|
|
stream.once('write', common.mustCall(() => {
|
|
readFile(dest, 'utf8', common.mustSucceed((data) => {
|
|
assert.strictEqual(data, 'hello world\n');
|
|
}));
|
|
}));
|
|
|
|
stream.on('finish', common.mustNotCall());
|
|
stream.on('close', common.mustCall());
|
|
};
|
|
|
|
{
|
|
const dest = getTempFile();
|
|
const fd = openSync(dest, 'w');
|
|
const stream = new Utf8Stream({ fd, sync: true });
|
|
|
|
assert.ok(stream.write('hello world\n'));
|
|
stream.destroy();
|
|
assert.throws(() => stream.write('hello world\n'), Error);
|
|
|
|
const data = readFileSync(dest, 'utf8');
|
|
assert.strictEqual(data, 'hello world\n');
|
|
};
|
|
|
|
{
|
|
const dest = getTempFile();
|
|
const stream = new Utf8Stream({ dest });
|
|
stream.destroy();
|
|
stream.on('close', common.mustCall());
|
|
}
|