micro/broker/http_broker_test.go

56 lines
1.1 KiB
Go
Raw Normal View History

2016-03-14 19:37:51 +00:00
package broker
import (
"testing"
"github.com/micro/go-micro/registry/mock"
)
func TestBroker(t *testing.T) {
m := mock.NewRegistry()
2016-03-15 22:12:28 +00:00
b := NewBroker(Registry(m))
2016-03-14 19:37:51 +00:00
if err := b.Init(); err != nil {
t.Errorf("Unexpected init error: %v", err)
}
if err := b.Connect(); err != nil {
t.Errorf("Unexpected connect error: %v", err)
}
msg := &Message{
Header: map[string]string{
"Content-Type": "application/json",
},
Body: []byte(`{"message": "Hello World"}`),
}
done := make(chan bool)
sub, err := b.Subscribe("test", func(p Publication) error {
m := p.Message()
t.Logf("Received message %+v", m)
if string(m.Body) != string(msg.Body) {
t.Errorf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
}
close(done)
return nil
})
if err != nil {
t.Errorf("Unexpected subscribe error: %v", err)
}
if err := b.Publish("test", msg); err != nil {
t.Errorf("Unexpected publish error: %v", err)
}
<-done
sub.Unsubscribe()
if err := b.Disconnect(); err != nil {
t.Errorf("Unexpected disconnect error: %v", err)
}
}