Files
moby/pkg/ioutils/readers_test.go
Sebastiaan van Stijn 4800a9b50d pkg/ioutils: remove // import comments
These comments were added to enforce using the correct import path for
our packages ("github.com/docker/docker", not "github.com/moby/moby").
However, when working in go module mode (not GOPATH / vendor), they have
no effect, so their impact is limited.

Remove these imports in preparation of migrating our code to become an
actual go module.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-30 15:59:18 +02:00

56 lines
1.2 KiB
Go

package ioutils
import (
"context"
"errors"
"io"
"strings"
"testing"
"time"
)
func TestReadCloserWrapperClose(t *testing.T) {
const text = "hello world"
testErr := errors.New("this will be called when closing")
wrapper := NewReadCloserWrapper(strings.NewReader(text), func() error {
return testErr
})
buf, err := io.ReadAll(wrapper)
if err != nil {
t.Errorf("io.ReadAll(wrapper) err = %v", err)
}
if string(buf) != text {
t.Errorf("expected %v, got: %v", text, string(buf))
}
err = wrapper.Close()
if !errors.Is(err, testErr) {
// readCloserWrapper should have called the anonymous func and thus, fail
t.Errorf("expected %v, got: %v", testErr, err)
}
}
type perpetualReader struct{}
func (p *perpetualReader) Read(buf []byte) (int, error) {
for i := 0; i != len(buf); i++ {
buf[i] = 'a'
}
return len(buf), nil
}
func TestCancelReadCloser(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
crc := NewCancelReadCloser(ctx, io.NopCloser(&perpetualReader{}))
for {
var buf [128]byte
_, err := crc.Read(buf[:])
if err == context.DeadlineExceeded {
break
} else if err != nil {
t.Fatalf("got unexpected error: %v", err)
}
}
}