-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector_test.go
279 lines (220 loc) · 5.02 KB
/
collector_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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package sync
import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/anchore/go-sync/internal/stats"
)
func Test_Collectors(t *testing.T) {
for i := 0; i < 100; i++ {
Test_Collector(t)
}
}
func Test_Collector(t *testing.T) {
const count = 20
const maxConcurrency = 5
c := NewCollector[int](NewExecutor(maxConcurrency))
concurrency := stats.Tracked[int]{}
for i := 0; i < count; i++ {
i := i
c.Provide(func() int {
defer concurrency.Incr()()
time.Sleep(1 * time.Millisecond)
return i
})
}
values := c.Collect()
require.Len(t, values, count)
for i := 0; i < count; i++ {
require.Contains(t, values, i)
}
require.LessOrEqual(t, concurrency.Max(), maxConcurrency)
}
func Test_lotsaLotsaCollector(t *testing.T) {
concurrency := 100
executors := 1000
concurrent := stats.Tracked[int64]{}
c := NewCollector[int](NewExecutor(concurrency))
for i := 0; i < executors; i++ {
i := i
c.Provide(func() int {
defer concurrent.Incr()()
Test_collector(t)
return i
})
}
got := c.Collect()
require.Len(t, got, executors)
t.Logf("max concurrent: %d", concurrent.Max())
}
func Test_lotsaCollector(t *testing.T) {
for i := 0; i < 1000; i++ {
Test_collector(t)
}
}
func Test_collector(t *testing.T) {
concurrency := (25)
count := 1000
wgs := make([]sync.WaitGroup, count)
concurrent := stats.Tracked[int64]{}
total := atomic.Uint64{}
makeFunc := func(idx int) func() int {
wgs[idx].Add(1)
return func() int {
defer total.Add(1)
concurrent.Incr()
wgs[idx].Wait()
concurrent.Decr()
return idx
}
}
var expected []int
c := NewCollector[int](NewExecutor(concurrency))
for i := 0; i < count; i++ {
expected = append(expected, i)
c.Provide(makeFunc(i))
}
go func() {
for i := 0; i < count; i++ {
wgs[i].Done()
}
}()
got := c.Collect()
require.ElementsMatch(t, expected, got)
require.LessOrEqual(t, concurrent.Max(), int64(concurrency))
require.Equal(t, total.Load(), uint64(count))
}
func Test_collectorSmall(t *testing.T) {
concurrency := (2)
count := 4
wgs := make([]sync.WaitGroup, count)
waiting := sync.WaitGroup{}
waiting.Add(int(concurrency))
concurrent := stats.Tracked[int]{}
total := atomic.Uint64{}
makeFunc := func(idx int) func() int {
wgs[idx].Add(1)
return func() int {
if idx < int(concurrency) {
waiting.Done()
}
defer total.Add(1)
concurrent.Incr()
wgs[idx].Wait()
concurrent.Decr()
return idx
}
}
var expected []int
c := NewCollector[int](NewExecutor(concurrency))
for i := 0; i < count; i++ {
expected = append(expected, i)
c.Provide(makeFunc(i))
}
time.Sleep(10 * time.Millisecond)
waiting.Wait()
require.Equal(t, 2, concurrent.Val())
go func() {
for i := 0; i < count; i++ {
wgs[i].Done()
}
}()
got := c.Collect()
require.ElementsMatch(t, expected, got)
require.LessOrEqual(t, concurrent.Max(), concurrency)
require.Equal(t, total.Load(), uint64(count))
}
func Test_collectorLimiting(t *testing.T) {
c := NewCollector[string](NewExecutor(2))
wg1 := &sync.WaitGroup{}
wg1.Add(1)
wg2 := &sync.WaitGroup{}
wg2.Add(1)
wg3 := &sync.WaitGroup{}
wg3.Add(1)
order := List[string]{}
executed := ""
wgReady := &sync.WaitGroup{}
wgReady.Add(2)
c.Provide(func() string {
order.Add("pre wg1")
wgReady.Done()
wg1.Wait()
order.Add("post wg1")
executed += "1_"
return "1"
})
c.Provide(func() string {
order.Add("pre wg2")
wgReady.Done()
wg2.Wait()
order.Add("post wg2")
executed += "2_"
wg3.Done()
return "2"
})
wgReady.Wait()
c.Provide(func() string {
order.Add("pre wg3")
wg3.Wait()
order.Add("post wg3")
executed += "3_"
wg1.Done()
return "3"
})
wg2.Done()
got := c.Collect()
require.Equal(t, "2_3_1_", executed)
require.ElementsMatch(t, []string{"2", "3", "1"}, got)
require.True(t,
order.indexOf("post wg2") < order.indexOf("post wg3") &&
order.indexOf("post wg3") < order.indexOf("post wg1"),
)
}
func Test_collectorDecoupledFromExecutor(t *testing.T) {
// this test shows that the executor and collector are loosely coupled -- a collector
// does not depend on the lifetime of the executor given to it.
exec := NewExecutor(2)
c := NewCollector[string](exec)
wg1 := &sync.WaitGroup{}
wg1.Add(1)
wg2 := &sync.WaitGroup{}
wg2.Add(1)
wg3 := &sync.WaitGroup{}
wg3.Add(1)
executed := ""
wgReady := &sync.WaitGroup{}
wgReady.Add(2)
c.Provide(func() string {
wgReady.Done()
executed += "1_"
wg1.Done()
return "1"
})
exec.Execute(func() {
wg3.Wait()
executed += "1e_"
})
c.Provide(func() string {
wgReady.Done()
wg1.Wait()
executed += "2_"
wg2.Done()
return "2"
})
wgReady.Wait()
c.Provide(func() string {
executed += "3_"
return "3"
})
// the key to this test is here: since "1e_" is not part of the collector, it will not be waited on. If we
// did accidentally wait on it, the test would hang here.
got := c.Collect()
assert.Equal(t, []string{"1", "2", "3"}, got)
wg3.Done()
exec.Wait()
require.Equal(t, "1_2_3_1e_", executed)
}