-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconcurrency_test.go
More file actions
267 lines (246 loc) · 8.5 KB
/
concurrency_test.go
File metadata and controls
267 lines (246 loc) · 8.5 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
265
266
267
package eventbus
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/GoCodeAlone/modular"
)
// Baseline stress test in drop mode to ensure no starvation of async subscribers.
func TestMemoryEventBusConcurrentPublishSubscribe(t *testing.T) {
const (
topic = "concurrent.topic"
publisherCount = 25
messagesPerPub = 200
asyncSubs = 5
syncSubs = 5
)
module := NewModule().(*EventBusModule)
app := newMockApp()
cfg := &EventBusConfig{Engine: "memory", WorkerCount: 50, DefaultEventBufferSize: 1000, MaxEventQueueSize: 100000, DeliveryMode: "drop"}
app.RegisterConfigSection(ModuleName, modular.NewStdConfigProvider(cfg))
if err := module.Init(app); err != nil {
t.Fatalf("init: %v", err)
}
ctx := context.Background()
if err := module.Start(ctx); err != nil {
t.Fatalf("start: %v", err)
}
defer module.Stop(ctx)
var asyncCount, syncCount int64
for i := 0; i < asyncSubs; i++ {
if _, err := module.SubscribeAsync(ctx, topic, func(ctx context.Context, e Event) error { atomic.AddInt64(&asyncCount, 1); return nil }); err != nil {
t.Fatalf("async sub: %v", err)
}
}
for i := 0; i < syncSubs; i++ {
if _, err := module.Subscribe(ctx, topic, func(ctx context.Context, e Event) error { atomic.AddInt64(&syncCount, 1); return nil }); err != nil {
t.Fatalf("sync sub: %v", err)
}
}
var wg sync.WaitGroup
wg.Add(publisherCount)
payload := map[string]any{"v": 1}
for p := 0; p < publisherCount; p++ {
go func() {
defer wg.Done()
for i := 0; i < messagesPerPub; i++ {
_ = module.Publish(ctx, topic, payload)
}
}()
}
wg.Wait()
time.Sleep(500 * time.Millisecond) // drain
finalSync := atomic.LoadInt64(&syncCount)
finalAsync := atomic.LoadInt64(&asyncCount)
if finalSync == 0 || finalAsync == 0 {
t.Fatalf("expected deliveries sync=%d async=%d", finalSync, finalAsync)
}
ratio := float64(finalAsync) / float64(finalSync)
if ratio < 0.10 { // baseline starvation guard for drop mode
t.Fatalf("async severely starved ratio=%.3f sync=%d async=%d", ratio, finalSync, finalAsync)
}
}
// Blocking/timeout mode fairness test expecting closer distribution between sync and async counts.
func TestMemoryEventBusBlockingModeFairness(t *testing.T) {
const (
topic = "blocking.fair"
publisherCount = 10
messagesPerPub = 100
asyncSubs = 3
syncSubs = 3
)
module := NewModule().(*EventBusModule)
app := newMockApp()
cfg := &EventBusConfig{Engine: "memory", WorkerCount: 20, DefaultEventBufferSize: 256, MaxEventQueueSize: 10000, DeliveryMode: "timeout", PublishBlockTimeout: 25 * time.Millisecond}
app.RegisterConfigSection(ModuleName, modular.NewStdConfigProvider(cfg))
if err := module.Init(app); err != nil {
t.Fatalf("init: %v", err)
}
ctx := context.Background()
if err := module.Start(ctx); err != nil {
t.Fatalf("start: %v", err)
}
defer module.Stop(ctx)
var asyncCount, syncCount int64
for i := 0; i < asyncSubs; i++ {
if _, err := module.SubscribeAsync(ctx, topic, func(ctx context.Context, e Event) error { atomic.AddInt64(&asyncCount, 1); return nil }); err != nil {
t.Fatalf("async sub: %v", err)
}
}
for i := 0; i < syncSubs; i++ {
if _, err := module.Subscribe(ctx, topic, func(ctx context.Context, e Event) error { atomic.AddInt64(&syncCount, 1); return nil }); err != nil {
t.Fatalf("sync sub: %v", err)
}
}
var wg sync.WaitGroup
wg.Add(publisherCount)
payload := map[string]any{"v": 2}
for p := 0; p < publisherCount; p++ {
go func() {
defer wg.Done()
for i := 0; i < messagesPerPub; i++ {
_ = module.Publish(ctx, topic, payload)
}
}()
}
wg.Wait()
// Drain/settle loop rationale:
// 1. Sync subscribers increment delivered immediately after handler completion; async subscribers enqueue work
// that is processed by the worker pool, so their counters lag briefly after publishers finish.
// 2. Without waiting for stabilization the async:sync ratio appears artificially low, causing flaky failures.
// 3. We poll until three consecutive ticks show no async progress (or timeout) to approximate a quiescent state.
// 4. Ratio bounds are deliberately wide (15%-300%) to only fail on pathological starvation while tolerating
// timing variance across CI environments.
deadline := time.Now().Add(2 * time.Second)
var lastAsync, stableTicks int64
for time.Now().Before(deadline) {
currAsync := atomic.LoadInt64(&asyncCount)
if currAsync == lastAsync {
stableTicks++
if stableTicks >= 3 { // ~3 consecutive ticks (~150ms) of no change
break
}
} else {
stableTicks = 0
lastAsync = currAsync
}
time.Sleep(50 * time.Millisecond)
}
finalSync := atomic.LoadInt64(&syncCount)
finalAsync := atomic.LoadInt64(&asyncCount)
if finalSync == 0 || finalAsync == 0 {
t.Fatalf("expected deliveries sync=%d async=%d", finalSync, finalAsync)
}
ratio := float64(finalAsync) / float64(finalSync)
time.Sleep(100 * time.Millisecond)
finalSync = atomic.LoadInt64(&syncCount)
finalAsync = atomic.LoadInt64(&asyncCount)
ratio = float64(finalAsync) / float64(finalSync)
// Fairness criteria: async should not be severely starved. Empirical CI runs after
// upgrading to v1.11.x showed ratios in the 0.17-0.20 range despite healthy async
// processing due to tighter scheduling contention in timeout mode. We relax the
// lower bound from 25% to 15% while keeping it stricter than the drop-mode test (10%).
if ratio < 0.15 || ratio > 3.0 {
t.Fatalf("unfair distribution ratio=%.2f sync=%d async=%d", ratio, finalSync, finalAsync)
}
}
// Unsubscribe behavior under load.
func TestMemoryEventBusUnsubscribeDuringPublish(t *testing.T) {
const topic = "unsubscribe.topic"
module := NewModule().(*EventBusModule)
app := newMockApp()
cfg := &EventBusConfig{Engine: "memory"}
app.RegisterConfigSection(ModuleName, modular.NewStdConfigProvider(cfg))
if err := module.Init(app); err != nil {
t.Fatalf("init: %v", err)
}
ctx := context.Background()
if err := module.Start(ctx); err != nil {
t.Fatalf("start: %v", err)
}
defer module.Stop(ctx)
var count int64
sub, err := module.Subscribe(ctx, topic, func(ctx context.Context, e Event) error { atomic.AddInt64(&count, 1); return nil })
if err != nil {
t.Fatalf("subscribe: %v", err)
}
stopPub := make(chan struct{})
done := make(chan struct{})
go func() {
payload := map[string]any{"k": "v"}
for i := 0; i < 5000; i++ {
select {
case <-stopPub:
close(done)
return
default:
}
_ = module.Publish(ctx, topic, payload)
}
close(done)
}()
time.Sleep(5 * time.Millisecond)
if err := module.Unsubscribe(ctx, sub); err != nil {
t.Fatalf("unsubscribe: %v", err)
}
close(stopPub)
<-done
final := atomic.LoadInt64(&count)
if final == 0 {
t.Fatalf("expected some deliveries before unsubscribe")
}
time.Sleep(50 * time.Millisecond)
if atomic.LoadInt64(&count) != final {
t.Fatalf("deliveries continued after unsubscribe")
}
}
// Stats behavior: ensure counters reflect delivered + dropped approximating total publishes and are monotonic.
func TestMemoryEventBusStatsAccounting(t *testing.T) {
module := NewModule().(*EventBusModule)
app := newMockApp()
cfg := &EventBusConfig{Engine: "memory", WorkerCount: 10, DefaultEventBufferSize: 128, MaxEventQueueSize: 2000, DeliveryMode: "drop"}
app.RegisterConfigSection(ModuleName, modular.NewStdConfigProvider(cfg))
if err := module.Init(app); err != nil {
t.Fatalf("init: %v", err)
}
ctx := context.Background()
if err := module.Start(ctx); err != nil {
t.Fatalf("start: %v", err)
}
defer module.Stop(ctx)
topic := "stats.topic"
var recv int64
// A slow subscriber to induce some drops under pressure.
if _, err := module.SubscribeAsync(ctx, topic, func(ctx context.Context, e Event) error {
atomic.AddInt64(&recv, 1)
time.Sleep(200 * time.Microsecond)
return nil
}); err != nil {
t.Fatalf("subscribe: %v", err)
}
publishCount := 1000
payload := map[string]any{"n": 1}
for i := 0; i < publishCount; i++ {
_ = module.Publish(ctx, topic, payload)
}
// Allow processing/drain
time.Sleep(500 * time.Millisecond)
delivered, dropped := module.Stats()
totalAccounted := delivered + dropped
if totalAccounted == 0 {
t.Fatalf("expected some accounted events")
}
if totalAccounted > uint64(publishCount) {
t.Fatalf("accounted exceeds publishes accounted=%d publishes=%d", totalAccounted, publishCount)
}
// Delivered should match recv
if delivered != uint64(atomic.LoadInt64(&recv)) {
t.Fatalf("delivered mismatch stats=%d recv=%d", delivered, recv)
}
// Basic ratio sanity: at least some delivered
if delivered == 0 {
t.Fatalf("no delivered events recorded")
}
}