refactor tests && add test for tombstone && fix concurent read\write map
Some checks failed
test / test (pull_request) Failing after 17m56s
lint / lint (pull_request) Failing after 18m10s
coverage / build (pull_request) Failing after 18m16s

This commit is contained in:
Evstigneev Denis
2026-01-15 11:46:54 +03:00
parent f8d9e0584f
commit 3759fa2f7c
5 changed files with 273 additions and 11 deletions

109
tombstone_test.go Normal file
View File

@@ -0,0 +1,109 @@
package kgo_test
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
kg "github.com/twmb/franz-go/pkg/kgo"
"go.unistack.org/micro/v3"
"go.unistack.org/micro/v3/codec"
"go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/server"
)
func TestSubscriberHandlesTombstoneMessages(t *testing.T) {
ctx := context.Background()
err := logger.DefaultLogger.Init(logger.WithLevel(loglevel))
require.Nil(t, err)
cl := cluster
_ = cl
b := helperCreateBroker(t)
require.Nil(t, b.Init())
require.Nil(t, b.Connect(ctx))
defer func() {
require.Nil(t, b.Disconnect(ctx))
}()
svc := helperCreateService(t, ctx, b)
require.Nil(t, svc.Init())
var (
msgsPublished = 1000
done = make(chan bool, 1)
counterMsgs = atomic.Int64{}
topicName = "test.tombstone.topic"
// INCORRECT: func(ctx context.Context, req any) error
fnHandler = func(ctx context.Context, req *codec.Frame) error {
counterMsgs.Add(1)
return nil
}
)
err = micro.RegisterSubscriber(
topicName,
svc.Server(),
fnHandler,
server.SubscriberQueue("queue"),
server.SubscriberAck(true),
server.SubscriberBodyOnly(true),
)
require.Nil(t, err)
{ // PUBLISH tombstones via franz-go client
client, err := kg.NewClient(
kg.SeedBrokers(cluster.ListenAddrs()...),
kg.AllowAutoTopicCreation(),
)
require.Nil(t, err)
defer client.Close()
var records []*kg.Record
for i := 0; i < msgsPublished; i++ {
records = append(records, &kg.Record{
Topic: topicName,
Key: []byte("tombstone-key"),
})
}
results := client.ProduceSync(ctx, records...)
for _, r := range results {
require.Nil(t, r.Err)
}
}
require.Nil(t, svc.Start())
go func() {
require.Nil(t, svc.Run())
}()
ticker := time.NewTicker(2 * time.Minute)
defer ticker.Stop()
pticker := time.NewTicker(1 * time.Second)
defer pticker.Stop()
go func() {
for {
select {
case <-pticker.C:
if prc := counterMsgs.Load(); prc == int64(msgsPublished) {
t.Log("everything is read:", prc)
close(done)
} else {
t.Logf("processed %v of %v\n", prc, msgsPublished)
}
case <-ticker.C:
close(done)
}
}
}()
<-done
require.Nil(t, svc.Stop())
}