fix(go): require kafka write acknowledgements

This commit is contained in:
lingniu
2026-07-02 13:06:18 +08:00
parent ba5a28e636
commit 924903feb4
2 changed files with 28 additions and 0 deletions

View File

@@ -41,6 +41,8 @@ func NewKafkaSink(cfg KafkaConfig) (*KafkaSink, error) {
Addr: kafka.TCP(cfg.Brokers...),
Balancer: &kafka.Hash{},
AllowAutoTopicCreation: false,
RequiredAcks: kafka.RequireAll,
Async: false,
}, cfg), nil
}

View File

@@ -3,6 +3,7 @@ package eventbus
import (
"context"
"encoding/json"
"reflect"
"testing"
"github.com/segmentio/kafka-go"
@@ -63,6 +64,31 @@ func TestKafkaSinkRejectsUnknownProtocol(t *testing.T) {
}
}
func TestNewKafkaSinkUsesProductionDeliveryGuarantees(t *testing.T) {
sink, err := NewKafkaSink(KafkaConfig{Brokers: []string{"127.0.0.1:9092"}})
if err != nil {
t.Fatalf("NewKafkaSink() error = %v", err)
}
defer sink.Close()
writer, ok := sink.writer.(*kafka.Writer)
if !ok {
t.Fatalf("writer type = %s", reflect.TypeOf(sink.writer))
}
if writer.RequiredAcks != kafka.RequireAll {
t.Fatalf("RequiredAcks = %v, want %v", writer.RequiredAcks, kafka.RequireAll)
}
if writer.Async {
t.Fatal("writer must publish synchronously so Kafka errors propagate")
}
if writer.AllowAutoTopicCreation {
t.Fatal("writer must not auto-create production topics")
}
if _, ok := writer.Balancer.(*kafka.Hash); !ok {
t.Fatalf("balancer type = %s, want *kafka.Hash", reflect.TypeOf(writer.Balancer))
}
}
type recordingWriter struct {
messages []kafka.Message
}