Skip to content

Commit 4c33e5d

Browse files
joyeecheungRafaelGSS
authored andcommitted
test: avoid race in file write stream handle tests
The test previously created two fs.promises.open calls on the same file with w+ back-to-back, and one of them could fail when checking the contents of that file if the other happened to be opening the file for write. Split them into different tests (with different tmpdir) to avoid the race. PR-URL: #44380 Refs: nodejs/reliability#354 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
1 parent c5413a1 commit 4c33e5d

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fs = require('fs');
4+
const path = require('path');
5+
const assert = require('assert');
6+
const tmpdir = require('../common/tmpdir');
7+
const file = path.join(tmpdir.path, 'write_stream_filehandle_test.txt');
8+
const input = 'hello world';
9+
10+
tmpdir.refresh();
11+
12+
fs.promises.open(file, 'w+').then((handle) => {
13+
let calls = 0;
14+
const {
15+
write: originalWriteFunction,
16+
writev: originalWritevFunction
17+
} = handle;
18+
handle.write = function write() {
19+
calls++;
20+
return Reflect.apply(originalWriteFunction, this, arguments);
21+
};
22+
handle.writev = function writev() {
23+
calls++;
24+
return Reflect.apply(originalWritevFunction, this, arguments);
25+
};
26+
const stream = fs.createWriteStream(null, { fd: handle });
27+
28+
stream.end(input);
29+
stream.on('close', common.mustCall(() => {
30+
assert(calls > 0, 'expected at least one call to fileHandle.write or ' +
31+
'fileHandle.writev, got 0');
32+
}));
33+
}).then(common.mustCall());

test/parallel/test-fs-write-stream-file-handle.js

-23
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,3 @@ fs.promises.open(file, 'w+').then((handle) => {
1919
assert.strictEqual(output, input);
2020
}));
2121
}).then(common.mustCall());
22-
23-
fs.promises.open(file, 'w+').then((handle) => {
24-
let calls = 0;
25-
const {
26-
write: originalWriteFunction,
27-
writev: originalWritevFunction
28-
} = handle;
29-
handle.write = function write() {
30-
calls++;
31-
return Reflect.apply(originalWriteFunction, this, arguments);
32-
};
33-
handle.writev = function writev() {
34-
calls++;
35-
return Reflect.apply(originalWritevFunction, this, arguments);
36-
};
37-
const stream = fs.createWriteStream(null, { fd: handle });
38-
39-
stream.end(input);
40-
stream.on('close', common.mustCall(() => {
41-
assert(calls > 0, 'expected at least one call to fileHandle.write or ' +
42-
'fileHandle.writev, got 0');
43-
}));
44-
}).then(common.mustCall());

0 commit comments

Comments
 (0)