-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathseqflow_test.go
More file actions
264 lines (228 loc) · 5.43 KB
/
Copy pathseqflow_test.go
File metadata and controls
264 lines (228 loc) · 5.43 KB
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
package seqflow
import (
"context"
"sync"
"testing"
"time"
)
type sumHandler struct {
mu sync.Mutex
sum int64
}
func (h *sumHandler) Handle(lower, upper int64) {
h.mu.Lock()
defer h.mu.Unlock()
for i := lower; i <= upper; i++ {
h.sum += i
}
}
func TestNew_InvalidCapacity(t *testing.T) {
h := &sumHandler{}
_, err := New[int64](WithCapacity(0), WithHandler("a", h))
if err == nil {
t.Error("容量为 0 应返回错误")
}
_, err = New[int64](WithCapacity(3), WithHandler("a", h))
if err == nil {
t.Error("非 2 的幂应返回错误")
}
}
func TestNew_NoHandlers(t *testing.T) {
_, err := New[int64](WithCapacity(1024))
if err == nil {
t.Error("无 handler 应返回错误")
}
}
func TestNew_DuplicateHandler(t *testing.T) {
h := &sumHandler{}
_, err := New[int64](WithCapacity(1024), WithHandler("a", h), WithHandler("a", h))
if err == nil {
t.Error("重复 handler 名应返回错误")
}
}
func TestDisruptor_SingleProducerSingleConsumer(t *testing.T) {
h := &sumHandler{}
d, err := New[int64](
WithCapacity(64),
WithHandler("sum", h),
)
if err != nil {
t.Fatalf("New 错误: %v", err)
}
go d.Listen()
// 发布 10 个事件
for i := int64(0); i < 10; i++ {
upper, err := d.Reserve(1)
if err != nil {
t.Fatalf("Reserve 错误: %v", err)
}
d.RingBuffer().Set(upper, i*10)
d.Commit(upper, upper)
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := d.Drain(ctx); err != nil {
t.Fatalf("Drain 错误: %v", err)
}
// 序列 0..9 之和 = 45
h.mu.Lock()
defer h.mu.Unlock()
if h.sum != 45 {
t.Errorf("sum = %d, 期望 45(序列 0-9 之和)", h.sum)
}
}
func TestDisruptor_MultiProducer(t *testing.T) {
h := &sumHandler{}
d, err := New[int64](
WithCapacity(64),
WithWriterCount(4),
WithHandler("sum", h),
)
if err != nil {
t.Fatalf("New 错误: %v", err)
}
go d.Listen()
var wg sync.WaitGroup
perWriter := 100
wg.Add(4)
for w := 0; w < 4; w++ {
go func() {
defer wg.Done()
for i := 0; i < perWriter; i++ {
upper, err := d.Reserve(1)
if err != nil {
t.Errorf("Reserve 错误: %v", err)
return
}
d.RingBuffer().Set(upper, 1)
d.Commit(upper, upper)
}
}()
}
wg.Wait()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := d.Drain(ctx); err != nil {
t.Fatalf("Drain 错误: %v", err)
}
}
func TestDisruptor_DAG_Diamond(t *testing.T) {
var aCount, bCount, cCount, dCount int64
var mu sync.Mutex
counter := func(ptr *int64) Handler {
return handlerFunc(func(lower, upper int64) {
mu.Lock()
*ptr += upper - lower + 1
mu.Unlock()
})
}
d, err := New[int64](
WithCapacity(64),
WithHandler("A", counter(&aCount)),
WithHandler("B", counter(&bCount), DependsOn("A")),
WithHandler("C", counter(&cCount), DependsOn("A")),
WithHandler("D", counter(&dCount), DependsOn("B", "C")),
)
if err != nil {
t.Fatalf("New 错误: %v", err)
}
go d.Listen()
for i := 0; i < 100; i++ {
upper, _ := d.Reserve(1)
d.RingBuffer().Set(upper, int64(i))
d.Commit(upper, upper)
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := d.Drain(ctx); err != nil {
t.Fatalf("Drain 错误: %v", err)
}
mu.Lock()
defer mu.Unlock()
for _, tc := range []struct {
name string
count int64
}{
{"A", aCount}, {"B", bCount}, {"C", cCount}, {"D", dCount},
} {
if tc.count != 100 {
t.Errorf("handler %s 处理了 %d 个事件, 期望 100", tc.name, tc.count)
}
}
}
func TestDisruptor_Close(t *testing.T) {
h := &sumHandler{}
d, err := New[int64](WithCapacity(64), WithHandler("sum", h))
if err != nil {
t.Fatalf("New 错误: %v", err)
}
done := make(chan struct{})
go func() {
d.Listen()
close(done)
}()
time.Sleep(10 * time.Millisecond)
d.Close()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("Close 后 Listen 未返回")
}
}
func TestDisruptor_ReserveAfterClose(t *testing.T) {
h := &sumHandler{}
d, _ := New[int64](WithCapacity(64), WithHandler("sum", h))
go d.Listen()
time.Sleep(10 * time.Millisecond)
d.Close()
_, err := d.Reserve(1)
if err != ErrClosed {
t.Errorf("Close 后 Reserve = %v, 期望 ErrClosed", err)
}
_, err = d.TryReserve(1)
if err != ErrClosed {
t.Errorf("Close 后 TryReserve = %v, 期望 ErrClosed", err)
}
}
func TestDisruptor_DrainThenClose(t *testing.T) {
h := &sumHandler{}
d, _ := New[int64](WithCapacity(64), WithHandler("sum", h))
go d.Listen()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_ = d.Drain(ctx)
err := d.Close()
if err != ErrClosed {
t.Errorf("Drain 后 Close = %v, 期望 ErrClosed", err)
}
}
func TestDisruptor_CloseThenDrain(t *testing.T) {
h := &sumHandler{}
d, _ := New[int64](WithCapacity(64), WithHandler("sum", h))
go d.Listen()
time.Sleep(10 * time.Millisecond)
d.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err := d.Drain(ctx)
if err != ErrClosed {
t.Errorf("Close 后 Drain = %v, 期望 ErrClosed", err)
}
}
func TestDisruptor_WithWaitStrategy(t *testing.T) {
h := &sumHandler{}
d, err := New[int64](
WithCapacity(64),
WithWaitStrategy(NewYieldingStrategy()),
WithHandler("sum", h),
)
if err != nil {
t.Fatalf("New 错误: %v", err)
}
go d.Listen()
upper, _ := d.Reserve(1)
d.Commit(upper, upper)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
d.Drain(ctx)
}