Files
go-iar-notificator/bus/bus_test.go
2025-09-08 13:27:09 -04:00

139 lines
3.2 KiB
Go

package bus_test
import (
"sync"
"testing"
"time"
"git.savin.nyc/alex/go-iar-notificator/bus"
)
func TestCustomEventBus_SubscribeAndPublish(t *testing.T) {
eb := bus.NewCustomEventBus()
// Channel to signal handler execution
handlerCalled := make(chan string, 1)
// Subscribe to an event
eb.Subscribe("test_event", "handler1", func(data any) {
if str, ok := data.(string); ok {
handlerCalled <- str
}
})
// Publish the event
eb.Publish("test_event", "test_data")
// Wait for the handler to be called
select {
case received := <-handlerCalled:
if received != "test_data" {
t.Errorf("Expected 'test_data', got %s", received)
}
case <-time.After(1 * time.Second):
t.Error("Handler was not called within timeout")
}
}
func TestCustomEventBus_MultipleSubscribers(t *testing.T) {
eb := bus.NewCustomEventBus()
var wg sync.WaitGroup
results := make(chan string, 2)
// Subscribe two handlers
eb.Subscribe("test_event", "handler1", func(data any) {
results <- "handler1"
wg.Done()
})
eb.Subscribe("test_event", "handler2", func(data any) {
results <- "handler2"
wg.Done()
})
wg.Add(2)
// Publish the event
eb.Publish("test_event", nil)
// Wait for both handlers
wg.Wait()
close(results)
// Check that both handlers were called
handlerCount := 0
for range results {
handlerCount++
}
if handlerCount != 2 {
t.Errorf("Expected 2 handlers to be called, got %d", handlerCount)
}
}
func TestCustomEventBus_GetSubscribers(t *testing.T) {
eb := bus.NewCustomEventBus()
// Subscribe to events
eb.Subscribe("event1", "handler1", func(data any) {})
eb.Subscribe("event1", "handler2", func(data any) {})
eb.Subscribe("event2", "handler3", func(data any) {})
subscribers := eb.GetSubscribers()
// Check event1
if len(subscribers["event1"]) != 2 {
t.Errorf("Expected 2 subscribers for event1, got %d", len(subscribers["event1"]))
}
if subscribers["event1"][0] != "handler1" || subscribers["event1"][1] != "handler2" {
t.Errorf("Unexpected subscribers for event1: %v", subscribers["event1"])
}
// Check event2
if len(subscribers["event2"]) != 1 {
t.Errorf("Expected 1 subscriber for event2, got %d", len(subscribers["event2"]))
}
if subscribers["event2"][0] != "handler3" {
t.Errorf("Unexpected subscriber for event2: %v", subscribers["event2"])
}
// Check non-existent event
if len(subscribers["event3"]) != 0 {
t.Errorf("Expected 0 subscribers for event3, got %d", len(subscribers["event3"]))
}
}
func TestCustomEventBus_NoSubscribers(t *testing.T) {
eb := bus.NewCustomEventBus()
// Publish to an event with no subscribers (should not panic)
eb.Publish("no_subscribers", "data")
// Should complete without issues
}
func TestCustomEventBus_ConcurrentAccess(t *testing.T) {
eb := bus.NewCustomEventBus()
var wg sync.WaitGroup
// Concurrently subscribe and publish
for i := 0; i < 10; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
eb.Subscribe("concurrent_event", string(rune('A'+id)), func(data any) {})
}(i)
}
wg.Wait()
// Publish
eb.Publish("concurrent_event", nil)
// Check subscribers
subscribers := eb.GetSubscribers()
if len(subscribers["concurrent_event"]) != 10 {
t.Errorf("Expected 10 subscribers, got %d", len(subscribers["concurrent_event"]))
}
}