2021-07-10 23:41:21 +03:00
|
|
|
package kgo_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-09 00:30:36 +03:00
|
|
|
"fmt"
|
2021-07-10 23:41:21 +03:00
|
|
|
"os"
|
|
|
|
"strings"
|
2021-08-24 13:20:31 +03:00
|
|
|
"sync/atomic"
|
2021-07-10 23:41:21 +03:00
|
|
|
"testing"
|
2021-08-22 12:10:58 +03:00
|
|
|
"time"
|
2021-07-10 23:41:21 +03:00
|
|
|
|
2021-08-25 21:51:15 +03:00
|
|
|
kg "github.com/twmb/franz-go/pkg/kgo"
|
2021-07-10 23:41:21 +03:00
|
|
|
kgo "github.com/unistack-org/micro-broker-kgo/v3"
|
2021-08-22 12:10:58 +03:00
|
|
|
jsoncodec "github.com/unistack-org/micro-codec-json/v3"
|
2021-07-10 23:41:21 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2021-08-22 12:10:58 +03:00
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2021-07-10 23:41:21 +03:00
|
|
|
)
|
|
|
|
|
2021-08-25 21:51:15 +03:00
|
|
|
var bm = &broker.Message{
|
|
|
|
Header: map[string]string{"hkey": "hval", metadata.HeaderTopic: "test"},
|
|
|
|
Body: []byte(`"body"`),
|
|
|
|
}
|
2021-07-10 23:41:21 +03:00
|
|
|
|
|
|
|
func TestPubSub(t *testing.T) {
|
|
|
|
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2021-08-22 12:10:58 +03:00
|
|
|
logger.DefaultLogger.Init(logger.WithLevel(logger.TraceLevel), logger.WithCallerSkipCount(3))
|
2021-07-10 23:41:21 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var addrs []string
|
|
|
|
if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 {
|
2021-08-22 12:10:58 +03:00
|
|
|
addrs = []string{"127.0.0.1:29091", "127.0.0.2:29092", "127.0.0.3:29093"}
|
2021-07-10 23:41:21 +03:00
|
|
|
} else {
|
|
|
|
addrs = strings.Split(addr, ",")
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:20:31 +03:00
|
|
|
b := kgo.NewBroker(
|
|
|
|
broker.Codec(jsoncodec.NewCodec()),
|
|
|
|
broker.Addrs(addrs...),
|
|
|
|
kgo.ClientID("test"),
|
|
|
|
kgo.CommitInterval(1*time.Second),
|
2021-08-25 21:51:15 +03:00
|
|
|
kgo.Options(kg.FetchMaxBytes(10*1024)),
|
2021-08-24 13:20:31 +03:00
|
|
|
)
|
2021-07-10 23:41:21 +03:00
|
|
|
if err := b.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Connect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := b.Disconnect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2021-08-24 13:20:31 +03:00
|
|
|
_ = bm
|
2021-08-22 12:10:58 +03:00
|
|
|
/*
|
2021-08-24 13:20:31 +03:00
|
|
|
fmt.Printf("prefill")
|
2021-08-22 12:10:58 +03:00
|
|
|
|
2021-08-24 13:20:31 +03:00
|
|
|
msgs := make([]*broker.Message, 0, 600000)
|
|
|
|
for i := 0; i < 600000; i++ {
|
|
|
|
msgs = append(msgs, bm)
|
|
|
|
}
|
2021-08-22 12:10:58 +03:00
|
|
|
|
2021-08-24 13:20:31 +03:00
|
|
|
if err := b.BatchPublish(ctx, msgs); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Skip()
|
2021-08-22 12:10:58 +03:00
|
|
|
*/
|
2021-07-10 23:41:21 +03:00
|
|
|
done := make(chan bool, 1)
|
2021-08-24 13:20:31 +03:00
|
|
|
idx := int64(0)
|
2021-07-10 23:41:21 +03:00
|
|
|
fn := func(msg broker.Event) error {
|
2021-08-24 13:20:31 +03:00
|
|
|
atomic.AddInt64(&idx, 1)
|
2021-08-25 21:51:15 +03:00
|
|
|
// time.Sleep(200 * time.Millisecond)
|
2021-07-10 23:41:21 +03:00
|
|
|
return msg.Ack()
|
|
|
|
}
|
|
|
|
|
2021-08-25 21:51:15 +03:00
|
|
|
sub, err := b.Subscribe(ctx, "test", fn, broker.SubscribeAutoAck(true), broker.SubscribeGroup("test17"), broker.SubscribeBodyOnly(true))
|
2021-07-10 23:41:21 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sub.Unsubscribe(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2021-08-22 12:10:58 +03:00
|
|
|
|
|
|
|
for {
|
2021-08-24 13:20:31 +03:00
|
|
|
if v := atomic.LoadInt64(&idx); v == 12637303 {
|
|
|
|
close(done)
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
fmt.Printf("processed %v\n", v)
|
|
|
|
}
|
2021-08-22 12:10:58 +03:00
|
|
|
time.Sleep(1 * time.Second)
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
<-done
|
|
|
|
}
|