优化结构

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
+51 -3
View File
@@ -16,7 +16,7 @@ func TestNormalizeSubscriptionSetValidDeclaration(t *testing.T) {
Topics: []TopicSubscription{
{Topic: " orders.created ", QoS: AtMostOnce, Handler: handler},
{Topic: "orders.updated", QoS: AtLeastOnce, Handler: handler},
{Topic: "orders.deleted", QoS: ExactlyOnce, Handler: handler},
{Topic: "orders.deleted", QoS: AtLeastOnce, Handler: handler},
},
}
@@ -31,7 +31,7 @@ func TestNormalizeSubscriptionSetValidDeclaration(t *testing.T) {
Topics: []TopicSubscription{
{Topic: "orders.created", QoS: AtMostOnce, Handler: handler},
{Topic: "orders.updated", QoS: AtLeastOnce, Handler: handler},
{Topic: "orders.deleted", QoS: ExactlyOnce, Handler: handler},
{Topic: "orders.deleted", QoS: AtLeastOnce, Handler: handler},
},
}
if got.Owner != want.Owner || got.Provider != want.Provider {
@@ -53,6 +53,34 @@ func TestNormalizeSubscriptionSetValidDeclaration(t *testing.T) {
}
}
func TestNormalizeSubscriptionSetAcceptsMQTTQoS2(t *testing.T) {
set, err := NormalizeSubscriptionSet(SubscriptionSet{
Owner: "devices",
Provider: ProviderEMQX,
Topics: []TopicSubscription{{Topic: "devices/status", QoS: ExactlyOnce, Handler: func(context.Context, Message) {}}},
})
if err != nil {
t.Fatal(err)
}
if set.Topics[0].QoS != ExactlyOnce {
t.Fatalf("qos = %d, want %d", set.Topics[0].QoS, ExactlyOnce)
}
}
func TestNormalizeSubscriptionSetAcceptsKafka(t *testing.T) {
set, err := NormalizeSubscriptionSet(SubscriptionSet{
Owner: "orders",
Provider: " KAFKA ",
Topics: []TopicSubscription{{Topic: "orders.created", QoS: AtLeastOnce, Handler: func(context.Context, Message) {}}},
})
if err != nil {
t.Fatal(err)
}
if set.Provider != ProviderKafka {
t.Fatalf("provider = %q, want %q", set.Provider, ProviderKafka)
}
}
func TestNormalizeSubscriptionSetRejectsInvalidDeclarations(t *testing.T) {
handler := func(context.Context, Message) {}
base := func() SubscriptionSet {
@@ -82,7 +110,7 @@ func TestNormalizeSubscriptionSetRejectsInvalidDeclarations(t *testing.T) {
},
{
name: "unsupported provider",
set: func() SubscriptionSet { set := base(); set.Provider = "kafka"; return set }(),
set: func() SubscriptionSet { set := base(); set.Provider = "pulsar"; return set }(),
wantErr: "unsupported mq provider",
},
{
@@ -128,6 +156,26 @@ func TestNormalizeSubscriptionSetRejectsInvalidDeclarations(t *testing.T) {
}(),
wantErr: "invalid mq qos",
},
{
name: "kafka qos 2",
set: func() SubscriptionSet {
set := base()
set.Provider = ProviderKafka
set.Topics[0].QoS = ExactlyOnce
return set
}(),
wantErr: "supports qos 0 or 1",
},
{
name: "rabbitmq qos 2",
set: func() SubscriptionSet {
set := base()
set.Provider = ProviderRabbitMQ
set.Topics[0].QoS = ExactlyOnce
return set
}(),
wantErr: "supports qos 0 or 1",
},
}
for _, test := range tests {