Skip to content

Commit

Permalink
Update Noop example for ChanReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Wirth committed Oct 4, 2016
1 parent 0bb09cb commit de9b1c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
14 changes: 4 additions & 10 deletions CREATING_TOXICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,13 @@ An implementation of the noop toxic above using the stream package would look so
```go
func (t *NoopToxic) Pipe(stub *toxics.ToxicStub) {
buf := make([]byte, 32*1024)
writer := stream.NewChanWriter(stub.Output)
reader := stream.NewChanReader(stub.Input)
reader.SetInterrupt(stub.Interrupt)
for {
n, err := reader.Read(buf)
if err == stream.ErrInterrupted {
writer.Write(buf[:n])
return
} else if err == io.EOF {
stub.Close()
n, err := stub.Reader.Read(buf)
if stub.HandleReadError(err) {
return
}
writer.Write(buf[:n])
stub.Writer.Write(buf[:n])
stub.Reader.Checkpoint(0)
}
}
```
Expand Down
14 changes: 14 additions & 0 deletions stream/io_chan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,20 @@ func TestReadWriterFlush(t *testing.T) {
AssertClosed(t, rw, []byte("rld"))
}

func TestReadWriterDoubleFlush(t *testing.T) {
rw := NewTestReadWriter()
buf := make([]byte, 8)

AssertRead(t, rw, buf, "hello wo", nil)
rw.FlushTo(rw)
rw.FlushTo(rw)
AssertRead(t, rw, buf, "foobar", nil)
rw.FlushTo(rw)
AssertRead(t, rw, buf, "", io.EOF)

AssertClosed(t, rw, []byte("rld"))
}

func TestReadWriterNoCheckpoint(t *testing.T) {
rw := NewTestReadWriter()
buf := make([]byte, 32)
Expand Down

0 comments on commit de9b1c9

Please sign in to comment.