Implement byte buffer pool
This commit is contained in:
143
pdu/bufpool_test.go
Normal file
143
pdu/bufpool_test.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package pdu
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRetrieveBufferOfRequestedSize(t *testing.T) {
|
||||
bpm := &BufferPoolManager{
|
||||
pools: make(map[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
|
||||
size := 1024
|
||||
buffer := bpm.GetBuffer(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[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
|
||||
size := 0
|
||||
buffer := bpm.GetBuffer(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[int]*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(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[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
|
||||
size := 1024
|
||||
buffer := bpm.GetBuffer(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[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
|
||||
size := 512
|
||||
buffer := bpm.GetBuffer(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[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
|
||||
size := 1024
|
||||
buffer := bpm.GetBuffer(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[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
|
||||
sizes := []int{512, 1024, 2048}
|
||||
for _, size := range sizes {
|
||||
buffer := bpm.GetBuffer(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))
|
||||
}
|
||||
}
|
||||
}
|
8
pdu/global.go
Normal file
8
pdu/global.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package pdu
|
||||
|
||||
import "sync"
|
||||
|
||||
var byteBufferPool = &BufferPoolManager{
|
||||
pools: make(map[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
Reference in New Issue
Block a user