Files
smpp-tester/pdu/bufpool_test.go
2024-07-23 09:31:26 +02:00

144 lines
2.8 KiB
Go

package pdu
import (
"sync"
"testing"
)
func TestRetrieveBufferOfRequestedSize(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 1024
buffer := bpm.GetBuffer(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
func TestRequestBufferSizeZero(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 0
buffer := bpm.GetBuffer(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
func TestConcurrentAccessToBufferPool(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 1024
var wg sync.WaitGroup
numRoutines := 100
for i := 0; i < numRoutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
buffer := bpm.GetBuffer(uint(size))
if buffer == nil {
t.Errorf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}()
}
wg.Wait()
}
func TestGetBufferLockUnlock(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 1024
buffer := bpm.GetBuffer(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
func TestVerifyPoolCreationForNewSizes(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 512
buffer := bpm.GetBuffer(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
func TestBufferPoolManagerGetBuffer(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 1024
buffer := bpm.GetBuffer(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
func TestGetBufferWithMultipleSizes(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
sizes := []int{512, 1024, 2048}
for _, size := range sizes {
buffer := bpm.GetBuffer(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer for size %d, got nil", size)
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
}