github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/bpool/bpool_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package bpool
    19  
    20  import "testing"
    21  
    22  // Tests - bytePool functionality.
    23  func TestBytePool(t *testing.T) {
    24  	size := uint64(4)
    25  	width := 1024
    26  	capWidth := 2048
    27  
    28  	bufPool := NewBytePoolCap(size, width, capWidth)
    29  
    30  	// Check the width
    31  	if bufPool.Width() != width {
    32  		t.Fatalf("bytepool width invalid: got %v want %v", bufPool.Width(), width)
    33  	}
    34  
    35  	// Check with width cap
    36  	if bufPool.WidthCap() != capWidth {
    37  		t.Fatalf("bytepool capWidth invalid: got %v want %v", bufPool.WidthCap(), capWidth)
    38  	}
    39  
    40  	// Check that retrieved buffer are of the expected width
    41  	b := bufPool.Get()
    42  	if len(b) != width {
    43  		t.Fatalf("bytepool length invalid: got %v want %v", len(b), width)
    44  	}
    45  	if cap(b) != capWidth {
    46  		t.Fatalf("bytepool cap invalid: got %v want %v", cap(b), capWidth)
    47  	}
    48  
    49  	bufPool.Put(b)
    50  
    51  	// Fill the pool beyond the capped pool size.
    52  	for i := uint64(0); i < size*2; i++ {
    53  		bufPool.Put(make([]byte, bufPool.w))
    54  	}
    55  
    56  	b = bufPool.Get()
    57  	if len(b) != width {
    58  		t.Fatalf("bytepool length invalid: got %v want %v", len(b), width)
    59  	}
    60  	if cap(b) != capWidth {
    61  		t.Fatalf("bytepool length invalid: got %v want %v", cap(b), capWidth)
    62  	}
    63  
    64  	bufPool.Put(b)
    65  
    66  	// Close the channel so we can iterate over it.
    67  	close(bufPool.c)
    68  
    69  	// Check the size of the pool.
    70  	if uint64(len(bufPool.c)) != size {
    71  		t.Fatalf("bytepool size invalid: got %v want %v", len(bufPool.c), size)
    72  	}
    73  
    74  	bufPoolNoCap := NewBytePoolCap(size, width, 0)
    75  	// Check the width
    76  	if bufPoolNoCap.Width() != width {
    77  		t.Fatalf("bytepool width invalid: got %v want %v", bufPool.Width(), width)
    78  	}
    79  
    80  	// Check with width cap
    81  	if bufPoolNoCap.WidthCap() != 0 {
    82  		t.Fatalf("bytepool capWidth invalid: got %v want %v", bufPool.WidthCap(), 0)
    83  	}
    84  	b = bufPoolNoCap.Get()
    85  	if len(b) != width {
    86  		t.Fatalf("bytepool length invalid: got %v want %v", len(b), width)
    87  	}
    88  	if cap(b) != width {
    89  		t.Fatalf("bytepool length invalid: got %v want %v", cap(b), width)
    90  	}
    91  }