AlexStocks commented on code in PR #992:
URL: https://github.com/apache/dubbo-go-pixiu/pull/992#discussion_r3563622800
##########
pkg/client/mq/kafka_facade.go:
##########
@@ -160,6 +168,7 @@ func (f *KafkaConsumerFacade) checkConsumerIsAlive(ctx
context.Context, key stri
select {
case <-f.done:
ticker.Stop()
+ return
Review Comment:
[P0] 这里只让 `checkConsumerIsAlive` 在 `done` 关闭时退出,但 `Subscribe` 对 `wg` 执行了
`Add(2)`,`consumeLoop` 从未调用 `wg.Done()`。因此 `Stop()` 在关闭 `done` 后仍会永久阻塞在
`wg.Wait()`,取消订阅接口会被挂死。需要让消费循环在所有退出路径执行 `Done()`,并补充 `Stop()` 能返回的测试。
##########
pkg/client/mq/kafka_facade_test.go:
##########
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package mq
+
+import (
+ "sync"
+ "testing"
+ "time"
+)
+
+// TestKafkaConsumerFacadeConsumerManagerInitialized verifies that
+// the consumerManager map is initialized during construction,
+// preventing nil map panics during Subscribe operations.
+func TestKafkaConsumerFacadeConsumerManagerInitialized(t *testing.T) {
+ facade := &KafkaConsumerFacade{
+ consumerManager: make(map[string]func()),
+ done: make(chan struct{}),
+ mu: sync.RWMutex{},
+ }
+
+ testKey := "test-topic-test-group"
+
+ // This should NOT panic due to nil map write
+ defer func() {
+ if r := recover(); r != nil {
+ t.Fatalf("Subscribe panicked with nil map: %v", r)
+ }
+ }()
+
+ facade.mu.Lock()
+ facade.consumerManager[testKey] = func() {}
+ facade.mu.Unlock()
+
+ facade.mu.RLock()
+ _, exists := facade.consumerManager[testKey]
+ facade.mu.RUnlock()
+
+ if !exists {
+ t.Error("Expected key to exist in consumerManager")
+ }
+}
+
+// TestKafkaConsumerFacadeConcurrentAccess verifies that concurrent
+// access to consumerManager is safe due to mutex protection.
+func TestKafkaConsumerFacadeConcurrentAccess(t *testing.T) {
+ facade := &KafkaConsumerFacade{
+ consumerManager: make(map[string]func()),
+ done: make(chan struct{}),
+ mu: sync.RWMutex{},
+ }
+
+ done := make(chan bool)
+
+ // Writer goroutine
+ go func() {
+ for i := 0; i < 100; i++ {
+ facade.mu.Lock()
+
facade.consumerManager[GetConsumerManagerKey([]string{"topic"}, "group")] =
func() {}
+ facade.mu.Unlock()
+ }
+ done <- true
+ }()
+
+ // Reader goroutine
+ go func() {
+ for i := 0; i < 100; i++ {
+ facade.mu.RLock()
+ _ = len(facade.consumerManager)
+ facade.mu.RUnlock()
+ }
+ done <- true
+ }()
+
+ <-done
+ <-done
+}
+
+// TestGetConsumerManagerKey verifies the key generation function
+func TestGetConsumerManagerKey(t *testing.T) {
+ topics := []string{"topic1", "topic2"}
+ group := "test-group"
+
+ key := GetConsumerManagerKey(topics, group)
+
+ expectedKey := GetConsumerManagerKey(topics, group)
+ if key != expectedKey {
+ t.Error("Key should be deterministic")
+ }
+
+ differentTopics := []string{"topic3", "topic4"}
+ differentKey := GetConsumerManagerKey(differentTopics, group)
+ if key == differentKey {
+ t.Error("Different topics should produce different keys")
+ }
+
+ differentGroup := "other-group"
+ differentGroupKey := GetConsumerManagerKey(topics, differentGroup)
+ if key == differentGroupKey {
+ t.Error("Different groups should produce different keys")
+ }
+}
+
+// TestNewKafkaConsumerFacadeConstructorInitializesMap verifies that
+// the consumerManager map is initialized during construction via
NewKafkaConsumerFacade.
+func TestNewKafkaConsumerFacadeConstructorInitializesMap(t *testing.T) {
+ if testing.Short() {
+ t.Skip("Skipping test that requires Kafka broker in short mode")
+ }
+
+ config := KafkaConsumerConfig{
+ Brokers: []string{"localhost:9092"},
+ ClientID: "test-client",
+ ProtocolVersion: "2.8.0",
+ Metadata: Metadata{
+ Full: true,
+ Retry: MetadataRetry{
+ Max: 3,
+ Backoff: 250 * time.Millisecond,
+ },
+ },
+ }
+
+ facade, err := NewKafkaConsumerFacade(config, "test-group")
+ if err != nil {
+ t.Logf("Cannot connect to Kafka broker, skipping: %v", err)
Review Comment:
[P1] 这个测试在 Kafka 不可用时记录日志后直接成功返回,因此常规 CI 环境不会执行构造成功后的断言;前两个测试又是手工初始化
`consumerManager`,即使生产构造器再次漏掉初始化也全部会通过。请通过可注入的 consumer-group factory
或构造辅助函数,在不依赖真实 Kafka 的条件下验证 `NewKafkaConsumerFacade` 的初始化结果。
##########
pkg/client/mq/kafka_facade.go:
##########
@@ -186,10 +195,17 @@ func (f *KafkaConsumerFacade) checkConsumerIsAlive(ctx
context.Context, key stri
}
if lastCheck != http.StatusOK {
- f.consumerManager[key]()
- delete(f.consumerManager, key)
+ f.mu.Lock()
+ if cancel, ok := f.consumerManager[key]; ok {
+ cancel()
Review Comment:
[P0] 这里保存并调用的 `cancel` 属于局部子上下文 `c`,但 Kafka 消费循环启动时传入的是原始 `ctx`,只有健康检查协程使用了
`c`。健康检查失败后会删除管理项并停止检查协程,却不会停止 `consumerGroup.Consume` 循环,留下仍在消费且无法再通过 map 管理的
goroutine。应将同一个可取消上下文传给消费循环,或分别保存并统一取消消费与检查任务。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]