-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
bounded_queue_test.go
329 lines (267 loc) · 8.49 KB
/
bounded_queue_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package queue
import (
"fmt"
"reflect"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber/jaeger-lib/metrics"
"github.com/uber/jaeger-lib/metrics/metricstest"
)
// In this test we run a queue with capacity 1 and a single consumer.
// We want to test the overflow behavior, so we block the consumer
// by holding a startLock before submitting items to the queue.
func TestBoundedQueue(t *testing.T) {
mFact := metricstest.NewFactory(0)
counter := mFact.Counter(metrics.Options{Name: "dropped", Tags: nil})
gauge := mFact.Gauge(metrics.Options{Name: "size", Tags: nil})
q := NewBoundedQueue(1, func(item interface{}) {
counter.Inc(1)
})
assert.Equal(t, 1, q.Capacity())
var startLock sync.Mutex
startLock.Lock() // block consumers
consumerState := newConsumerState(t)
q.StartConsumers(1, func(item interface{}) {
consumerState.record(item.(string))
// block further processing until startLock is released
startLock.Lock()
//lint:ignore SA2001 empty section is ok
startLock.Unlock()
})
assert.True(t, q.Produce("a"))
// at this point "a" may or may not have been received by the consumer go-routine
// so let's make sure it has been
consumerState.waitToConsumeOnce()
// at this point the item must have been read off the queue, but the consumer is blocked
assert.Equal(t, 0, q.Size())
consumerState.assertConsumed(map[string]bool{
"a": true,
})
// produce two more items. The first one should be accepted, but not consumed.
assert.True(t, q.Produce("b"))
assert.Equal(t, 1, q.Size())
// the second should be rejected since the queue is full
assert.False(t, q.Produce("c"))
assert.Equal(t, 1, q.Size())
q.StartLengthReporting(time.Millisecond, gauge)
for i := 0; i < 1000; i++ {
_, g := mFact.Snapshot()
if g["size"] == 0 {
time.Sleep(time.Millisecond)
}
}
c, g := mFact.Snapshot()
assert.EqualValues(t, 1, c["dropped"])
assert.EqualValues(t, 1, g["size"])
startLock.Unlock() // unblock consumer
consumerState.assertConsumed(map[string]bool{
"a": true,
"b": true,
})
// now that consumers are unblocked, we can add more items
expected := map[string]bool{
"a": true,
"b": true,
}
for _, item := range []string{"d", "e", "f"} {
assert.True(t, q.Produce(item))
expected[item] = true
consumerState.assertConsumed(expected)
}
q.Stop()
assert.False(t, q.Produce("x"), "cannot push to closed queue")
}
type consumerState struct {
sync.Mutex
t *testing.T
consumed map[string]bool
consumedOnce int32
}
func newConsumerState(t *testing.T) *consumerState {
return &consumerState{
t: t,
consumed: make(map[string]bool),
}
}
func (s *consumerState) record(val string) {
s.Lock()
defer s.Unlock()
s.consumed[val] = true
atomic.StoreInt32(&s.consumedOnce, 1)
}
func (s *consumerState) snapshot() map[string]bool {
s.Lock()
defer s.Unlock()
out := make(map[string]bool)
for k, v := range s.consumed {
out[k] = v
}
return out
}
func (s *consumerState) waitToConsumeOnce() {
for i := 0; i < 1000; i++ {
if atomic.LoadInt32(&s.consumedOnce) == 0 {
time.Sleep(time.Millisecond)
}
}
require.EqualValues(s.t, 1, atomic.LoadInt32(&s.consumedOnce), "expected to consumer once")
}
func (s *consumerState) assertConsumed(expected map[string]bool) {
for i := 0; i < 1000; i++ {
if snapshot := s.snapshot(); !reflect.DeepEqual(snapshot, expected) {
time.Sleep(time.Millisecond)
}
}
assert.Equal(s.t, expected, s.snapshot())
}
func TestResizeUp(t *testing.T) {
q := NewBoundedQueue(2, func(item interface{}) {
fmt.Printf("dropped: %v\n", item)
})
var firstConsumer, secondConsumer, releaseConsumers sync.WaitGroup
firstConsumer.Add(1)
secondConsumer.Add(1)
releaseConsumers.Add(1)
released, resized := false, false
q.StartConsumers(1, func(item interface{}) {
if !resized { // we'll have a second consumer once the queue is resized
// signal that the worker is processing
firstConsumer.Done()
} else {
// once we release the lock, we might end up with multiple calls to reach this
if !released {
secondConsumer.Done()
}
}
// wait until we are signaled that we can finish
releaseConsumers.Wait()
})
assert.True(t, q.Produce("a")) // in process
firstConsumer.Wait()
assert.True(t, q.Produce("b")) // in queue
assert.True(t, q.Produce("c")) // in queue
assert.False(t, q.Produce("d")) // dropped
assert.EqualValues(t, 2, q.Capacity())
assert.EqualValues(t, q.Capacity(), q.Size())
assert.EqualValues(t, q.Capacity(), len(*q.items))
resized = true
assert.True(t, q.Resize(4))
assert.True(t, q.Produce("e")) // in process by the second consumer
secondConsumer.Wait()
assert.True(t, q.Produce("f")) // in the new queue
assert.True(t, q.Produce("g")) // in the new queue
assert.False(t, q.Produce("h")) // the new queue has the capacity, but the sum of queues doesn't
assert.EqualValues(t, 4, q.Capacity())
assert.EqualValues(t, q.Capacity(), q.Size()) // the combined queues are at the capacity right now
assert.EqualValues(t, 2, len(*q.items)) // the new internal queue should have two items only
released = true
releaseConsumers.Done()
}
func TestResizeDown(t *testing.T) {
q := NewBoundedQueue(4, func(item interface{}) {
fmt.Printf("dropped: %v\n", item)
})
var consumer, releaseConsumers sync.WaitGroup
consumer.Add(1)
releaseConsumers.Add(1)
released := false
q.StartConsumers(1, func(item interface{}) {
// once we release the lock, we might end up with multiple calls to reach this
if !released {
// signal that the worker is processing
consumer.Done()
}
// wait until we are signaled that we can finish
releaseConsumers.Wait()
})
assert.True(t, q.Produce("a")) // in process
consumer.Wait()
assert.True(t, q.Produce("b")) // in queue
assert.True(t, q.Produce("c")) // in queue
assert.True(t, q.Produce("d")) // in queue
assert.True(t, q.Produce("e")) // dropped
assert.EqualValues(t, 4, q.Capacity())
assert.EqualValues(t, q.Capacity(), q.Size())
assert.EqualValues(t, q.Capacity(), len(*q.items))
assert.True(t, q.Resize(2))
assert.False(t, q.Produce("f")) // dropped
assert.EqualValues(t, 2, q.Capacity())
assert.EqualValues(t, 4, q.Size()) // the queue will eventually drain, but it will live for a while over capacity
assert.EqualValues(t, 0, len(*q.items)) // the new queue is empty, as the old queue is still full and over capacity
released = true
releaseConsumers.Done()
}
func TestResizeOldQueueIsDrained(t *testing.T) {
q := NewBoundedQueue(2, func(item interface{}) {
fmt.Printf("dropped: %v\n", item)
})
var consumerReady, consumed, readyToConsume sync.WaitGroup
consumerReady.Add(1)
readyToConsume.Add(1)
consumed.Add(5) // we expect 5 items to be processed
first := true
q.StartConsumers(1, func(item interface{}) {
// first run only
if first {
first = false
consumerReady.Done()
}
readyToConsume.Wait()
consumed.Done()
})
assert.True(t, q.Produce("a"))
consumerReady.Wait()
assert.True(t, q.Produce("b"))
assert.True(t, q.Produce("c"))
assert.False(t, q.Produce("d"))
q.Resize(4)
assert.True(t, q.Produce("e"))
assert.True(t, q.Produce("f"))
assert.False(t, q.Produce("g"))
readyToConsume.Done()
consumed.Wait() // once this returns, we've consumed all items, meaning that both queues are drained
}
func TestNoopResize(t *testing.T) {
q := NewBoundedQueue(2, func(item interface{}) {
})
assert.False(t, q.Resize(2))
}
func TestZeroSize(t *testing.T) {
q := NewBoundedQueue(0, func(item interface{}) {
})
var wg sync.WaitGroup
wg.Add(1)
q.StartConsumers(1, func(item interface{}) {
wg.Done()
})
assert.True(t, q.Produce("a")) // in process
wg.Wait()
// if we didn't finish with a timeout, then we are good
}
func BenchmarkBoundedQueue(b *testing.B) {
q := NewBoundedQueue(1000, func(item interface{}) {
})
q.StartConsumers(10, func(item interface{}) {
})
for n := 0; n < b.N; n++ {
q.Produce(n)
}
}