120 lines
2.1 KiB
Go
120 lines
2.1 KiB
Go
package eventbus
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
)
|
|
|
|
func TestBusBasics(t *testing.T) {
|
|
bus, err := NewEventBus(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
|
|
eventType := logical.EventType("someType")
|
|
|
|
event, err := logical.NewEvent()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = bus.Send(ctx, eventType, event)
|
|
if err != ErrNotStarted {
|
|
t.Errorf("Expected not started error but got: %v", err)
|
|
}
|
|
|
|
bus.Start()
|
|
|
|
err = bus.Send(ctx, eventType, event)
|
|
if err != nil {
|
|
t.Errorf("Expected no error sending: %v", err)
|
|
}
|
|
|
|
ch, err := bus.Subscribe(ctx, eventType)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
event, err = logical.NewEvent()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = bus.Send(ctx, eventType, event)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
timeout := time.After(1 * time.Second)
|
|
select {
|
|
case message := <-ch:
|
|
if message.ID() != event.ID() {
|
|
t.Errorf("Got unexpected message: %+v", message)
|
|
}
|
|
case <-timeout:
|
|
t.Error("Timeout waiting for message")
|
|
}
|
|
}
|
|
|
|
func TestBus2Subscriptions(t *testing.T) {
|
|
bus, err := NewEventBus(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
|
|
eventType1 := logical.EventType("someType1")
|
|
eventType2 := logical.EventType("someType2")
|
|
bus.Start()
|
|
|
|
ch1, err := bus.Subscribe(ctx, eventType1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ch2, err := bus.Subscribe(ctx, eventType2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
event1, err := logical.NewEvent()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
event2, err := logical.NewEvent()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = bus.Send(ctx, eventType2, event2)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
err = bus.Send(ctx, eventType1, event1)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
timeout := time.After(1 * time.Second)
|
|
select {
|
|
case message := <-ch1:
|
|
if message.ID() != event1.ID() {
|
|
t.Errorf("Got unexpected message: %v", message)
|
|
}
|
|
case <-timeout:
|
|
t.Error("Timeout waiting for event1")
|
|
}
|
|
select {
|
|
case message := <-ch2:
|
|
if message.ID() != event2.ID() {
|
|
t.Errorf("Got unexpected message: %v", message)
|
|
}
|
|
case <-timeout:
|
|
t.Error("Timeout waiting for event2")
|
|
}
|
|
}
|