var FlushWritable=require("../lib/FlushWritable"),Readable=require("stream").Readable,util=require("util"),should=require("should");function TestWritable(){FlushWritable.call(this)}function TestReadable(){this.i=0,Readable.call(this)}util.inherits(TestWritable,FlushWritable),TestWritable.prototype._flush=function(e){this.flushCalled=!0,setTimeout(function(){e(this.err)}.bind(this),10)},TestWritable.prototype._write=function(e,i,t){t()},util.inherits(TestReadable,Readable),TestReadable.prototype._read=function(){2==this.i++?this.push(null):this.push("foo")},describe("FlushWritable",function(){it("should call _flush prior to emitting finish",function(e){var i=new TestReadable,t=new TestWritable,l=!1;t.on("finish",function(){l=!0}),i.pipe(t),setTimeout(function(){t.should.have.property("flushCalled"),l.should.eql(!1),setTimeout(function(){l.should.eql(!0),e()},10)},5)}),it("should emit error instead of finish for errors in cb",function(e){var i=new TestReadable,t=new TestWritable,l=!1,s=!1;t.on("finish",function(){l=!0}),t.on("error",function(){s=!0}),t.err=new Error("bar"),i.pipe(t),setTimeout(function(){l.should.eql(!1),s.should.eql(!0),e()},15)}),it("should finish immediately if no _flush is defined",function(e){var i=new TestReadable,t=new TestWritable,l=!1;t.on("finish",function(){l=!0}),t._flush=void 0,i.pipe(t),setTimeout(function(){l.should.eql(!0),e()},5)})});