-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_ring_queue_options_test.go
More file actions
126 lines (99 loc) · 3.58 KB
/
Copy pathexample_ring_queue_options_test.go
File metadata and controls
126 lines (99 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package bytepool
import (
"fmt"
"testing"
)
// ExampleWithRingQueueType demonstrates how to configure different ring queue types
func ExampleWithRingQueueType() {
// Create BytePool with default lock-free ring queue
defaultPool := NewPools([]int{128, 256, 512})
// Create BytePool with mutex-based ring queue for strong consistency
mutexPool := NewPools([]int{128, 256, 512}, WithRingQueueType(MutexRingQueue))
// Use both pools
buf1 := defaultPool.Get(100)
buf2 := mutexPool.Get(100)
defaultPool.Put(buf1)
mutexPool.Put(buf2)
// Get statistics
stats1 := defaultPool.GetPoolStats()
stats2 := mutexPool.GetPoolStats()
fmt.Printf("Default pool recent lengths: %v\n", len(stats1["recent_lengths"].([]int)))
fmt.Printf("Mutex pool recent lengths: %v\n", len(stats2["recent_lengths"].([]int)))
// Output:
// Default pool recent lengths: 1
// Mutex pool recent lengths: 1
}
// ExampleWithRingQueue demonstrates how to use a custom ring queue
func ExampleWithRingQueue() {
// Create a custom locked ring queue with specific capacity
customQueue := NewLockedRingQueue[int](64)
// Create BytePool with custom ring queue
pool := NewPools([]int{128, 256, 512}, WithRingQueue(customQueue))
// Use the pool
buf := pool.Get(150)
pool.Put(buf)
// Access the custom queue directly
fmt.Printf("Custom queue capacity: %d\n", customQueue.Cap())
fmt.Printf("Custom queue length: %d\n", customQueue.Len())
// Output:
// Custom queue capacity: 64
// Custom queue length: 1
}
// Example_ringQueueComparison demonstrates the differences between lock-free and mutex-based queues
func Example_ringQueueComparison() {
// Lock-free ring queue
lockFreeQueue := NewRingQueue[string](3)
lockFreeQueue.Push("a")
lockFreeQueue.Push("b")
lockFreeQueue.Push("c")
fmt.Printf("Lock-free queue data: %v\n", lockFreeQueue.Bytes())
fmt.Printf("Lock-free queue length: %d\n", lockFreeQueue.Len())
// Mutex-based ring queue with additional operations
mutexQueue := NewLockedRingQueue[string](3)
mutexQueue.Push("x")
mutexQueue.Push("y")
mutexQueue.Push("z")
fmt.Printf("Mutex queue data: %v\n", mutexQueue.Bytes())
// Additional operations only available in mutex-based queue
if item, ok := mutexQueue.Peek(); ok {
fmt.Printf("Oldest item (peek): %s\n", item)
}
if item, ok := mutexQueue.Pop(); ok {
fmt.Printf("Popped item: %s\n", item)
fmt.Printf("After pop, length: %d\n", mutexQueue.Len())
}
// Output:
// Lock-free queue data: [a b c]
// Lock-free queue length: 3
// Mutex queue data: [x y z]
// Oldest item (peek): x
// Popped item: x
// After pop, length: 2
}
func TestRingQueueOptions(t *testing.T) {
// Test that different configurations work correctly
// Test lock-free configuration
lockFreePool := NewPools([]int{128, 256}, WithRingQueueType(LockFreeRingQueue))
buf := lockFreePool.Get(100)
lockFreePool.Put(buf)
stats := lockFreePool.GetPoolStats()
if len(stats["recent_lengths"].([]int)) != 1 {
t.Errorf("Expected 1 recent length, got %d", len(stats["recent_lengths"].([]int)))
}
// Test mutex configuration
mutexPool := NewPools([]int{128, 256}, WithRingQueueType(MutexRingQueue))
buf = mutexPool.Get(200)
mutexPool.Put(buf)
stats = mutexPool.GetPoolStats()
if len(stats["recent_lengths"].([]int)) != 1 {
t.Errorf("Expected 1 recent length, got %d", len(stats["recent_lengths"].([]int)))
}
// Test custom ring queue
customQueue := NewLockedRingQueue[int](10)
customPool := NewPools([]int{128, 256}, WithRingQueue(customQueue))
buf = customPool.Get(150)
customPool.Put(buf)
if customQueue.Len() != 1 {
t.Errorf("Expected custom queue length 1, got %d", customQueue.Len())
}
}