forked from uber/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checked_frame_pool_test.go
83 lines (75 loc) · 2.05 KB
/
checked_frame_pool_test.go
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
package tchannel
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/uber/tchannel-go/testutils/goroutines"
)
func TestCheckedFramePoolForTest(t *testing.T) {
tests := []struct {
msg string
operations func(pool *CheckedFramePoolForTest)
wantHasIssues bool
wantBadAllocations int
wantBadReleases int
}{
{
msg: "no bad releases or leaks",
operations: func(pool *CheckedFramePoolForTest) {
for i := 0; i < 10; i++ {
pool.Release(pool.Get())
}
},
},
{
msg: "frames are leaked",
operations: func(pool *CheckedFramePoolForTest) {
for i := 0; i < 10; i++ {
pool.Release(pool.Get())
}
for i := 0; i < 10; i++ {
_ = pool.Get()
}
},
wantHasIssues: true,
wantBadAllocations: 10,
},
{
msg: "frames are double released",
operations: func(pool *CheckedFramePoolForTest) {
for i := 0; i < 10; i++ {
pool.Release(pool.Get())
}
f := pool.Get()
pool.Release(f)
pool.Release(f)
},
wantHasIssues: true,
wantBadReleases: 1,
},
}
for _, tt := range tests {
t.Run(tt.msg, func(t *testing.T) {
pool := NewCheckedFramePoolForTest()
tt.operations(pool)
results := pool.CheckEmpty()
assert.Equal(t, tt.wantHasIssues, results.HasIssues(), "Unexpected HasIssues() state")
assert.Equal(t, tt.wantBadAllocations, len(results.Unreleased), "Unexpected allocs")
assert.Equal(t, tt.wantBadReleases, len(results.BadReleases), "Unexpected bad releases")
})
}
}
func CheckFramePoolIsEmpty(t testing.TB, pool *CheckedFramePoolForTest) {
t.Helper()
stacks := goroutines.GetAll()
if result := pool.CheckEmpty(); result.HasIssues() {
if len(result.Unreleased) > 0 {
t.Errorf("Frame pool has %v unreleased frames, errors:\n%v\nStacks:%v",
len(result.Unreleased), strings.Join(result.Unreleased, "\n"), stacks)
}
if len(result.BadReleases) > 0 {
t.Errorf("Frame pool has %v bad releases, errors:\n%v\nStacks:%v",
len(result.BadReleases), strings.Join(result.BadReleases, "\n"), stacks)
}
}
}