优化结构

This commit is contained in:
Yvan
2026-08-30 01:08:10 +08:00
parent 509df03d61
commit e0c35e4e9b
23 changed files with 1513 additions and 38 deletions
+10 -1
View File
@@ -279,7 +279,13 @@ func configStrings(values map[string]any, key string) []string {
}
stringsValue, ok := values[key].([]string)
if ok {
return append([]string(nil), stringsValue...)
result := make([]string, 0, len(stringsValue))
for _, item := range stringsValue {
if value := strings.TrimSpace(item); value != "" {
result = append(result, value)
}
}
return result
}
return nil
}
@@ -327,6 +333,9 @@ func (r *Reloadable) retryOnce() {
client := r.clientLocked(provider)
if client != nil && client.Connected() {
if r.pending[provider] {
if retryAt := r.nextRetry[provider]; !retryAt.IsZero() && now.Before(retryAt) {
continue
}
if err := r.reconcileProviderLocked(provider); err != nil {
r.scheduleRetryLocked(provider, config.Values)
}
+67
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"log/slog"
"strings"
"testing"
"time"
@@ -209,3 +210,69 @@ func TestReloadableLegacySubscribeDoesNotLeaveOfflineDeclaration(t *testing.T) {
t.Fatalf("offline legacy declaration remained: %#v", r.subscriptions[ProviderEMQX])
}
}
func TestReloadableExposesKafkaNamedClient(t *testing.T) {
r := &Reloadable{}
client := r.Client(" KAFKA ")
if client == nil {
t.Fatal("kafka named client is nil")
}
if client.Connected() {
t.Fatal("unconfigured kafka named client reported connected")
}
}
func TestConfigStringsDecodesJSONArrays(t *testing.T) {
values := map[string]any{"brokers": []any{" kafka-1:9092 ", "", "kafka-2:9092"}}
brokers := configStrings(values, "brokers")
if len(brokers) != 2 || brokers[0] != "kafka-1:9092" || brokers[1] != "kafka-2:9092" {
t.Fatalf("brokers = %#v", brokers)
}
}
func TestConfigStringsNormalizesTypedSlices(t *testing.T) {
values := map[string]any{"brokers": []string{" kafka-1:9092 ", "", "kafka-2:9092"}}
brokers := configStrings(values, "brokers")
if len(brokers) != 2 || brokers[0] != "kafka-1:9092" || brokers[1] != "kafka-2:9092" {
t.Fatalf("brokers = %#v", brokers)
}
}
func TestReloadableRejectsUnsupportedKafkaQoS(t *testing.T) {
r := &Reloadable{}
err := r.Register(platformmq.SubscriptionSet{
Owner: "orders",
Provider: ProviderKafka,
Topics: []platformmq.TopicSubscription{{Topic: "orders.created", QoS: platformmq.ExactlyOnce, Handler: func(context.Context, platformmq.Message) {}}},
})
if err == nil || !strings.Contains(err.Error(), "supports qos 0 or 1") {
t.Fatalf("Register() error = %v", err)
}
}
func TestRetryOnceHonorsPendingSubscriptionDeadline(t *testing.T) {
client := &fakeClient{}
r := &Reloadable{
clients: map[string]platformmq.Client{ProviderKafka: client},
configs: map[string]runtimeconfig.Config{
ProviderKafka: {Kind: "mq", Provider: ProviderKafka, Enabled: true},
},
subscriptions: map[string]map[string]map[string]subscription{
ProviderKafka: {
"orders.created": {"orders": {qos: platformmq.AtLeastOnce, handler: func(context.Context, platformmq.Message) {}}},
},
},
bindings: map[string]map[string]byte{ProviderKafka: {}},
pending: map[string]bool{ProviderKafka: true},
nextRetry: map[string]time.Time{ProviderKafka: time.Now().Add(time.Minute)},
}
r.retryOnce()
if len(client.subscribed) != 0 {
t.Fatalf("subscription retried before deadline: %v", client.subscribed)
}
r.nextRetry[ProviderKafka] = time.Now().Add(-time.Second)
r.retryOnce()
if len(client.subscribed) != 1 || client.subscribed[0] != "orders.created" {
t.Fatalf("subscription was not retried after deadline: %v", client.subscribed)
}
}