-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocking_batch_test.go
148 lines (128 loc) · 2.96 KB
/
blocking_batch_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
package batcher
import (
"context"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"sync"
"testing"
"time"
)
func TestBlockingQueueConcurrency_PopAll_Empty(t *testing.T) {
bq := NewBlockingQueue[string](1)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
bq.PopAll(ctx)
cancel()
}
func TestBlockingQueueConcurrency_Push_PopAll(t *testing.T) {
bq := NewBlockingQueue[string](1)
bq.Push("a")
items := bq.PopAll(context.TODO())
require.Equal(t, []string{"a"}, items)
}
func TestBlockingQueueConcurrency_WithConcurrency(t *testing.T) {
var (
publishers = 321
consumers = 123
items = 2234
queueSize = 33
)
bq := NewBlockingQueue[int](queueSize)
ctx := context.Background()
pubGroup, ctx := errgroup.WithContext(ctx)
consGroup, _ := errgroup.WithContext(ctx)
var publishing = true
for i := 0; i < publishers; i++ {
pubGroup.Go(func() error {
for i := 0; i < items; i++ {
bq.Push(i)
}
return nil
})
}
popped := make([]int, 0)
cLock := &sync.Mutex{}
for i := 0; i < consumers; i++ {
consGroup.Go(func() error {
for publishing || bq.Size() > 0 {
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
items := bq.PopAll(ctx)
if items == nil {
cancel()
continue
}
cLock.Lock()
popped = append(popped, items...)
cLock.Unlock()
cancel()
}
return nil
})
}
require.NoError(t, pubGroup.Wait())
publishing = false
require.NoError(t, consGroup.Wait())
require.Len(t, popped, items*publishers)
require.Equal(t, 0, bq.Size())
}
func TestPopAll_Full(t *testing.T) {
bq := NewBlockingQueue[int](123)
for i := 0; i < 100; i++ {
bq.Push(i)
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
bq.PopAll(ctx)
cancel()
}
func BenchmarkBlockingQueue(b *testing.B) {
bq := NewBlockingQueue[int](123)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for i := 0; i < 123; i++ {
bq.Push(i)
}
bq.PopAll(context.TODO())
}
}
func BenchmarkBlockingQueue_Concurrently(b *testing.B) {
var (
publishers = 321
consumers = 123
items = 2234
queueSize = 33
)
bq := NewBlockingQueue[int](queueSize)
ctx := context.Background()
pubGroup, ctx := errgroup.WithContext(ctx)
consGroup, _ := errgroup.WithContext(ctx)
var publishing = true
for i := 0; i < publishers; i++ {
pubGroup.Go(func() error {
for i := 0; i < items; i++ {
bq.Push(i)
}
return nil
})
}
popped := make([]int, 0)
cLock := &sync.Mutex{}
for i := 0; i < consumers; i++ {
consGroup.Go(func() error {
for publishing || bq.Size() > 0 {
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
items := bq.PopAll(ctx)
if items == nil {
cancel()
continue
}
cLock.Lock()
popped = append(popped, items...)
cLock.Unlock()
cancel()
}
return nil
})
}
require.NoError(b, pubGroup.Wait())
publishing = false
require.NoError(b, consGroup.Wait())
}