1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const fs = require('fs'); 26 27const tmpdir = require('../common/tmpdir'); 28tmpdir.refresh(); 29 30const stream = fs.createWriteStream(`${tmpdir.path}/out`, { 31 highWaterMark: 10 32}); 33const err = new Error('BAM'); 34 35const write = fs.write; 36let writeCalls = 0; 37fs.write = function() { 38 switch (writeCalls++) { 39 case 0: 40 console.error('first write'); 41 // First time is ok. 42 return write.apply(fs, arguments); 43 case 1: { 44 // Then it breaks. 45 console.error('second write'); 46 const cb = arguments[arguments.length - 1]; 47 return process.nextTick(function() { 48 cb(err); 49 }); 50 } 51 default: 52 // It should not be called again! 53 throw new Error('BOOM!'); 54 } 55}; 56 57fs.close = common.mustCall(function(fd_, cb) { 58 console.error('fs.close', fd_, stream.fd); 59 assert.strictEqual(fd_, stream.fd); 60 fs.closeSync(fd_); 61 process.nextTick(cb); 62}); 63 64stream.on('error', common.mustCall(function(err_) { 65 console.error('error handler'); 66 assert.strictEqual(stream.fd, null); 67 assert.strictEqual(err_, err); 68})); 69 70 71stream.write(Buffer.allocUnsafe(256), function() { 72 console.error('first cb'); 73 stream.write(Buffer.allocUnsafe(256), common.mustCall(function(err_) { 74 console.error('second cb'); 75 assert.strictEqual(err_, err); 76 })); 77}); 78