feat(eventbus): refactor handler storage to use a slice and add tests for handler order

This commit is contained in:
mlogclub
2026-04-21 16:42:29 +08:00
parent 6c005d5daa
commit 6789a8dee0
2 changed files with 79 additions and 9 deletions
+55
View File
@@ -3,6 +3,7 @@ package eventbus
import (
"context"
"errors"
"reflect"
"sync"
"sync/atomic"
"testing"
@@ -53,6 +54,60 @@ func TestPublishReturnsJoinedHandlerErrors(t *testing.T) {
}
}
func TestPublishCallsHandlersInSubscribeOrder(t *testing.T) {
bus := New[testEvent]()
calls := make([]int, 0, 3)
bus.Subscribe(func(ctx context.Context, event testEvent) error {
calls = append(calls, 1)
return nil
})
bus.Subscribe(func(ctx context.Context, event testEvent) error {
calls = append(calls, 2)
return nil
})
bus.Subscribe(func(ctx context.Context, event testEvent) error {
calls = append(calls, 3)
return nil
})
if err := bus.Publish(context.Background(), testEvent{}); err != nil {
t.Fatalf("publish failed: %v", err)
}
if !reflect.DeepEqual(calls, []int{1, 2, 3}) {
t.Fatalf("expected handlers to run in subscribe order, got %#v", calls)
}
}
func TestUnsubscribeKeepsRemainingHandlerOrder(t *testing.T) {
bus := New[testEvent]()
calls := make([]int, 0, 2)
bus.Subscribe(func(ctx context.Context, event testEvent) error {
calls = append(calls, 1)
return nil
})
_, unsubscribe := bus.Subscribe(func(ctx context.Context, event testEvent) error {
calls = append(calls, 2)
return nil
})
bus.Subscribe(func(ctx context.Context, event testEvent) error {
calls = append(calls, 3)
return nil
})
unsubscribe()
if err := bus.Publish(context.Background(), testEvent{}); err != nil {
t.Fatalf("publish failed: %v", err)
}
if !reflect.DeepEqual(calls, []int{1, 3}) {
t.Fatalf("expected remaining handlers to keep order, got %#v", calls)
}
}
func TestPanicReturnsError(t *testing.T) {
bus := New[testEvent]()