superkit/event/event_test.go

33 lines
676 B
Go
Raw Normal View History

2024-06-06 16:02:04 +02:00
package event
import (
2024-06-07 11:03:09 +02:00
"context"
"reflect"
2024-06-06 16:02:04 +02:00
"testing"
)
2024-06-07 11:03:09 +02:00
func TestEventSubscribeEmit(t *testing.T) {
2024-06-23 08:40:24 +02:00
expect := 1
ctx, cancel := context.WithCancel(context.Background())
Subscribe("foo.a", func(_ context.Context, event any) {
defer cancel()
2024-06-07 11:03:09 +02:00
value, ok := event.(int)
if !ok {
t.Errorf("expected int got %v", reflect.TypeOf(event))
}
if value != 1 {
t.Errorf("expected %d got %d", expect, value)
}
})
2024-06-23 08:40:24 +02:00
Emit("foo.a", expect)
<-ctx.Done()
2024-06-07 11:03:09 +02:00
}
func TestUnsubscribe(t *testing.T) {
2024-06-23 08:40:24 +02:00
sub := Subscribe("foo.b", func(_ context.Context, _ any) {})
2024-06-07 11:03:09 +02:00
Unsubscribe(sub)
2024-06-23 08:40:24 +02:00
if _, ok := stream.subs["foo.b"]; ok {
2024-06-07 11:03:09 +02:00
t.Errorf("expected topic foo.bar to be deleted")
}
2024-06-06 16:02:04 +02:00
}