2026-04-20 22:58:54 +08:00
|
|
|
package eventbus
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-21 11:50:43 +08:00
|
|
|
"context"
|
2026-04-20 22:58:54 +08:00
|
|
|
"reflect"
|
|
|
|
|
"sync"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-21 16:41:12 +08:00
|
|
|
type userCreatedEvent struct {
|
|
|
|
|
UserID int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type orderCreatedEvent struct {
|
|
|
|
|
OrderID int64
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 22:58:54 +08:00
|
|
|
func TestGetReturnsSameBusForSameType(t *testing.T) {
|
|
|
|
|
resetManagerForTest(t)
|
|
|
|
|
|
2026-04-21 16:41:12 +08:00
|
|
|
first := Get[userCreatedEvent]()
|
|
|
|
|
second := Get[userCreatedEvent]()
|
2026-04-20 22:58:54 +08:00
|
|
|
|
|
|
|
|
if first != second {
|
|
|
|
|
t.Fatalf("expected same bus instance for same event type")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetReturnsDifferentBusForDifferentTypes(t *testing.T) {
|
|
|
|
|
resetManagerForTest(t)
|
|
|
|
|
|
2026-04-21 16:41:12 +08:00
|
|
|
userBus := Get[userCreatedEvent]()
|
|
|
|
|
orderBus := Get[orderCreatedEvent]()
|
2026-04-20 22:58:54 +08:00
|
|
|
|
|
|
|
|
if userBus == any(orderBus) {
|
|
|
|
|
t.Fatalf("expected different bus instances for different event types")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetCreatesOnlyOneBusUnderConcurrency(t *testing.T) {
|
|
|
|
|
resetManagerForTest(t)
|
|
|
|
|
|
|
|
|
|
const workers = 32
|
|
|
|
|
|
2026-04-21 16:41:12 +08:00
|
|
|
results := make(chan *Bus[userCreatedEvent], workers)
|
2026-04-20 22:58:54 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
|
|
for range workers {
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
2026-04-21 16:41:12 +08:00
|
|
|
results <- Get[userCreatedEvent]()
|
2026-04-20 22:58:54 +08:00
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
close(results)
|
|
|
|
|
|
2026-04-21 16:41:12 +08:00
|
|
|
var first *Bus[userCreatedEvent]
|
2026-04-20 22:58:54 +08:00
|
|
|
for bus := range results {
|
|
|
|
|
if first == nil {
|
|
|
|
|
first = bus
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if bus != first {
|
|
|
|
|
t.Fatalf("expected same bus instance for concurrent access")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 11:50:43 +08:00
|
|
|
func TestRegisterReturnsConfiguredGlobalBus(t *testing.T) {
|
|
|
|
|
resetManagerForTest(t)
|
|
|
|
|
|
|
|
|
|
var handled bool
|
|
|
|
|
registered := Register[testEvent](WithErrorHandler[testEvent](func(ctx context.Context, err error) {
|
|
|
|
|
handled = true
|
|
|
|
|
}))
|
|
|
|
|
got := Get[testEvent]()
|
|
|
|
|
|
|
|
|
|
if registered != got {
|
|
|
|
|
t.Fatalf("expected registered bus to be returned by Get")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
got.handleError(context.Background(), assertErr{})
|
|
|
|
|
if !handled {
|
|
|
|
|
t.Fatalf("expected registered error handler to be used")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type assertErr struct{}
|
|
|
|
|
|
|
|
|
|
func (assertErr) Error() string {
|
|
|
|
|
return "assert"
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 22:58:54 +08:00
|
|
|
func resetManagerForTest(t *testing.T) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
|
buss = make(map[reflect.Type]any)
|
|
|
|
|
mu.Unlock()
|
|
|
|
|
}
|