package queue import ( "context" "errors" "testing" "time" "github.com/IBM/sarama" "go.uber.org/zap" ) // mockSession ConsumerGroupSession 的轻量 mock。 type mockSession struct { ctx context.Context marked []*sarama.ConsumerMessage commits int } func (s *mockSession) Claims() map[string][]int32 { return nil } func (s *mockSession) MemberID() string { return "mock" } func (s *mockSession) GenerationID() int32 { return 1 } func (s *mockSession) MarkOffset(string, int32, int64, string) {} func (s *mockSession) ResetOffset(string, int32, int64, string) {} func (s *mockSession) MarkMessage(m *sarama.ConsumerMessage, _ string) { s.marked = append(s.marked, m) } func (s *mockSession) Context() context.Context { return s.ctx } func (s *mockSession) Commit() { s.commits++ } // mockClaim ConsumerGroupClaim 的轻量 mock。 type mockClaim struct { topic string partition int32 msgs chan *sarama.ConsumerMessage } func (c *mockClaim) Topic() string { return c.topic } func (c *mockClaim) Partition() int32 { return c.partition } func (c *mockClaim) InitialOffset() int64 { return 0 } func (c *mockClaim) HighWaterMarkOffset() int64 { return 0 } func (c *mockClaim) Messages() <-chan *sarama.ConsumerMessage { return c.msgs } // runHandler 在 mock session/claim 上执行 ConsumeClaim 并等待结束。 func runHandler(t *testing.T, cfg ConsumerConfig, msgs []*sarama.ConsumerMessage) *mockSession { t.Helper() sess := &mockSession{ctx: context.Background()} claim := &mockClaim{topic: "it", partition: 0, msgs: make(chan *sarama.ConsumerMessage, len(msgs))} for _, m := range msgs { claim.msgs <- m } close(claim.msgs) // 关闭后 ConsumeClaim 自然返回 h := &groupHandler{cfg: cfg, log: zap.NewNop()} done := make(chan struct{}) go func() { defer close(done); _ = h.ConsumeClaim(sess, claim) }() select { case <-done: case <-time.After(2 * time.Second): t.Fatal("ConsumeClaim 未退出") } return sess } // TestConsumeClaim_MarkAndCommit 验证:成功才 Mark,按 N 条提交。 func TestConsumeClaim_MarkAndCommit(t *testing.T) { failOn := "bad" cfg := ConsumerConfig{ Handler: func(_ context.Context, m *sarama.ConsumerMessage) error { if string(m.Value) == failOn { return errors.New("biz error") } return nil }, CommitEveryN: 2, } msgs := []*sarama.ConsumerMessage{ {Topic: "it", Value: []byte("ok1")}, {Topic: "it", Value: []byte(failOn)}, // 失败:不 Mark 不计入提交 {Topic: "it", Value: []byte("ok2")}, // 凑满 2 条 → 第 1 次提交 {Topic: "it", Value: []byte("ok3")}, {Topic: "it", Value: []byte("ok4")}, // 凑满 2 条 → 第 2 次提交 } sess := runHandler(t, cfg, msgs) if len(sess.marked) != 4 { t.Errorf("Mark 数 = %d, 期望 4(失败消息不应 Mark)", len(sess.marked)) } if sess.commits != 2 { t.Errorf("Commit 次数 = %d, 期望 2", sess.commits) } } // TestConsumeClaim_AllFail 验证全部失败时零提交(at-least-once 重投)。 func TestConsumeClaim_AllFail(t *testing.T) { cfg := ConsumerConfig{ Handler: func(_ context.Context, _ *sarama.ConsumerMessage) error { return errors.New("always") }, CommitEveryN: 1, } sess := runHandler(t, cfg, []*sarama.ConsumerMessage{ {Topic: "it", Value: []byte("a")}, {Topic: "it", Value: []byte("b")}, }) if len(sess.marked) != 0 || sess.commits != 0 { t.Errorf("全部失败不应有 Mark/Commit, 实际 %d/%d", len(sess.marked), sess.commits) } } // TestNewConsumer_ConfigValidation 验证配置校验。 func TestNewConsumer_ConfigValidation(t *testing.T) { h := func(context.Context, *sarama.ConsumerMessage) error { return nil } cases := []ConsumerConfig{ {GroupID: "g", Topics: []string{"t"}, Handler: h}, // 无 brokers {Brokers: []string{"b"}, Topics: []string{"t"}, Handler: h}, // 无 group {Brokers: []string{"b"}, GroupID: "g", Handler: h}, // 无 topics {Brokers: []string{"b"}, GroupID: "g", Topics: []string{"t"}}, // 无 handler } for i, cfg := range cases { if _, err := NewConsumer(cfg); err == nil { t.Errorf("用例 %d 应返回配置错误", i) } } }