2017-04-11 08:24:42 +08:00
|
|
|
// Flags: --expose-internals
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2018-05-03 14:40:48 -04:00
|
|
|
const SyncWriteStream = require('internal/fs/utils').SyncWriteStream;
|
2017-04-11 08:24:42 +08:00
|
|
|
|
2017-12-24 22:38:11 -08:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
2017-04-11 08:24:42 +08:00
|
|
|
|
2017-12-24 22:38:11 -08:00
|
|
|
const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
|
2017-04-11 08:24:42 +08:00
|
|
|
|
2017-06-16 17:49:20 +02:00
|
|
|
// Verify constructing the instance with default options.
|
2017-04-11 08:24:42 +08:00
|
|
|
{
|
|
|
|
const stream = new SyncWriteStream(1);
|
|
|
|
|
|
|
|
assert.strictEqual(stream.fd, 1);
|
|
|
|
assert.strictEqual(stream.readable, false);
|
|
|
|
assert.strictEqual(stream.autoClose, true);
|
|
|
|
assert.strictEqual(stream.listenerCount('end'), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify constructing the instance with specified options.
|
|
|
|
{
|
|
|
|
const stream = new SyncWriteStream(1, { autoClose: false });
|
|
|
|
|
|
|
|
assert.strictEqual(stream.fd, 1);
|
|
|
|
assert.strictEqual(stream.readable, false);
|
|
|
|
assert.strictEqual(stream.autoClose, false);
|
|
|
|
assert.strictEqual(stream.listenerCount('end'), 1);
|
|
|
|
}
|
|
|
|
|
2018-01-06 19:34:27 +01:00
|
|
|
// Verify that the file will be written synchronously.
|
2017-04-11 08:24:42 +08:00
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
|
|
|
const chunk = Buffer.from('foo');
|
|
|
|
|
|
|
|
assert.strictEqual(stream._write(chunk, null, common.mustCall(1)), true);
|
|
|
|
assert.strictEqual(fs.readFileSync(filename).equals(chunk), true);
|
|
|
|
}
|
|
|
|
|
2017-06-16 17:49:20 +02:00
|
|
|
// Verify that the stream will unset the fd after destroy().
|
2017-04-11 08:24:42 +08:00
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
|
|
|
|
|
|
|
stream.on('close', common.mustCall(3));
|
|
|
|
|
|
|
|
assert.strictEqual(stream.destroy(), true);
|
|
|
|
assert.strictEqual(stream.fd, null);
|
|
|
|
assert.strictEqual(stream.destroy(), true);
|
|
|
|
assert.strictEqual(stream.destroySoon(), true);
|
|
|
|
}
|
|
|
|
|
2018-01-06 19:34:27 +01:00
|
|
|
// Verify that the 'end' event listener will also destroy the stream.
|
2017-04-11 08:24:42 +08:00
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
|
|
|
|
|
|
|
assert.strictEqual(stream.fd, fd);
|
|
|
|
|
|
|
|
stream.emit('end');
|
|
|
|
assert.strictEqual(stream.fd, null);
|
|
|
|
}
|