github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/ioutils/readers_test.go (about) 1 package ioutils // import "github.com/Prakhar-Agarwal-byte/moby/pkg/ioutils" 2 3 import ( 4 "context" 5 "errors" 6 "io" 7 "strings" 8 "testing" 9 "testing/iotest" 10 "time" 11 ) 12 13 func TestReadCloserWrapperClose(t *testing.T) { 14 const text = "hello world" 15 testErr := errors.New("this will be called when closing") 16 wrapper := NewReadCloserWrapper(strings.NewReader(text), func() error { 17 return testErr 18 }) 19 20 buf, err := io.ReadAll(wrapper) 21 if err != nil { 22 t.Errorf("io.ReadAll(wrapper) err = %v", err) 23 } 24 if string(buf) != text { 25 t.Errorf("expected %v, got: %v", text, string(buf)) 26 } 27 err = wrapper.Close() 28 if !errors.Is(err, testErr) { 29 // readCloserWrapper should have called the anonymous func and thus, fail 30 t.Errorf("expected %v, got: %v", testErr, err) 31 } 32 } 33 34 func TestReaderErrWrapperReadOnError(t *testing.T) { 35 called := false 36 expectedErr := errors.New("error reader always fail") 37 wrapper := NewReaderErrWrapper(iotest.ErrReader(expectedErr), func() { 38 called = true 39 }) 40 _, err := wrapper.Read([]byte{}) 41 if !errors.Is(err, expectedErr) { 42 t.Errorf("expected %v, got: %v", expectedErr, err) 43 } 44 if !called { 45 t.Fatalf("readErrWrapper should have called the anonymous function on failure") 46 } 47 } 48 49 func TestReaderErrWrapperRead(t *testing.T) { 50 const text = "hello world" 51 wrapper := NewReaderErrWrapper(strings.NewReader(text), func() { 52 t.Fatalf("readErrWrapper should not have called the anonymous function") 53 }) 54 num, err := wrapper.Read(make([]byte, len(text)+10)) 55 if err != nil { 56 t.Error(err) 57 } 58 if expected := len(text); num != expected { 59 t.Errorf("readerErrWrapper should have read %d byte, but read %d", expected, num) 60 } 61 } 62 63 type perpetualReader struct{} 64 65 func (p *perpetualReader) Read(buf []byte) (n int, err error) { 66 for i := 0; i != len(buf); i++ { 67 buf[i] = 'a' 68 } 69 return len(buf), nil 70 } 71 72 func TestCancelReadCloser(t *testing.T) { 73 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) 74 defer cancel() 75 crc := NewCancelReadCloser(ctx, io.NopCloser(&perpetualReader{})) 76 for { 77 var buf [128]byte 78 _, err := crc.Read(buf[:]) 79 if err == context.DeadlineExceeded { 80 break 81 } else if err != nil { 82 t.Fatalf("got unexpected error: %v", err) 83 } 84 } 85 }