126 lines
3.2 KiB
Go
126 lines
3.2 KiB
Go
//go:build integration
|
|
|
|
package integration
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/IBM/sarama"
|
|
"go.uber.org/zap"
|
|
|
|
"go-sms/internal/pkg/queue"
|
|
)
|
|
|
|
func kafkaAddr() string {
|
|
if a := os.Getenv("GOSMS_TEST_KAFKA_ADDR"); a != "" {
|
|
return a
|
|
}
|
|
return "127.0.0.1:19092"
|
|
}
|
|
|
|
// TestKafka_ProduceConsume 开发环境验收(Day 7):
|
|
// 异步生产者发送成功 → 消费者组接收并处理 → 手动提交 offset,无 ErrOutOfBrokers。
|
|
func TestKafka_ProduceConsume(t *testing.T) {
|
|
addr := kafkaAddr()
|
|
// 每次运行使用独立主题与消费组,保证 OffsetOldest 全量可重现
|
|
topic := fmt.Sprintf("go-sms-it-queue-%d", time.Now().UnixNano())
|
|
group := topic + "-group"
|
|
|
|
var mu sync.Mutex
|
|
received := make(map[string]string) // key -> value
|
|
gotAll := make(chan struct{})
|
|
ready := make(chan struct{}) // 消费组就绪信号(收到探测消息)
|
|
const total = 5
|
|
const readyKey = "__ready__"
|
|
|
|
cons, err := queue.NewConsumer(queue.ConsumerConfig{
|
|
Brokers: []string{addr},
|
|
GroupID: group,
|
|
Topics: []string{topic},
|
|
Oldest: true,
|
|
CommitEveryN: 1, // 每条手动提交
|
|
Logger: zap.NewExample(),
|
|
Handler: func(_ context.Context, msg *sarama.ConsumerMessage) error {
|
|
if string(msg.Key) == readyKey { // 探测消息:组已完成分配
|
|
select {
|
|
case <-ready:
|
|
default:
|
|
close(ready)
|
|
}
|
|
return nil
|
|
}
|
|
mu.Lock()
|
|
received[string(msg.Key)] = string(msg.Value)
|
|
n := len(received)
|
|
mu.Unlock()
|
|
if n == total {
|
|
select {
|
|
case <-gotAll:
|
|
default:
|
|
close(gotAll)
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("创建消费者失败(检查 make up 与 %s): %v", addr, err)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go func() { _ = cons.Run(ctx) }()
|
|
defer cons.Close()
|
|
|
|
// 探测消息确认消费组完成首次分配
|
|
// (冷启动集群首个消费组需等待 __consumer_offsets 初始化,固定 sleep 不可靠)
|
|
prod, err := queue.NewProducer(queue.ProducerConfig{
|
|
Brokers: []string{addr},
|
|
Logger: zap.NewExample(),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("创建生产者失败: %v", err)
|
|
}
|
|
defer prod.Close()
|
|
|
|
if err := prod.Send(topic, []byte(readyKey), []byte("ping")); err != nil {
|
|
t.Fatalf("发送探测消息失败: %v", err)
|
|
}
|
|
select {
|
|
case <-ready:
|
|
t.Log("消费组已就绪(探测消息已收到)")
|
|
case <-time.After(30 * time.Second):
|
|
t.Fatal("消费组 30s 内未完成首次分配")
|
|
}
|
|
|
|
for i := 0; i < total; i++ {
|
|
key := fmt.Sprintf("138%08d", i)
|
|
val := fmt.Sprintf(`{"mobile":"%s","content":"Day10 验收消息 %d"}`, key, i)
|
|
if err := prod.Send(topic, []byte(key), []byte(val)); err != nil {
|
|
t.Fatalf("发送第 %d 条失败: %v", i, err)
|
|
}
|
|
}
|
|
t.Logf("生产者已投递 %d 条消息到 %s(无 ErrOutOfBrokers)", total, topic)
|
|
|
|
select {
|
|
case <-gotAll:
|
|
case <-time.After(20 * time.Second):
|
|
mu.Lock()
|
|
t.Fatalf("消费者超时未收齐(收到 %d/%d 条)", len(received), total)
|
|
}
|
|
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
for i := 0; i < total; i++ {
|
|
key := fmt.Sprintf("138%08d", i)
|
|
if received[key] == "" {
|
|
t.Errorf("未收到 key=%s 的消息", key)
|
|
}
|
|
}
|
|
t.Logf("消费者组 %s 接收并处理全部 %d 条消息,offset 手动提交成功", group, total)
|
|
}
|