2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-02-15 20:45:57 -05:00
|
|
|
|
|
|
|
package bpool
|
|
|
|
|
2024-08-28 21:40:52 -04:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
2018-02-15 20:45:57 -05:00
|
|
|
|
|
|
|
// Tests - bytePool functionality.
|
|
|
|
func TestBytePool(t *testing.T) {
|
2024-01-30 14:13:27 -05:00
|
|
|
size := uint64(4)
|
2023-12-21 11:59:38 -05:00
|
|
|
width := 1024
|
|
|
|
capWidth := 2048
|
2018-02-15 20:45:57 -05:00
|
|
|
|
2024-08-28 21:40:52 -04:00
|
|
|
bp := NewBytePoolCap(size, width, capWidth)
|
2018-02-15 20:45:57 -05:00
|
|
|
|
|
|
|
// Check the width
|
2024-08-28 21:40:52 -04:00
|
|
|
if bp.Width() != width {
|
|
|
|
t.Fatalf("bytepool width invalid: got %v want %v", bp.Width(), width)
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check with width cap
|
2024-08-28 21:40:52 -04:00
|
|
|
if bp.WidthCap() != capWidth {
|
|
|
|
t.Fatalf("bytepool capWidth invalid: got %v want %v", bp.WidthCap(), capWidth)
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that retrieved buffer are of the expected width
|
2024-08-28 21:40:52 -04:00
|
|
|
b := bp.Get()
|
2018-02-15 20:45:57 -05:00
|
|
|
if len(b) != width {
|
|
|
|
t.Fatalf("bytepool length invalid: got %v want %v", len(b), width)
|
|
|
|
}
|
|
|
|
if cap(b) != capWidth {
|
2023-12-21 11:59:38 -05:00
|
|
|
t.Fatalf("bytepool cap invalid: got %v want %v", cap(b), capWidth)
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
|
|
|
|
2024-08-28 21:40:52 -04:00
|
|
|
bp.Put(b)
|
2018-02-15 20:45:57 -05:00
|
|
|
|
|
|
|
// Fill the pool beyond the capped pool size.
|
2024-01-30 14:13:27 -05:00
|
|
|
for i := uint64(0); i < size*2; i++ {
|
2024-08-28 21:40:52 -04:00
|
|
|
bp.Put(make([]byte, bp.w, bp.wcap))
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
|
|
|
|
2024-08-28 21:40:52 -04:00
|
|
|
b = bp.Get()
|
2018-02-15 20:45:57 -05:00
|
|
|
if len(b) != width {
|
|
|
|
t.Fatalf("bytepool length invalid: got %v want %v", len(b), width)
|
|
|
|
}
|
|
|
|
if cap(b) != capWidth {
|
|
|
|
t.Fatalf("bytepool length invalid: got %v want %v", cap(b), capWidth)
|
|
|
|
}
|
|
|
|
|
2024-08-28 21:40:52 -04:00
|
|
|
bp.Put(b)
|
2018-02-15 20:45:57 -05:00
|
|
|
|
|
|
|
// Check the size of the pool.
|
2024-08-28 21:40:52 -04:00
|
|
|
if uint64(len(bp.c)) != size {
|
|
|
|
t.Fatalf("bytepool size invalid: got %v want %v", len(bp.c), size)
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
|
|
|
|
2024-08-28 21:40:52 -04:00
|
|
|
// lets drain the buf channel first before we validate invalid buffers.
|
|
|
|
for i := uint64(0); i < size; i++ {
|
|
|
|
bp.Get() // discard
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
|
|
|
|
2024-08-28 21:40:52 -04:00
|
|
|
// Try putting some invalid buffers into pool
|
|
|
|
bp.Put(make([]byte, bp.w, bp.wcap-1)) // wrong capacity is rejected (less)
|
|
|
|
bp.Put(make([]byte, bp.w, bp.wcap+1)) // wrong capacity is rejected (more)
|
|
|
|
bp.Put(make([]byte, width)) // wrong capacity is rejected (very less)
|
|
|
|
if len(bp.c) > 0 {
|
|
|
|
t.Fatal("bytepool should have rejected invalid packets")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try putting a short slice into pool
|
|
|
|
bp.Put(make([]byte, bp.w, bp.wcap)[:2])
|
|
|
|
if len(bp.c) != 1 {
|
|
|
|
t.Fatal("bytepool should have accepted short slice with sufficient capacity")
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
2024-08-28 21:40:52 -04:00
|
|
|
|
|
|
|
b = bp.Get()
|
2018-02-15 20:45:57 -05:00
|
|
|
if len(b) != width {
|
|
|
|
t.Fatalf("bytepool length invalid: got %v want %v", len(b), width)
|
|
|
|
}
|
2024-08-28 21:40:52 -04:00
|
|
|
|
|
|
|
// Close the channel.
|
|
|
|
close(bp.c)
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|