test(concurrency): 修复异步测试仓库与事件计数竞争

This commit is contained in:
姚章浩
2026-10-28 10:48:56 +08:00
parent 23387779c1
commit 3619f44ac8
2 changed files with 36 additions and 7 deletions
+5 -4
View File
@@ -3,6 +3,7 @@ package event
import (
"context"
"fmt"
"sync/atomic"
"testing"
"time"
)
@@ -201,13 +202,13 @@ func TestEventBus_EmitAndWait(t *testing.T) {
ctx := context.Background()
bus := NewAsyncEventBus()
counter := 0
var counter atomic.Int32
// Register handlers
for i := 0; i < 3; i++ {
bus.On(EventQueryReceived, func(ctx context.Context, event Event) error {
time.Sleep(50 * time.Millisecond)
counter++
counter.Add(1)
return nil
})
}
@@ -222,8 +223,8 @@ func TestEventBus_EmitAndWait(t *testing.T) {
t.Errorf("EmitAndWait failed: %v", err)
}
if counter != 3 {
t.Errorf("Expected 3 handlers to complete, got %d", counter)
if got := counter.Load(); got != 3 {
t.Errorf("Expected 3 handlers to complete, got %d", got)
}
}