github.com/glycerine/docker@v1.8.2/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 written, err = writer.Write([]byte("barfoo")) 116 if err != nil { 117 t.Fatal(err) 118 } 119 // If we now try to flush it, it should panic (the writer is nil) 120 // recover it 121 defer func() { 122 if r := recover(); r == nil { 123 t.Fatal("Trying to flush the writter should have 'paniced', did not.") 124 } 125 }() 126 writer.Flush() 127 } 128 129 type simpleWriterCloser struct { 130 io.Writer 131 closed bool 132 } 133 134 func (r *simpleWriterCloser) Close() error { 135 r.closed = true 136 return nil 137 } 138 139 func TestNewWriteCloserWrapperWithAWriteCloser(t *testing.T) { 140 buf := new(bytes.Buffer) 141 bw := bufio.NewWriter(buf) 142 sw := &simpleWriterCloser{ 143 Writer: new(bytes.Buffer), 144 closed: false, 145 } 146 bw.Flush() 147 writer := BufioWriter32KPool.NewWriteCloserWrapper(bw, sw) 148 if writer == nil { 149 t.Fatalf("BufioReaderPool should not return a nil writer.") 150 } 151 written, err := writer.Write([]byte("foobar")) 152 if err != nil { 153 t.Fatal(err) 154 } 155 if written != 6 { 156 t.Fatalf("Should have written 6 bytes, but wrote %v bytes", written) 157 } 158 writer.Close() 159 if !sw.closed { 160 t.Fatalf("The ReaderCloser should have been closed, it is not.") 161 } 162 }