-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
bounded_memory_queue_test.go
270 lines (223 loc) · 7.31 KB
/
bounded_memory_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
// Copyright The OpenTelemetry Authors
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"context"
"fmt"
"reflect"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/exporter/exportertest"
)
func newNopQueueSettings(callback func(item Request)) QueueSettings {
return QueueSettings{
CreateSettings: exportertest.NewNopCreateSettings(),
DataType: component.DataTypeMetrics,
Callback: callback,
}
}
type stringRequest struct {
Request
str string
}
func newStringRequest(str string) Request {
return stringRequest{str: str}
}
// 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) {
q := NewBoundedMemoryQueue(1, 1)
var startLock sync.Mutex
startLock.Lock() // block consumers
consumerState := newConsumerState(t)
assert.NoError(t, q.Start(context.Background(), componenttest.NewNopHost(), newNopQueueSettings(func(item Request) {
consumerState.record(item.(stringRequest).str)
// block further processing until startLock is released
startLock.Lock()
//nolint:staticcheck // SA2001 ignore this!
startLock.Unlock()
})))
assert.True(t, q.Produce(newStringRequest("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(newStringRequest("b")))
assert.Equal(t, 1, q.Size())
// the second should be rejected since the queue is full
assert.False(t, q.Produce(newStringRequest("c")))
assert.Equal(t, 1, q.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(newStringRequest(item)))
expected[item] = true
consumerState.assertConsumed(expected)
}
assert.NoError(t, q.Shutdown(context.Background()))
assert.False(t, q.Produce(newStringRequest("x")), "cannot push to closed queue")
}
// In this test we run a queue with many items and a slow consumer.
// When the queue is stopped, the remaining items should be processed.
// Due to the way q.Stop() waits for all consumers to finish, the
// same lock strategy use above will not work, as calling Unlock
// only after Stop will mean the consumers are still locked while
// trying to perform the final consumptions.
func TestShutdownWhileNotEmpty(t *testing.T) {
q := NewBoundedMemoryQueue(10, 1)
consumerState := newConsumerState(t)
assert.NoError(t, q.Start(context.Background(), componenttest.NewNopHost(), newNopQueueSettings(func(item Request) {
consumerState.record(item.(stringRequest).str)
time.Sleep(1 * time.Second)
})))
q.Produce(newStringRequest("a"))
q.Produce(newStringRequest("b"))
q.Produce(newStringRequest("c"))
q.Produce(newStringRequest("d"))
q.Produce(newStringRequest("e"))
q.Produce(newStringRequest("f"))
q.Produce(newStringRequest("g"))
q.Produce(newStringRequest("h"))
q.Produce(newStringRequest("i"))
q.Produce(newStringRequest("j"))
assert.NoError(t, q.Shutdown(context.Background()))
assert.False(t, q.Produce(newStringRequest("x")), "cannot push to closed queue")
consumerState.assertConsumed(map[string]bool{
"a": true,
"b": true,
"c": true,
"d": true,
"e": true,
"f": true,
"g": true,
"h": true,
"i": true,
"j": true,
})
assert.Equal(t, 0, q.Size())
}
func Benchmark_QueueUsage_10000_1_50000(b *testing.B) {
queueUsage(b, 10000, 1, 50000)
}
func Benchmark_QueueUsage_10000_2_50000(b *testing.B) {
queueUsage(b, 10000, 2, 50000)
}
func Benchmark_QueueUsage_10000_5_50000(b *testing.B) {
queueUsage(b, 10000, 5, 50000)
}
func Benchmark_QueueUsage_10000_10_50000(b *testing.B) {
queueUsage(b, 10000, 10, 50000)
}
func Benchmark_QueueUsage_50000_1_50000(b *testing.B) {
queueUsage(b, 50000, 1, 50000)
}
func Benchmark_QueueUsage_50000_2_50000(b *testing.B) {
queueUsage(b, 50000, 2, 50000)
}
func Benchmark_QueueUsage_50000_5_50000(b *testing.B) {
queueUsage(b, 50000, 5, 50000)
}
func Benchmark_QueueUsage_50000_10_50000(b *testing.B) {
queueUsage(b, 50000, 10, 50000)
}
func Benchmark_QueueUsage_10000_1_250000(b *testing.B) {
queueUsage(b, 10000, 1, 250000)
}
func Benchmark_QueueUsage_10000_2_250000(b *testing.B) {
queueUsage(b, 10000, 2, 250000)
}
func Benchmark_QueueUsage_10000_5_250000(b *testing.B) {
queueUsage(b, 10000, 5, 250000)
}
func Benchmark_QueueUsage_10000_10_250000(b *testing.B) {
queueUsage(b, 10000, 10, 250000)
}
func queueUsage(b *testing.B, capacity int, numConsumers int, numberOfItems int) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q := NewBoundedMemoryQueue(capacity, numConsumers)
err := q.Start(context.Background(), componenttest.NewNopHost(), newNopQueueSettings(func(item Request) {
time.Sleep(1 * time.Millisecond)
}))
require.NoError(b, err)
for j := 0; j < numberOfItems; j++ {
q.Produce(newStringRequest(fmt.Sprintf("%d", j)))
}
assert.NoError(b, q.Shutdown(context.Background()))
}
}
type consumerState struct {
sync.Mutex
t *testing.T
consumed map[string]bool
consumedOnce *atomic.Bool
}
func newConsumerState(t *testing.T) *consumerState {
return &consumerState{
t: t,
consumed: make(map[string]bool),
consumedOnce: &atomic.Bool{},
}
}
func (s *consumerState) record(val string) {
s.Lock()
defer s.Unlock()
s.consumed[val] = true
s.consumedOnce.Store(true)
}
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() {
require.Eventually(s.t, s.consumedOnce.Load, 2*time.Second, 10*time.Millisecond, "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 TestZeroSizeWithConsumers(t *testing.T) {
q := NewBoundedMemoryQueue(0, 1)
err := q.Start(context.Background(), componenttest.NewNopHost(), newNopQueueSettings(func(item Request) {}))
assert.NoError(t, err)
assert.True(t, q.Produce(newStringRequest("a"))) // in process
assert.NoError(t, q.Shutdown(context.Background()))
}
func TestZeroSizeNoConsumers(t *testing.T) {
q := NewBoundedMemoryQueue(0, 0)
err := q.Start(context.Background(), componenttest.NewNopHost(), newNopQueueSettings(func(item Request) {}))
assert.NoError(t, err)
assert.False(t, q.Produce(newStringRequest("a"))) // in process
assert.NoError(t, q.Shutdown(context.Background()))
}