micro/broker/memory/memory_test.go

51 lines
924 B
Go
Raw Normal View History

2019-01-14 18:27:25 +03:00
package memory
2016-03-18 01:46:20 +03:00
import (
"fmt"
"testing"
"github.com/micro/go-micro/v2/broker"
2016-03-18 01:46:20 +03:00
)
2019-01-14 18:27:25 +03:00
func TestMemoryBroker(t *testing.T) {
2016-03-18 01:46:20 +03:00
b := NewBroker()
if err := b.Connect(); err != nil {
2016-04-06 20:03:27 +03:00
t.Fatalf("Unexpected connect error %v", err)
2016-03-18 01:46:20 +03:00
}
topic := "test"
count := 10
2019-07-07 14:44:09 +03:00
fn := func(p broker.Event) error {
2016-03-18 01:46:20 +03:00
return nil
}
sub, err := b.Subscribe(topic, fn)
if err != nil {
t.Fatalf("Unexpected error subscribing %v", err)
}
for i := 0; i < count; i++ {
message := &broker.Message{
Header: map[string]string{
"foo": "bar",
"id": fmt.Sprintf("%d", i),
},
Body: []byte(`hello world`),
}
if err := b.Publish(topic, message); err != nil {
t.Fatalf("Unexpected error publishing %d", i)
}
}
if err := sub.Unsubscribe(); err != nil {
t.Fatalf("Unexpected error unsubscribing from %s: %v", topic, err)
}
if err := b.Disconnect(); err != nil {
t.Fatalf("Unexpected connect error %v", err)
}
}