-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathrecycle_test.go
217 lines (171 loc) · 3.99 KB
/
recycle_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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package pool_test
import (
"bytes"
"testing"
"time"
"github.com/lightningnetwork/lnd/buffer"
"github.com/lightningnetwork/lnd/pool"
)
type mockRecycler bool
func (m *mockRecycler) Recycle() {
*m = false
}
// TestRecyclers verifies that known recyclable types properly return to their
// zero-value after invoking Recycle.
func TestRecyclers(t *testing.T) {
tests := []struct {
name string
newItem func() interface{}
}{
{
"mock recycler",
func() interface{} { return new(mockRecycler) },
},
{
"write_buffer",
func() interface{} { return new(buffer.Write) },
},
{
"read_buffer",
func() interface{} { return new(buffer.Read) },
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Initialize the Recycler to test.
r := test.newItem().(pool.Recycler)
// Dirty the item.
dirtyGeneric(t, r)
// Invoke Recycle to clear the item.
r.Recycle()
// Assert the item is now clean.
isCleanGeneric(t, r)
})
}
}
type recyclePoolTest struct {
name string
newPool func() interface{}
}
// TestGenericRecyclePoolTests generically tests that pools derived from the
// base Recycle pool properly are properly configured.
func TestConcreteRecyclePoolTests(t *testing.T) {
const (
gcInterval = time.Second
expiryInterval = 250 * time.Millisecond
)
tests := []recyclePoolTest{
{
name: "write buffer pool",
newPool: func() interface{} {
return pool.NewWriteBuffer(
gcInterval, expiryInterval,
)
},
},
{
name: "read buffer pool",
newPool: func() interface{} {
return pool.NewReadBuffer(
gcInterval, expiryInterval,
)
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testRecyclePool(t, test)
})
}
}
func testRecyclePool(t *testing.T, test recyclePoolTest) {
p := test.newPool()
// Take an item from the pool.
r1 := takeGeneric(t, p)
// Dirty the item.
dirtyGeneric(t, r1)
// Return the item to the pool.
returnGeneric(t, p, r1)
// Take items from the pool until we find the original. We expect at
// most two, in the event that a fresh item is populated after the
// first is taken.
for i := 0; i < 2; i++ {
// Wait a small duration to ensure the tests are reliable, and
// don't to active the non-blocking case unintentionally.
<-time.After(time.Millisecond)
r2 := takeGeneric(t, p)
// Take an item, skipping those whose pointer does not match the
// one we dirtied.
if r1 != r2 {
continue
}
// Finally, verify that the item has been properly cleaned.
isCleanGeneric(t, r2)
return
}
t.Fatalf("original item not found")
}
func takeGeneric(t *testing.T, p interface{}) pool.Recycler {
t.Helper()
switch pp := p.(type) {
case *pool.WriteBuffer:
return pp.Take()
case *pool.ReadBuffer:
return pp.Take()
default:
t.Fatalf("unknown pool type: %T", p)
}
return nil
}
func returnGeneric(t *testing.T, p, item interface{}) {
t.Helper()
switch pp := p.(type) {
case *pool.WriteBuffer:
pp.Return(item.(*buffer.Write))
case *pool.ReadBuffer:
pp.Return(item.(*buffer.Read))
default:
t.Fatalf("unknown pool type: %T", p)
}
}
func dirtyGeneric(t *testing.T, i interface{}) {
t.Helper()
switch item := i.(type) {
case *mockRecycler:
*item = true
case *buffer.Write:
dirtySlice(item[:])
case *buffer.Read:
dirtySlice(item[:])
default:
t.Fatalf("unknown item type: %T", i)
}
}
func dirtySlice(slice []byte) {
for i := range slice {
slice[i] = 0xff
}
}
func isCleanGeneric(t *testing.T, i interface{}) {
t.Helper()
switch item := i.(type) {
case *mockRecycler:
if isDirty := *item; isDirty {
t.Fatalf("mock recycler still diry")
}
case *buffer.Write:
isCleanSlice(t, item[:])
case *buffer.Read:
isCleanSlice(t, item[:])
default:
t.Fatalf("unknown item type: %T", i)
}
}
func isCleanSlice(t *testing.T, slice []byte) {
t.Helper()
expSlice := make([]byte, len(slice))
if !bytes.Equal(expSlice, slice) {
t.Fatalf("slice not recycled, want: %v, got: %v",
expSlice, slice)
}
}