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)) } } }