34 lines
709 B
Go
34 lines
709 B
Go
package application
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"chub/internal/domain"
|
|
)
|
|
|
|
func TestEventBusPublishesAndCancels(t *testing.T) {
|
|
bus := NewEventBus()
|
|
ch, cancel := bus.Subscribe(1)
|
|
event := domain.BrowserEvent{Kind: domain.EventBrowserStarted, InstanceID: "demo"}
|
|
bus.Publish(event)
|
|
select {
|
|
case got := <-ch:
|
|
if got.InstanceID != event.InstanceID || got.Kind != event.Kind {
|
|
t.Fatalf("unexpected event: %#v", got)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("event was not published")
|
|
}
|
|
cancel()
|
|
bus.Publish(event)
|
|
select {
|
|
case _, ok := <-ch:
|
|
if ok {
|
|
t.Fatal("cancelled subscriber received an event")
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("subscriber was not closed")
|
|
}
|
|
}
|