11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst path = require('path');
51cb0ef41Sopenharmony_ciconst fs = require('fs');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst file = path.join(tmpdir.path, 'write-autoclose-opt1.txt');
101cb0ef41Sopenharmony_citmpdir.refresh();
111cb0ef41Sopenharmony_cilet stream = fs.createWriteStream(file, { flags: 'w+', autoClose: false });
121cb0ef41Sopenharmony_cistream.write('Test1');
131cb0ef41Sopenharmony_cistream.end();
141cb0ef41Sopenharmony_cistream.on('finish', common.mustCall(function() {
151cb0ef41Sopenharmony_ci  stream.on('close', common.mustNotCall());
161cb0ef41Sopenharmony_ci  process.nextTick(common.mustCall(function() {
171cb0ef41Sopenharmony_ci    assert.strictEqual(stream.closed, false);
181cb0ef41Sopenharmony_ci    assert.notStrictEqual(stream.fd, null);
191cb0ef41Sopenharmony_ci    next();
201cb0ef41Sopenharmony_ci  }));
211cb0ef41Sopenharmony_ci}));
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cifunction next() {
241cb0ef41Sopenharmony_ci  // This will tell us if the fd is usable again or not
251cb0ef41Sopenharmony_ci  stream = fs.createWriteStream(null, { fd: stream.fd, start: 0 });
261cb0ef41Sopenharmony_ci  stream.write('Test2');
271cb0ef41Sopenharmony_ci  stream.end();
281cb0ef41Sopenharmony_ci  stream.on('finish', common.mustCall(function() {
291cb0ef41Sopenharmony_ci    assert.strictEqual(stream.closed, false);
301cb0ef41Sopenharmony_ci    stream.on('close', common.mustCall(function() {
311cb0ef41Sopenharmony_ci      assert.strictEqual(stream.fd, null);
321cb0ef41Sopenharmony_ci      assert.strictEqual(stream.closed, true);
331cb0ef41Sopenharmony_ci      process.nextTick(next2);
341cb0ef41Sopenharmony_ci    }));
351cb0ef41Sopenharmony_ci  }));
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cifunction next2() {
391cb0ef41Sopenharmony_ci  // This will test if after reusing the fd data is written properly
401cb0ef41Sopenharmony_ci  fs.readFile(file, function(err, data) {
411cb0ef41Sopenharmony_ci    assert.ifError(err);
421cb0ef41Sopenharmony_ci    assert.strictEqual(data.toString(), 'Test2');
431cb0ef41Sopenharmony_ci    process.nextTick(common.mustCall(next3));
441cb0ef41Sopenharmony_ci  });
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cifunction next3() {
481cb0ef41Sopenharmony_ci  // This is to test success scenario where autoClose is true
491cb0ef41Sopenharmony_ci  const stream = fs.createWriteStream(file, { autoClose: true });
501cb0ef41Sopenharmony_ci  stream.write('Test3');
511cb0ef41Sopenharmony_ci  stream.end();
521cb0ef41Sopenharmony_ci  stream.on('finish', common.mustCall(function() {
531cb0ef41Sopenharmony_ci    assert.strictEqual(stream.closed, false);
541cb0ef41Sopenharmony_ci    stream.on('close', common.mustCall(function() {
551cb0ef41Sopenharmony_ci      assert.strictEqual(stream.fd, null);
561cb0ef41Sopenharmony_ci      assert.strictEqual(stream.closed, true);
571cb0ef41Sopenharmony_ci    }));
581cb0ef41Sopenharmony_ci  }));
591cb0ef41Sopenharmony_ci}
60