Added piper read timeout error

This commit is contained in:
Quentin Renard
2024-10-15 16:22:55 +02:00
parent 4b1f5effe9
commit c3c854cda4
2 changed files with 12 additions and 7 deletions

9
io.go
View File

@@ -123,8 +123,8 @@ func (w *WriterAdapter) write(i []byte) {
}
}
// Piper doesn't block on writes. It will block on reads unless you provide a ReadTimeout
// in which case it will return, after the provided timeout, if no read is available. When closing the
// Piper doesn't block on writes. It will block on reads unless you provide a ReadTimeout in which case
// it will return an optional error, after the provided timeout, if no read is available. When closing the
// piper, it will interrupt any ongoing read/future writes and return io.EOF.
// Piper doesn't handle multiple readers at the same time.
type Piper struct {
@@ -136,7 +136,8 @@ type Piper struct {
}
type PiperOptions struct {
ReadTimeout time.Duration
ReadTimeout time.Duration
ReadTimeoutError error
}
func NewPiper(o PiperOptions) *Piper {
@@ -191,7 +192,7 @@ func (p *Piper) Read(i []byte) (n int, err error) {
for {
// Check context
if ctx != nil && ctx.Err() != nil {
return 0, nil
return 0, p.o.ReadTimeoutError
}
// Lock

View File

@@ -157,7 +157,11 @@ func TestPiper(t *testing.T) {
}
// Piper should timeout on read if a read timeout is provided
p2 := NewPiper(PiperOptions{ReadTimeout: time.Millisecond})
e1 := errors.New("1")
p2 := NewPiper(PiperOptions{
ReadTimeout: time.Millisecond,
ReadTimeoutError: e1,
})
defer p2.Close()
ctx6, cancel6 := context.WithTimeout(context.Background(), time.Second)
defer cancel6()
@@ -169,7 +173,7 @@ func TestPiper(t *testing.T) {
if errCtx := ctx6.Err(); errors.Is(errCtx, context.DeadlineExceeded) {
t.Fatalf("expected no deadline exceeded error, got %+v", errCtx)
}
if err != nil {
t.Fatalf("expected nil, got %+v", err)
if !errors.Is(err, e1) {
t.Fatalf("expected %s, got %s", e1, err)
}
}