2021-07-10 23:41:21 +03:00
|
|
|
package kgo_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-27 02:24:43 +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-10-03 18:30:56 +03:00
|
|
|
kgo "go.unistack.org/micro-broker-kgo/v3"
|
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2021-07-10 23:41:21 +03:00
|
|
|
)
|
|
|
|
|
2021-09-03 18:48:32 +03:00
|
|
|
var (
|
2021-09-15 18:49:50 +03:00
|
|
|
msgcnt = int64(12000000)
|
|
|
|
group = "38"
|
|
|
|
prefill = false
|
|
|
|
loglevel = logger.InfoLevel
|
2021-09-03 18:48:32 +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
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func TestConnect(t *testing.T) {
|
|
|
|
var addrs []string
|
|
|
|
ctx := context.TODO()
|
|
|
|
b := kgo.NewBroker(
|
|
|
|
broker.Addrs(addrs...),
|
|
|
|
kgo.CommitInterval(5*time.Second),
|
|
|
|
kgo.Options(kg.ClientID("test"), kg.FetchMaxBytes(10*1024*1024)),
|
|
|
|
)
|
|
|
|
if err := b.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Connect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
func TestPubSub(t *testing.T) {
|
|
|
|
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2024-12-19 13:21:32 +03:00
|
|
|
if err := logger.DefaultLogger.Init(logger.WithLevel(loglevel), logger.WithAddCallerSkipCount(1)); err != nil {
|
2021-09-15 18:30:07 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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.Addrs(addrs...),
|
2021-08-26 00:36:32 +03:00
|
|
|
kgo.CommitInterval(5*time.Second),
|
|
|
|
kgo.Options(kg.ClientID("test"), kg.FetchMaxBytes(10*1024*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-09-03 18:48:32 +03:00
|
|
|
if prefill {
|
|
|
|
msgs := make([]*broker.Message, 0, msgcnt)
|
|
|
|
for i := int64(0); i < msgcnt; i++ {
|
|
|
|
msgs = append(msgs, bm)
|
|
|
|
}
|
2021-08-22 12:10:58 +03:00
|
|
|
|
2021-09-03 18:48:32 +03:00
|
|
|
if err := b.BatchPublish(ctx, msgs); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// t.Skip()
|
|
|
|
}
|
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-26 00:36:32 +03:00
|
|
|
sub, err := b.Subscribe(ctx, "test", fn,
|
|
|
|
broker.SubscribeAutoAck(true),
|
2021-09-03 18:48:32 +03:00
|
|
|
broker.SubscribeGroup(group),
|
2021-08-26 00:36:32 +03:00
|
|
|
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
|
|
|
|
2021-08-26 00:36:32 +03:00
|
|
|
ticker := time.NewTicker(2 * time.Minute)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2021-08-27 02:24:43 +03:00
|
|
|
pticker := time.NewTicker(1 * time.Second)
|
|
|
|
defer pticker.Stop()
|
2021-08-26 00:36:32 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2021-08-27 02:24:43 +03:00
|
|
|
case <-pticker.C:
|
2021-09-03 18:48:32 +03:00
|
|
|
if prc := atomic.LoadInt64(&idx); prc == msgcnt {
|
|
|
|
close(done)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("processed %v\n", prc)
|
|
|
|
}
|
2021-08-26 00:36:32 +03:00
|
|
|
case <-ticker.C:
|
|
|
|
close(done)
|
|
|
|
}
|
2021-08-24 13:20:31 +03:00
|
|
|
}
|
2021-08-26 00:36:32 +03:00
|
|
|
}()
|
2021-07-10 23:41:21 +03:00
|
|
|
<-done
|
|
|
|
}
|