dashboard/pkg/observe/observe_test.go
2025-05-21 16:46:17 +08:00

68 lines
1.2 KiB
Go

package observe
import (
"context"
"errors"
"fmt"
"testing"
"time"
)
type baseObserver struct {
name string
}
func newBaseObserver(name string) *baseObserver {
return &baseObserver{
name: name,
}
}
func (b *baseObserver) OnChange(ctx context.Context, e *Event) error {
fmt.Println(b.name, e.Topic, e.Value)
return errors.New("jjjjj")
}
func Test_syncEventBus(t *testing.T) {
obA := newBaseObserver("A")
obB := newBaseObserver("B")
obC := newBaseObserver("C")
obD := newBaseObserver("D")
sBus := NewSyncEventBus()
topic := "sync"
sBus.Subscribe(topic, obA)
sBus.Subscribe(topic, obB)
sBus.Subscribe(topic, obC)
sBus.Subscribe(topic, obD)
sBus.Publish(context.Background(), &Event{
Topic: topic,
Value: "hello redhat",
})
}
func Test_asyncEventBus(t *testing.T) {
obA := newBaseObserver("A")
obB := newBaseObserver("B")
obC := newBaseObserver("C")
obD := newBaseObserver("D")
sBus := NewAsyncEventBus()
defer sBus.Stop()
topic := "async"
sBus.Subscribe(topic, obA)
sBus.Subscribe(topic, obB)
sBus.Subscribe(topic, obC)
sBus.Subscribe(topic, obD)
sBus.Publish(context.Background(), &Event{
Topic: topic,
Value: "hello redhat",
})
<-time.After(time.Second)
}