-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
234 lines (177 loc) · 4.38 KB
/
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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package pools_test
import (
"math/rand"
"sync"
"testing"
"time"
pools "github.com/DaanV2/go-resource-pool"
"github.com/stretchr/testify/require"
)
type TestItem struct {
ID int
Update bool
}
func Test_ResourcePool(t *testing.T) {
t.Run("Should be able to get a resource from the pool and update it", func(t *testing.T) {
original := &TestItem{
ID: 1,
Update: false,
}
pool := pools.NewResourcePool([]*TestItem{original}, 10)
item, returnFn := pool.Loan(0)
item.Update = true
returnFn(item)
require.Equal(t, original.Update, true)
})
t.Run("Should be able to get a resource from the pool using a string key", func(t *testing.T) {
original := &TestItem{
ID: 1,
Update: false,
}
pool := pools.NewResourcePool([]*TestItem{original}, 10)
item, returnFn := pool.LoanByString("dir/file.txt")
item.Update = true
returnFn(item)
require.Equal(t, original.Update, true)
})
t.Run("Should get an item, update it and return it to the pool, while another has to wait", func(t *testing.T) {
original := &TestItem{
ID: 1,
Update: false,
}
pool := pools.NewResourcePool([]*TestItem{original}, 10)
wait := sync.WaitGroup{}
wait.Add(2)
// Wait 1 second, then update
go func() {
defer wait.Done()
item, returnFn := pool.Loan(0)
item.Update = true
item.ID = 2
time.Sleep(5 * time.Second)
returnFn(item)
}()
go func() {
defer wait.Done()
time.Sleep(2 * time.Second)
item, returnFn := pool.Loan(0)
require.True(t, item.Update)
require.Equal(t, item.ID, 2)
item.ID = 3
returnFn(item)
}()
wait.Wait()
require.Equal(t, original.ID, 3)
})
t.Run("Larger pool size test", func(t *testing.T) {
items := make([]*TestItem, 0, 100)
for i := 0; i < 100; i++ {
items = append(items, &TestItem{
ID: i,
Update: false,
})
}
pool := pools.NewResourcePool(items, 100)
wait := sync.WaitGroup{}
len := pool.Len()
wait.Add(len)
for i := 0; i < len; i++ {
go func(index int) {
defer wait.Done()
item, returnFn := pool.Loan(uint64(index))
item.Update = true
returnFn(item)
}(i)
}
wait.Wait()
for i := 0; i < len; i++ {
require.True(t, items[i].Update)
}
})
}
func Test_ResourcePool_Deadlocks(t *testing.T) {
t.Run("Larger pool size test nr2.", func(t *testing.T) {
amount := 10
items := make([]*TestItem, 0, amount)
for i := 0; i < amount; i++ {
items = append(items, &TestItem{
ID: i,
Update: false,
})
}
pool := pools.NewResourcePool(items, amount)
wait := sync.WaitGroup{}
wait.Add(100)
seed := 123456789
// 1000 go routines, access a random item from the pool, sleep and update it
// This should not cause any deadlocks
for i := 0; i < 100; i++ {
go func(index int) {
defer wait.Done()
cs := seed * index
cs = cs ^ seed
rnd := rand.New(rand.NewSource(int64(cs)))
for j := 0; j < amount; j++ {
index := rnd.Uint64() % uint64(amount)
item, returnFn := pool.Loan(index)
item.Update = true
time.Sleep(100 * time.Millisecond)
returnFn(item)
}
}(i)
}
wait.Wait()
})
}
func Fuzz_ResourcePool_KeyInt(f *testing.F) {
f.Fuzz(func(t *testing.T, key uint64) {
original := &TestItem{
ID: 1,
Update: false,
}
pool := pools.NewResourcePool([]*TestItem{original}, 10)
item, returnFn := pool.Loan(key)
item.Update = true
returnFn(item)
require.Equal(t, original.Update, true)
})
}
func Fuzz_ResourcePool_KeyString(f *testing.F) {
f.Fuzz(func(t *testing.T, key uint64) {
original := &TestItem{
ID: 1,
Update: false,
}
pool := pools.NewResourcePool([]*TestItem{original}, 10)
item, returnFn := pool.Loan(key)
item.Update = true
returnFn(item)
require.Equal(t, original.Update, true)
})
}
func Fuzz_ResourcePool_KeyBytes(f *testing.F) {
f.Fuzz(func(t *testing.T, key []byte) {
original := &TestItem{
ID: 1,
Update: false,
}
pool := pools.NewResourcePool([]*TestItem{original}, 10)
item, returnFn := pool.LoanByBytes(key)
item.Update = true
returnFn(item)
require.Equal(t, original.Update, true)
})
}
func Fuzz_ResourcePool_KeyUint64(f *testing.F) {
f.Fuzz(func(t *testing.T, key uint64) {
original := &TestItem{
ID: 1,
Update: false,
}
pool := pools.NewResourcePool([]*TestItem{original}, 10)
item, returnFn := pool.LoanByUint64(key)
item.Update = true
returnFn(item)
require.Equal(t, original.Update, true)
})
}