node/test/parallel/test-fs-write-stream-double-close.js
Matteo Collina acf56be536 fs: guarantee order of callbacks in ws.close
Refactor WriteStream.prototype.close and WriteStream.prototype._destroy
to always call the callback passed to close in order. Protects from
calling .close() without a callback.

Fixes: https://github.com/nodejs/node/issues/17951
See: https://github.com/nodejs/node/pull/15407

PR-URL: https://github.com/nodejs/node/pull/18002
Fixes: https://github.com/nodejs/node/issues/17951
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-01-08 09:17:16 +01:00

46 lines
873 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
common.refreshTmpDir();
{
const s = fs.createWriteStream(path.join(common.tmpDir, 'rw'));
s.close(common.mustCall());
s.close(common.mustCall());
}
{
const s = fs.createWriteStream(path.join(common.tmpDir, 'rw2'));
let emits = 0;
s.on('close', () => {
emits++;
});
s.close(common.mustCall(() => {
assert.strictEqual(emits, 1);
s.close(common.mustCall(() => {
assert.strictEqual(emits, 1);
}));
process.nextTick(() => {
s.close(common.mustCall(() => {
assert.strictEqual(emits, 1);
}));
});
}));
}
{
const s = fs.createWriteStream(path.join(common.tmpDir, 'rw'), {
autoClose: false
});
s.close(common.mustCall());
s.close(common.mustCall());
}