github.com/jingleWang/moby@v1.13.1/pkg/pools/pools_test.go (about) 1 package pools 2 3 import ( 4 "bufio" 5 "bytes" 6 "io" 7 "strings" 8 "testing" 9 ) 10 11 func TestBufioReaderPoolGetWithNoReaderShouldCreateOne(t *testing.T) { 12 reader := BufioReader32KPool.Get(nil) 13 if reader == nil { 14 t.Fatalf("BufioReaderPool should have create a bufio.Reader but did not.") 15 } 16 } 17 18 func TestBufioReaderPoolPutAndGet(t *testing.T) { 19 sr := bufio.NewReader(strings.NewReader("foobar")) 20 reader := BufioReader32KPool.Get(sr) 21 if reader == nil { 22 t.Fatalf("BufioReaderPool should not return a nil reader.") 23 } 24 // verify the first 3 byte 25 buf1 := make([]byte, 3) 26 _, err := reader.Read(buf1) 27 if err != nil { 28 t.Fatal(err) 29 } 30 if actual := string(buf1); actual != "foo" { 31 t.Fatalf("The first letter should have been 'foo' but was %v", actual) 32 } 33 BufioReader32KPool.Put(reader) 34 // Try to read the next 3 bytes 35 _, err = sr.Read(make([]byte, 3)) 36 if err == nil || err != io.EOF { 37 t.Fatalf("The buffer should have been empty, issue an EOF error.") 38 } 39 } 40 41 type simpleReaderCloser struct { 42 io.Reader 43 closed bool 44 } 45 46 func (r *simpleReaderCloser) Close() error { 47 r.closed = true 48 return nil 49 } 50 51 func TestNewReadCloserWrapperWithAReadCloser(t *testing.T) { 52 br := bufio.NewReader(strings.NewReader("")) 53 sr := &simpleReaderCloser{ 54 Reader: strings.NewReader("foobar"), 55 closed: false, 56 } 57 reader := BufioReader32KPool.NewReadCloserWrapper(br, sr) 58 if reader == nil { 59 t.Fatalf("NewReadCloserWrapper should not return a nil reader.") 60 } 61 // Verify the content of reader 62 buf := make([]byte, 3) 63 _, err := reader.Read(buf) 64 if err != nil { 65 t.Fatal(err) 66 } 67 if actual := string(buf); actual != "foo" { 68 t.Fatalf("The first 3 letter should have been 'foo' but were %v", actual) 69 } 70 reader.Close() 71 // Read 3 more bytes "bar" 72 _, err = reader.Read(buf) 73 if err != nil { 74 t.Fatal(err) 75 } 76 if actual := string(buf); actual != "bar" { 77 t.Fatalf("The first 3 letter should have been 'bar' but were %v", actual) 78 } 79 if !sr.closed { 80 t.Fatalf("The ReaderCloser should have been closed, it is not.") 81 } 82 } 83 84 func TestBufioWriterPoolGetWithNoReaderShouldCreateOne(t *testing.T) { 85 writer := BufioWriter32KPool.Get(nil) 86 if writer == nil { 87 t.Fatalf("BufioWriterPool should have create a bufio.Writer but did not.") 88 } 89 } 90 91 func TestBufioWriterPoolPutAndGet(t *testing.T) { 92 buf := new(bytes.Buffer) 93 bw := bufio.NewWriter(buf) 94 writer := BufioWriter32KPool.Get(bw) 95 if writer == nil { 96 t.Fatalf("BufioReaderPool should not return a nil writer.") 97 } 98 written, err := writer.Write([]byte("foobar")) 99 if err != nil { 100 t.Fatal(err) 101 } 102 if written != 6 { 103 t.Fatalf("Should have written 6 bytes, but wrote %v bytes", written) 104 } 105 // Make sure we Flush all the way ? 106 writer.Flush() 107 bw.Flush() 108 if len(buf.Bytes()) != 6 { 109 t.Fatalf("The buffer should contain 6 bytes ('foobar') but contains %v ('%v')", buf.Bytes(), string(buf.Bytes())) 110 } 111 // Reset the buffer 112 buf.Reset() 113 BufioWriter32KPool.Put(writer) 114 // Try to write something 115 if _, err = writer.Write([]byte("barfoo")); err != nil { 116 t.Fatal(err) 117 } 118 // If we now try to flush it, it should panic (the writer is nil) 119 // recover it 120 defer func() { 121 if r := recover(); r == nil { 122 t.Fatal("Trying to flush the writter should have 'paniced', did not.") 123 } 124 }() 125 writer.Flush() 126 } 127 128 type simpleWriterCloser struct { 129 io.Writer 130 closed bool 131 } 132 133 func (r *simpleWriterCloser) Close() error { 134 r.closed = true 135 return nil 136 } 137 138 func TestNewWriteCloserWrapperWithAWriteCloser(t *testing.T) { 139 buf := new(bytes.Buffer) 140 bw := bufio.NewWriter(buf) 141 sw := &simpleWriterCloser{ 142 Writer: new(bytes.Buffer), 143 closed: false, 144 } 145 bw.Flush() 146 writer := BufioWriter32KPool.NewWriteCloserWrapper(bw, sw) 147 if writer == nil { 148 t.Fatalf("BufioReaderPool should not return a nil writer.") 149 } 150 written, err := writer.Write([]byte("foobar")) 151 if err != nil { 152 t.Fatal(err) 153 } 154 if written != 6 { 155 t.Fatalf("Should have written 6 bytes, but wrote %v bytes", written) 156 } 157 writer.Close() 158 if !sw.closed { 159 t.Fatalf("The ReaderCloser should have been closed, it is not.") 160 } 161 }