github.com/torfuzx/docker@v1.8.1/pkg/ioutils/multireader_test.go (about) 1 package ioutils 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "strings" 10 "testing" 11 ) 12 13 func TestMultiReadSeekerReadAll(t *testing.T) { 14 str := "hello world" 15 s1 := strings.NewReader(str + " 1") 16 s2 := strings.NewReader(str + " 2") 17 s3 := strings.NewReader(str + " 3") 18 mr := MultiReadSeeker(s1, s2, s3) 19 20 expectedSize := int64(s1.Len() + s2.Len() + s3.Len()) 21 22 b, err := ioutil.ReadAll(mr) 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 expected := "hello world 1hello world 2hello world 3" 28 if string(b) != expected { 29 t.Fatalf("ReadAll failed, got: %q, expected %q", string(b), expected) 30 } 31 32 size, err := mr.Seek(0, os.SEEK_END) 33 if err != nil { 34 t.Fatal(err) 35 } 36 if size != expectedSize { 37 t.Fatalf("reader size does not match, got %d, expected %d", size, expectedSize) 38 } 39 40 // Reset the position and read again 41 pos, err := mr.Seek(0, os.SEEK_SET) 42 if err != nil { 43 t.Fatal(err) 44 } 45 if pos != 0 { 46 t.Fatalf("expected position to be set to 0, got %d", pos) 47 } 48 49 b, err = ioutil.ReadAll(mr) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 if string(b) != expected { 55 t.Fatalf("ReadAll failed, got: %q, expected %q", string(b), expected) 56 } 57 } 58 59 func TestMultiReadSeekerReadEach(t *testing.T) { 60 str := "hello world" 61 s1 := strings.NewReader(str + " 1") 62 s2 := strings.NewReader(str + " 2") 63 s3 := strings.NewReader(str + " 3") 64 mr := MultiReadSeeker(s1, s2, s3) 65 66 var totalBytes int64 67 for i, s := range []*strings.Reader{s1, s2, s3} { 68 sLen := int64(s.Len()) 69 buf := make([]byte, s.Len()) 70 expected := []byte(fmt.Sprintf("%s %d", str, i+1)) 71 72 if _, err := mr.Read(buf); err != nil && err != io.EOF { 73 t.Fatal(err) 74 } 75 76 if !bytes.Equal(buf, expected) { 77 t.Fatalf("expected %q to be %q", string(buf), string(expected)) 78 } 79 80 pos, err := mr.Seek(0, os.SEEK_CUR) 81 if err != nil { 82 t.Fatalf("iteration: %d, error: %v", i+1, err) 83 } 84 85 // check that the total bytes read is the current position of the seeker 86 totalBytes += sLen 87 if pos != totalBytes { 88 t.Fatalf("expected current position to be: %d, got: %d, iteration: %d", totalBytes, pos, i+1) 89 } 90 91 // This tests not only that SEEK_SET and SEEK_CUR give the same values, but that the next iteration is in the expected position as well 92 newPos, err := mr.Seek(pos, os.SEEK_SET) 93 if err != nil { 94 t.Fatal(err) 95 } 96 if newPos != pos { 97 t.Fatalf("expected to get same position when calling SEEK_SET with value from SEEK_CUR, cur: %d, set: %d", pos, newPos) 98 } 99 } 100 } 101 102 func TestMultiReadSeekerReadSpanningChunks(t *testing.T) { 103 str := "hello world" 104 s1 := strings.NewReader(str + " 1") 105 s2 := strings.NewReader(str + " 2") 106 s3 := strings.NewReader(str + " 3") 107 mr := MultiReadSeeker(s1, s2, s3) 108 109 buf := make([]byte, s1.Len()+3) 110 _, err := mr.Read(buf) 111 if err != nil { 112 t.Fatal(err) 113 } 114 115 // expected is the contents of s1 + 3 bytes from s2, ie, the `hel` at the end of this string 116 expected := "hello world 1hel" 117 if string(buf) != expected { 118 t.Fatalf("expected %s to be %s", string(buf), expected) 119 } 120 } 121 122 func TestMultiReadSeekerNegativeSeek(t *testing.T) { 123 str := "hello world" 124 s1 := strings.NewReader(str + " 1") 125 s2 := strings.NewReader(str + " 2") 126 s3 := strings.NewReader(str + " 3") 127 mr := MultiReadSeeker(s1, s2, s3) 128 129 s1Len := s1.Len() 130 s2Len := s2.Len() 131 s3Len := s3.Len() 132 133 s, err := mr.Seek(int64(-1*s3.Len()), os.SEEK_END) 134 if err != nil { 135 t.Fatal(err) 136 } 137 if s != int64(s1Len+s2Len) { 138 t.Fatalf("expected %d to be %d", s, s1.Len()+s2.Len()) 139 } 140 141 buf := make([]byte, s3Len) 142 if _, err := mr.Read(buf); err != nil && err != io.EOF { 143 t.Fatal(err) 144 } 145 expected := fmt.Sprintf("%s %d", str, 3) 146 if string(buf) != fmt.Sprintf("%s %d", str, 3) { 147 t.Fatalf("expected %q to be %q", string(buf), expected) 148 } 149 }