2015-11-29 20:41:19 +05:30
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
2017-12-24 22:38:11 -08:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
|
2023-08-15 22:45:14 +09:00
|
|
|
const file = tmpdir.resolve('write-autoclose-opt1.txt');
|
2017-12-24 22:38:11 -08:00
|
|
|
tmpdir.refresh();
|
2017-07-10 20:55:21 -04:00
|
|
|
let stream = fs.createWriteStream(file, { flags: 'w+', autoClose: false });
|
2015-11-29 20:41:19 +05:30
|
|
|
stream.write('Test1');
|
|
|
|
stream.end();
|
|
|
|
stream.on('finish', common.mustCall(function() {
|
2018-01-05 10:41:14 +01:00
|
|
|
stream.on('close', common.mustNotCall());
|
2015-11-29 20:41:19 +05:30
|
|
|
process.nextTick(common.mustCall(function() {
|
2018-01-05 10:41:14 +01:00
|
|
|
assert.strictEqual(stream.closed, false);
|
2016-09-17 12:32:33 +02:00
|
|
|
assert.notStrictEqual(stream.fd, null);
|
2015-11-29 20:41:19 +05:30
|
|
|
next();
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
function next() {
|
|
|
|
// This will tell us if the fd is usable again or not
|
2017-07-10 20:55:21 -04:00
|
|
|
stream = fs.createWriteStream(null, { fd: stream.fd, start: 0 });
|
2015-11-29 20:41:19 +05:30
|
|
|
stream.write('Test2');
|
|
|
|
stream.end();
|
|
|
|
stream.on('finish', common.mustCall(function() {
|
2018-01-05 10:41:14 +01:00
|
|
|
assert.strictEqual(stream.closed, false);
|
|
|
|
stream.on('close', common.mustCall(function() {
|
2020-02-14 13:32:45 +01:00
|
|
|
assert.strictEqual(stream.fd, null);
|
2018-01-05 10:41:14 +01:00
|
|
|
assert.strictEqual(stream.closed, true);
|
|
|
|
process.nextTick(next2);
|
|
|
|
}));
|
2015-11-29 20:41:19 +05:30
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function next2() {
|
|
|
|
// This will test if after reusing the fd data is written properly
|
|
|
|
fs.readFile(file, function(err, data) {
|
2016-12-30 10:54:01 -05:00
|
|
|
assert.ifError(err);
|
2015-11-29 20:41:19 +05:30
|
|
|
assert.strictEqual(data.toString(), 'Test2');
|
|
|
|
process.nextTick(common.mustCall(next3));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function next3() {
|
|
|
|
// This is to test success scenario where autoClose is true
|
2017-07-10 20:55:21 -04:00
|
|
|
const stream = fs.createWriteStream(file, { autoClose: true });
|
2015-11-29 20:41:19 +05:30
|
|
|
stream.write('Test3');
|
|
|
|
stream.end();
|
|
|
|
stream.on('finish', common.mustCall(function() {
|
2018-01-05 10:41:14 +01:00
|
|
|
assert.strictEqual(stream.closed, false);
|
|
|
|
stream.on('close', common.mustCall(function() {
|
2020-02-14 13:32:45 +01:00
|
|
|
assert.strictEqual(stream.fd, null);
|
2015-11-29 20:41:19 +05:30
|
|
|
assert.strictEqual(stream.closed, true);
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|