2020-04-15 04:07:17 +03:00
|
|
|
package segmentio_test
|
|
|
|
|
|
|
|
import (
|
2021-01-19 15:53:47 +03:00
|
|
|
"context"
|
2020-04-15 04:07:17 +03:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
segmentio "github.com/unistack-org/micro-broker-segmentio/v3"
|
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-04-15 04:07:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
bm = &broker.Message{
|
|
|
|
Header: map[string]string{"hkey": "hval"},
|
2021-07-08 14:11:14 +03:00
|
|
|
Body: []byte(`"body"`),
|
2020-04-15 04:07:17 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPubSub(t *testing.T) {
|
2021-01-19 15:53:47 +03:00
|
|
|
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
2020-04-15 04:07:17 +03:00
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:28:13 +03:00
|
|
|
if err := logger.DefaultLogger.Init(logger.WithLevel(logger.TraceLevel)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-08 14:11:14 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
var addrs []string
|
|
|
|
if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 {
|
|
|
|
addrs = []string{"127.0.0.1:9092"}
|
|
|
|
} else {
|
|
|
|
addrs = strings.Split(addr, ",")
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:28:13 +03:00
|
|
|
b := segmentio.NewBroker(broker.Addrs(addrs...), segmentio.ClientID("test"))
|
2021-01-19 15:53:47 +03:00
|
|
|
if err := b.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Connect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
2020-04-15 04:07:17 +03:00
|
|
|
}
|
|
|
|
defer func() {
|
2021-01-19 15:53:47 +03:00
|
|
|
if err := b.Disconnect(ctx); err != nil {
|
2020-04-15 04:07:17 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
done := make(chan bool, 1)
|
|
|
|
fn := func(msg broker.Event) error {
|
|
|
|
done <- true
|
|
|
|
return msg.Ack()
|
|
|
|
}
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
sub, err := b.Subscribe(ctx, "test_topic", fn, broker.SubscribeGroup("test"))
|
2020-04-15 04:07:17 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2021-01-19 15:53:47 +03:00
|
|
|
if err := sub.Unsubscribe(ctx); err != nil {
|
2020-04-15 04:07:17 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2021-01-19 15:53:47 +03:00
|
|
|
if err := b.Publish(ctx, "test_topic", bm); err != nil {
|
2020-04-15 04:07:17 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
<-done
|
|
|
|
}
|