github.com/bhojpur/cache@v0.0.4/pkg/pools/pools_test.go (about)

     1  package pools
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"bufio"
    25  	"bytes"
    26  	"io"
    27  	"strings"
    28  	"testing"
    29  
    30  	"gotest.tools/v3/assert"
    31  	is "gotest.tools/v3/assert/cmp"
    32  )
    33  
    34  func TestBufioReaderPoolGetWithNoReaderShouldCreateOne(t *testing.T) {
    35  	reader := BufioReader32KPool.Get(nil)
    36  	if reader == nil {
    37  		t.Fatalf("BufioReaderPool should have create a bufio.Reader but did not.")
    38  	}
    39  }
    40  
    41  func TestBufioReaderPoolPutAndGet(t *testing.T) {
    42  	sr := bufio.NewReader(strings.NewReader("foobar"))
    43  	reader := BufioReader32KPool.Get(sr)
    44  	if reader == nil {
    45  		t.Fatalf("BufioReaderPool should not return a nil reader.")
    46  	}
    47  	// verify the first 3 byte
    48  	buf1 := make([]byte, 3)
    49  	_, err := reader.Read(buf1)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	if actual := string(buf1); actual != "foo" {
    54  		t.Fatalf("The first letter should have been 'foo' but was %v", actual)
    55  	}
    56  	BufioReader32KPool.Put(reader)
    57  	// Try to read the next 3 bytes
    58  	_, err = sr.Read(make([]byte, 3))
    59  	if err == nil || err != io.EOF {
    60  		t.Fatalf("The buffer should have been empty, issue an EOF error.")
    61  	}
    62  }
    63  
    64  type simpleReaderCloser struct {
    65  	io.Reader
    66  	closed bool
    67  }
    68  
    69  func (r *simpleReaderCloser) Close() error {
    70  	r.closed = true
    71  	return nil
    72  }
    73  
    74  func TestNewReadCloserWrapperWithAReadCloser(t *testing.T) {
    75  	br := bufio.NewReader(strings.NewReader(""))
    76  	sr := &simpleReaderCloser{
    77  		Reader: strings.NewReader("foobar"),
    78  		closed: false,
    79  	}
    80  	reader := BufioReader32KPool.NewReadCloserWrapper(br, sr)
    81  	if reader == nil {
    82  		t.Fatalf("NewReadCloserWrapper should not return a nil reader.")
    83  	}
    84  	// Verify the content of reader
    85  	buf := make([]byte, 3)
    86  	_, err := reader.Read(buf)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	if actual := string(buf); actual != "foo" {
    91  		t.Fatalf("The first 3 letter should have been 'foo' but were %v", actual)
    92  	}
    93  	reader.Close()
    94  	// Read 3 more bytes "bar"
    95  	_, err = reader.Read(buf)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	if actual := string(buf); actual != "bar" {
   100  		t.Fatalf("The first 3 letter should have been 'bar' but were %v", actual)
   101  	}
   102  	if !sr.closed {
   103  		t.Fatalf("The ReaderCloser should have been closed, it is not.")
   104  	}
   105  }
   106  
   107  func TestBufioWriterPoolGetWithNoReaderShouldCreateOne(t *testing.T) {
   108  	writer := BufioWriter32KPool.Get(nil)
   109  	if writer == nil {
   110  		t.Fatalf("BufioWriterPool should have create a bufio.Writer but did not.")
   111  	}
   112  }
   113  
   114  func TestBufioWriterPoolPutAndGet(t *testing.T) {
   115  	buf := new(bytes.Buffer)
   116  	bw := bufio.NewWriter(buf)
   117  	writer := BufioWriter32KPool.Get(bw)
   118  	assert.Assert(t, writer != nil)
   119  
   120  	written, err := writer.Write([]byte("foobar"))
   121  	assert.NilError(t, err)
   122  	assert.Check(t, is.Equal(6, written))
   123  
   124  	// Make sure we Flush all the way ?
   125  	writer.Flush()
   126  	bw.Flush()
   127  	assert.Check(t, is.Len(buf.Bytes(), 6))
   128  	// Reset the buffer
   129  	buf.Reset()
   130  	BufioWriter32KPool.Put(writer)
   131  	// Try to write something
   132  	if _, err = writer.Write([]byte("barfoo")); err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	// If we now try to flush it, it should panic (the writer is nil)
   136  	// recover it
   137  	defer func() {
   138  		if r := recover(); r == nil {
   139  			t.Fatal("Trying to flush the writter should have 'paniced', did not.")
   140  		}
   141  	}()
   142  	writer.Flush()
   143  }
   144  
   145  type simpleWriterCloser struct {
   146  	io.Writer
   147  	closed bool
   148  }
   149  
   150  func (r *simpleWriterCloser) Close() error {
   151  	r.closed = true
   152  	return nil
   153  }
   154  
   155  func TestNewWriteCloserWrapperWithAWriteCloser(t *testing.T) {
   156  	buf := new(bytes.Buffer)
   157  	bw := bufio.NewWriter(buf)
   158  	sw := &simpleWriterCloser{
   159  		Writer: new(bytes.Buffer),
   160  		closed: false,
   161  	}
   162  	bw.Flush()
   163  	writer := BufioWriter32KPool.NewWriteCloserWrapper(bw, sw)
   164  	if writer == nil {
   165  		t.Fatalf("BufioReaderPool should not return a nil writer.")
   166  	}
   167  	written, err := writer.Write([]byte("foobar"))
   168  	if err != nil {
   169  		t.Fatal(err)
   170  	}
   171  	if written != 6 {
   172  		t.Fatalf("Should have written 6 bytes, but wrote %v bytes", written)
   173  	}
   174  	writer.Close()
   175  	if !sw.closed {
   176  		t.Fatalf("The ReaderCloser should have been closed, it is not.")
   177  	}
   178  }
   179  
   180  func TestBufferPoolPutAndGet(t *testing.T) {
   181  	buf := buffer32KPool.Get()
   182  	buffer32KPool.Put(buf)
   183  }