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-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...),
|
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-08-22 12:10:58 +03:00
|
|
|
|
2021-08-27 02:24:43 +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-27 02:24:43 +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-26 00:36:32 +03:00
|
|
|
sub, err := b.Subscribe(ctx, "test", fn,
|
|
|
|
broker.SubscribeAutoAck(true),
|
2021-08-27 02:24:43 +03:00
|
|
|
broker.SubscribeGroup("test29"),
|
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:
|
|
|
|
fmt.Printf("processed %v\n", atomic.LoadInt64(&idx))
|
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
|
|
|
|
}
|