Skip to content

Commit de9b1c9

Browse files
author
Jacob Wirth
committed
Update Noop example for ChanReader
1 parent 0bb09cb commit de9b1c9

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

CREATING_TOXICS.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,13 @@ An implementation of the noop toxic above using the stream package would look so
145145
```go
146146
func (t *NoopToxic) Pipe(stub *toxics.ToxicStub) {
147147
buf := make([]byte, 32*1024)
148-
writer := stream.NewChanWriter(stub.Output)
149-
reader := stream.NewChanReader(stub.Input)
150-
reader.SetInterrupt(stub.Interrupt)
151148
for {
152-
n, err := reader.Read(buf)
153-
if err == stream.ErrInterrupted {
154-
writer.Write(buf[:n])
155-
return
156-
} else if err == io.EOF {
157-
stub.Close()
149+
n, err := stub.Reader.Read(buf)
150+
if stub.HandleReadError(err) {
158151
return
159152
}
160-
writer.Write(buf[:n])
153+
stub.Writer.Write(buf[:n])
154+
stub.Reader.Checkpoint(0)
161155
}
162156
}
163157
```

stream/io_chan_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,20 @@ func TestReadWriterFlush(t *testing.T) {
380380
AssertClosed(t, rw, []byte("rld"))
381381
}
382382

383+
func TestReadWriterDoubleFlush(t *testing.T) {
384+
rw := NewTestReadWriter()
385+
buf := make([]byte, 8)
386+
387+
AssertRead(t, rw, buf, "hello wo", nil)
388+
rw.FlushTo(rw)
389+
rw.FlushTo(rw)
390+
AssertRead(t, rw, buf, "foobar", nil)
391+
rw.FlushTo(rw)
392+
AssertRead(t, rw, buf, "", io.EOF)
393+
394+
AssertClosed(t, rw, []byte("rld"))
395+
}
396+
383397
func TestReadWriterNoCheckpoint(t *testing.T) {
384398
rw := NewTestReadWriter()
385399
buf := make([]byte, 32)

0 commit comments

Comments
 (0)