11cb0ef41Sopenharmony_ci// Flags: --expose-internals 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst common = require('../common'); 51cb0ef41Sopenharmony_ciconst assert = require('assert'); 61cb0ef41Sopenharmony_ciconst fs = require('fs'); 71cb0ef41Sopenharmony_ciconst path = require('path'); 81cb0ef41Sopenharmony_ciconst SyncWriteStream = require('internal/fs/sync_write_stream'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 111cb0ef41Sopenharmony_citmpdir.refresh(); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst filename = path.join(tmpdir.path, 'sync-write-stream.txt'); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci// Verify constructing the instance with default options. 161cb0ef41Sopenharmony_ci{ 171cb0ef41Sopenharmony_ci const stream = new SyncWriteStream(1); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci assert.strictEqual(stream.fd, 1); 201cb0ef41Sopenharmony_ci assert.strictEqual(stream.readable, false); 211cb0ef41Sopenharmony_ci assert.strictEqual(stream.autoClose, true); 221cb0ef41Sopenharmony_ci} 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci// Verify constructing the instance with specified options. 251cb0ef41Sopenharmony_ci{ 261cb0ef41Sopenharmony_ci const stream = new SyncWriteStream(1, { autoClose: false }); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci assert.strictEqual(stream.fd, 1); 291cb0ef41Sopenharmony_ci assert.strictEqual(stream.readable, false); 301cb0ef41Sopenharmony_ci assert.strictEqual(stream.autoClose, false); 311cb0ef41Sopenharmony_ci} 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci// Verify that the file will be written synchronously. 341cb0ef41Sopenharmony_ci{ 351cb0ef41Sopenharmony_ci const fd = fs.openSync(filename, 'w'); 361cb0ef41Sopenharmony_ci const stream = new SyncWriteStream(fd); 371cb0ef41Sopenharmony_ci const chunk = Buffer.from('foo'); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci let calledSynchronously = false; 401cb0ef41Sopenharmony_ci stream._write(chunk, null, common.mustCall(() => { 411cb0ef41Sopenharmony_ci calledSynchronously = true; 421cb0ef41Sopenharmony_ci }, 1)); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci assert.ok(calledSynchronously); 451cb0ef41Sopenharmony_ci assert.strictEqual(fs.readFileSync(filename).equals(chunk), true); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci fs.closeSync(fd); 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci// Verify that the stream will unset the fd after destroy(). 511cb0ef41Sopenharmony_ci{ 521cb0ef41Sopenharmony_ci const fd = fs.openSync(filename, 'w'); 531cb0ef41Sopenharmony_ci const stream = new SyncWriteStream(fd); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci stream.on('close', common.mustCall()); 561cb0ef41Sopenharmony_ci assert.strictEqual(stream.destroy(), stream); 571cb0ef41Sopenharmony_ci assert.strictEqual(stream.fd, null); 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci// Verify that the stream will unset the fd after destroySoon(). 611cb0ef41Sopenharmony_ci{ 621cb0ef41Sopenharmony_ci const fd = fs.openSync(filename, 'w'); 631cb0ef41Sopenharmony_ci const stream = new SyncWriteStream(fd); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci stream.on('close', common.mustCall()); 661cb0ef41Sopenharmony_ci assert.strictEqual(stream.destroySoon(), stream); 671cb0ef41Sopenharmony_ci assert.strictEqual(stream.fd, null); 681cb0ef41Sopenharmony_ci} 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci// Verify that calling end() will also destroy the stream. 711cb0ef41Sopenharmony_ci{ 721cb0ef41Sopenharmony_ci const fd = fs.openSync(filename, 'w'); 731cb0ef41Sopenharmony_ci const stream = new SyncWriteStream(fd); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci assert.strictEqual(stream.fd, fd); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci stream.end(); 781cb0ef41Sopenharmony_ci stream.on('close', common.mustCall(() => { 791cb0ef41Sopenharmony_ci assert.strictEqual(stream.fd, null); 801cb0ef41Sopenharmony_ci })); 811cb0ef41Sopenharmony_ci} 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci// Verify that an error on _write() triggers an 'error' event. 841cb0ef41Sopenharmony_ci{ 851cb0ef41Sopenharmony_ci const fd = fs.openSync(filename, 'w'); 861cb0ef41Sopenharmony_ci const stream = new SyncWriteStream(fd); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci assert.strictEqual(stream.fd, fd); 891cb0ef41Sopenharmony_ci stream._write({}, null, common.mustCall((err) => { 901cb0ef41Sopenharmony_ci assert(err); 911cb0ef41Sopenharmony_ci fs.closeSync(fd); 921cb0ef41Sopenharmony_ci })); 931cb0ef41Sopenharmony_ci} 94