-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbounded_test.go
More file actions
155 lines (141 loc) · 4.23 KB
/
Copy pathbounded_test.go
File metadata and controls
155 lines (141 loc) · 4.23 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package bytebufferpool_test
import (
"errors"
"sync"
"sync/atomic"
"testing"
bytebufferpool "github.com/ymj4023/bytebufferpool"
)
func TestBoundedPoolEnforcesRetainedCapacity(t *testing.T) {
pool, err := bytebufferpool.New(bytebufferpool.Config{
Mode: bytebufferpool.Bounded,
Classes: []int{64},
MaxPooledCapacity: 64,
MaxRetainedCapacity: 64,
StatsEnabled: false,
})
if err != nil {
t.Fatalf("New Bounded Pool: %v", err)
}
first := pool.Acquire(64)
second := pool.Acquire(64)
if got := first.Release(); got != bytebufferpool.Retained {
t.Fatalf("first Release() = %v; want Retained", got)
}
if got := second.Release(); got != bytebufferpool.DroppedFull {
t.Fatalf("second Release() = %v; want DroppedFull", got)
}
stats := pool.Stats()
if !stats.RetainedAvailable {
t.Fatal("Bounded Stats reported retained inventory unavailable")
}
if stats.RetainedStorageCount != 1 || stats.RetainedCapacity != 64 {
t.Fatalf("Bounded retained inventory = %d storage objects/%d bytes; want 1/64", stats.RetainedStorageCount, stats.RetainedCapacity)
}
reused := pool.Acquire(64)
stats = pool.Stats()
if stats.RetainedStorageCount != 0 || stats.RetainedCapacity != 0 {
t.Fatalf("inventory after Acquire = %d storage objects/%d bytes; want 0/0", stats.RetainedStorageCount, stats.RetainedCapacity)
}
if got := reused.Release(); got != bytebufferpool.Retained {
t.Fatalf("reused Release() = %v; want Retained", got)
}
}
func TestBoundedPoolRejectsMissingBudget(t *testing.T) {
_, err := bytebufferpool.New(bytebufferpool.Config{
Mode: bytebufferpool.Bounded,
Classes: []int{64},
MaxPooledCapacity: 64,
})
if !errors.Is(err, bytebufferpool.ErrInvalidConfig) {
t.Fatalf("New Bounded Pool without budget error = %v; want ErrInvalidConfig", err)
}
}
func TestBoundedPoolNeverExceedsBudgetUnderConcurrentRelease(t *testing.T) {
const (
capacity = 64
limit = 8 * capacity
leases = 128
)
pool, err := bytebufferpool.New(bytebufferpool.Config{
Mode: bytebufferpool.Bounded,
Classes: []int{capacity},
MaxPooledCapacity: capacity,
MaxRetainedCapacity: limit,
})
if err != nil {
t.Fatalf("New Bounded Pool: %v", err)
}
acquired := make([]bytebufferpool.Lease, leases)
for i := range acquired {
acquired[i] = pool.Acquire(capacity)
}
var wait sync.WaitGroup
for i := range acquired {
wait.Add(1)
go func(lease *bytebufferpool.Lease) {
defer wait.Done()
lease.Release()
}(&acquired[i])
}
wait.Wait()
stats := pool.Stats()
if stats.RetainedCapacity > limit {
t.Fatalf("RetainedCapacity = %d; exceeds budget %d", stats.RetainedCapacity, limit)
}
if stats.RetainedStorageCount*capacity != stats.RetainedCapacity {
t.Fatalf("retained inventory inconsistent: %d storage objects/%d bytes", stats.RetainedStorageCount, stats.RetainedCapacity)
}
}
func TestBoundedPoolReportsExactInventoryDuringConcurrentReuse(t *testing.T) {
const (
capacity = 64
workers = 8
iterations = 10_000
)
pool, err := bytebufferpool.New(bytebufferpool.Config{
Mode: bytebufferpool.Bounded,
Classes: []int{capacity},
MaxPooledCapacity: capacity,
MaxRetainedCapacity: workers * capacity,
})
if err != nil {
t.Fatalf("New Bounded Pool: %v", err)
}
var unexpectedReleases atomic.Int64
var wait sync.WaitGroup
for range workers {
wait.Add(1)
go func() {
defer wait.Done()
for range iterations {
lease := pool.Acquire(capacity)
if lease.Release() != bytebufferpool.Retained {
unexpectedReleases.Add(1)
}
}
}()
}
done := make(chan struct{})
go func() {
wait.Wait()
close(done)
}()
for {
select {
case <-done:
if got := unexpectedReleases.Load(); got != 0 {
t.Fatalf("unexpected release statuses = %d; want 0", got)
}
return
default:
stats := pool.Stats()
if stats.RetainedStorageCount < 0 || stats.RetainedStorageCount > workers {
t.Fatalf("RetainedStorageCount = %d; want within [0,%d]", stats.RetainedStorageCount, workers)
}
if stats.RetainedCapacity != stats.RetainedStorageCount*capacity {
t.Fatalf("inventory snapshot = %d storage objects/%d bytes; want exact capacity", stats.RetainedStorageCount, stats.RetainedCapacity)
}
}
}
}