2018-02-05 16:05:59 -05:00
|
|
|
package pools // import "github.com/docker/docker/pkg/pools"
|
2015-05-15 14:13:54 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-09-11 14:55:05 -04:00
|
|
|
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2015-05-15 14:13:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBufioReaderPoolGetWithNoReaderShouldCreateOne(t *testing.T) {
|
|
|
|
reader := BufioReader32KPool.Get(nil)
|
|
|
|
if reader == nil {
|
|
|
|
t.Fatalf("BufioReaderPool should have create a bufio.Reader but did not.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBufioReaderPoolPutAndGet(t *testing.T) {
|
|
|
|
sr := bufio.NewReader(strings.NewReader("foobar"))
|
|
|
|
reader := BufioReader32KPool.Get(sr)
|
|
|
|
if reader == nil {
|
|
|
|
t.Fatalf("BufioReaderPool should not return a nil reader.")
|
|
|
|
}
|
|
|
|
// verify the first 3 byte
|
|
|
|
buf1 := make([]byte, 3)
|
|
|
|
_, err := reader.Read(buf1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if actual := string(buf1); actual != "foo" {
|
|
|
|
t.Fatalf("The first letter should have been 'foo' but was %v", actual)
|
|
|
|
}
|
|
|
|
BufioReader32KPool.Put(reader)
|
|
|
|
// Try to read the next 3 bytes
|
|
|
|
_, err = sr.Read(make([]byte, 3))
|
|
|
|
if err == nil || err != io.EOF {
|
|
|
|
t.Fatalf("The buffer should have been empty, issue an EOF error.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type simpleReaderCloser struct {
|
|
|
|
io.Reader
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *simpleReaderCloser) Close() error {
|
|
|
|
r.closed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewReadCloserWrapperWithAReadCloser(t *testing.T) {
|
|
|
|
br := bufio.NewReader(strings.NewReader(""))
|
|
|
|
sr := &simpleReaderCloser{
|
|
|
|
Reader: strings.NewReader("foobar"),
|
|
|
|
closed: false,
|
|
|
|
}
|
|
|
|
reader := BufioReader32KPool.NewReadCloserWrapper(br, sr)
|
|
|
|
if reader == nil {
|
|
|
|
t.Fatalf("NewReadCloserWrapper should not return a nil reader.")
|
|
|
|
}
|
|
|
|
// Verify the content of reader
|
|
|
|
buf := make([]byte, 3)
|
|
|
|
_, err := reader.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if actual := string(buf); actual != "foo" {
|
|
|
|
t.Fatalf("The first 3 letter should have been 'foo' but were %v", actual)
|
|
|
|
}
|
|
|
|
reader.Close()
|
|
|
|
// Read 3 more bytes "bar"
|
|
|
|
_, err = reader.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if actual := string(buf); actual != "bar" {
|
|
|
|
t.Fatalf("The first 3 letter should have been 'bar' but were %v", actual)
|
|
|
|
}
|
|
|
|
if !sr.closed {
|
|
|
|
t.Fatalf("The ReaderCloser should have been closed, it is not.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBufioWriterPoolGetWithNoReaderShouldCreateOne(t *testing.T) {
|
|
|
|
writer := BufioWriter32KPool.Get(nil)
|
|
|
|
if writer == nil {
|
|
|
|
t.Fatalf("BufioWriterPool should have create a bufio.Writer but did not.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBufioWriterPoolPutAndGet(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
bw := bufio.NewWriter(buf)
|
|
|
|
writer := BufioWriter32KPool.Get(bw)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, writer != nil)
|
2017-09-11 14:55:05 -04:00
|
|
|
|
2015-05-15 14:13:54 -04:00
|
|
|
written, err := writer.Write([]byte("foobar"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(6, written))
|
2017-09-11 14:55:05 -04:00
|
|
|
|
2015-05-15 14:13:54 -04:00
|
|
|
// Make sure we Flush all the way ?
|
|
|
|
writer.Flush()
|
|
|
|
bw.Flush()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Len(buf.Bytes(), 6))
|
2015-05-15 14:13:54 -04:00
|
|
|
// Reset the buffer
|
|
|
|
buf.Reset()
|
|
|
|
BufioWriter32KPool.Put(writer)
|
|
|
|
// Try to write something
|
2016-03-16 22:43:26 -04:00
|
|
|
if _, err = writer.Write([]byte("barfoo")); err != nil {
|
2015-05-15 14:13:54 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// If we now try to flush it, it should panic (the writer is nil)
|
|
|
|
// recover it
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Fatal("Trying to flush the writter should have 'paniced', did not.")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
writer.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
type simpleWriterCloser struct {
|
|
|
|
io.Writer
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *simpleWriterCloser) Close() error {
|
|
|
|
r.closed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewWriteCloserWrapperWithAWriteCloser(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
bw := bufio.NewWriter(buf)
|
|
|
|
sw := &simpleWriterCloser{
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
closed: false,
|
|
|
|
}
|
|
|
|
bw.Flush()
|
|
|
|
writer := BufioWriter32KPool.NewWriteCloserWrapper(bw, sw)
|
|
|
|
if writer == nil {
|
|
|
|
t.Fatalf("BufioReaderPool should not return a nil writer.")
|
|
|
|
}
|
|
|
|
written, err := writer.Write([]byte("foobar"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if written != 6 {
|
|
|
|
t.Fatalf("Should have written 6 bytes, but wrote %v bytes", written)
|
|
|
|
}
|
|
|
|
writer.Close()
|
|
|
|
if !sw.closed {
|
|
|
|
t.Fatalf("The ReaderCloser should have been closed, it is not.")
|
|
|
|
}
|
|
|
|
}
|
2017-06-10 08:03:49 -04:00
|
|
|
|
|
|
|
func TestBufferPoolPutAndGet(t *testing.T) {
|
|
|
|
buf := buffer32KPool.Get()
|
|
|
|
buffer32KPool.Put(buf)
|
|
|
|
}
|