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