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 default: 51 // It should not be called again! 52 throw new Error('BOOM!'); 53 } 54}; 55 56fs.close = common.mustCall(function(fd_, cb) { 57 console.error('fs.close', fd_, stream.fd); 58 assert.strictEqual(fd_, stream.fd); 59 fs.closeSync(fd_); 60 process.nextTick(cb); 61}); 62 63stream.on('error', common.mustCall(function(err_) { 64 console.error('error handler'); 65 assert.strictEqual(stream.fd, null); 66 assert.strictEqual(err_, err); 67})); 68 69 70stream.write(Buffer.allocUnsafe(256), function() { 71 console.error('first cb'); 72 stream.write(Buffer.allocUnsafe(256), common.mustCall(function(err_) { 73 console.error('second cb'); 74 assert.strictEqual(err_, err); 75 })); 76}); 77