Files
VLoop/backend/internal/middleware/rabbitmq/constants_test.go
huanghaosheng 4e412a2c84 feat(mq): 声明停泊链路与各队列独立的重试参数
为"处理失败的消息有去处"准备 broker 侧契约,本提交只做声明,不改变消费行为。

停泊链路(declareParking):DLX 交换机 dlx.events + 唯一停泊队列 failed.events
+ parked.# 绑定。形状与旧的 <queue>.dlx 相反——旧实现每个业务队列建一个
<queue>.dlx 且都以 "#" 绑定同一个交换机,一旦真的产生死信,topic 会把它扇出成
N 份;停泊队列只有一个、绑定一次,一条消息只会落一份。

重试参数(RetryArgs / RetryQueueArgs):x-dead-letter-exchange 指向本队列专属的
retry.events.<Q>,重试队列 <Q>.retry 用队列级 TTL 延迟后经默认交换机按队列名
精确回源。每队列独立是必需的:like.events 与 notification.like 共享交换机且
路由键重叠,共用重试入口会把消息重投到错误的队列。

RetryArgs 刻意不设 x-dead-letter-routing-key,使第 1 次被拒绝时保留原路由键;
从第 2 次起路由键是队列名(回源那步写死),这一点由 RetryArgs 的注释和
TestRetryArgs_OnlyOverridesDeadLetterExchange 共同钉住。

DeclareTopologies 先声明停泊链路再声明业务拓扑,且停泊链路声明失败会终止整轮
重放(而不是仅告警):业务队列的 DLX 指向一个不存在的交换机时,broker 会丢弃
被拒绝的消息,而 worker 以为已经处理完。既有顺序测试相应改为先断言停泊前缀。

同时补上 ParkedInfo/ParseParkedInfo/EventIDOf 与新的线缆名常量,
新增 constants_test.go 的钉值断言。
2026-10-30 13:12:38 +08:00

64 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package rabbitmq
import "testing"
// TestTopologyConstants_WireNamesAreStable 锁定 broker 侧的"线缆名称"。
//
// 这些字符串是运行中集群里的持久实体名:改名等于新开一套交换机/队列,
// 旧队列里的消息与消费者会静默失联。导出给 cmd/worker 与 http 层复用之后,
// 重命名变得更容易,所以用一条测试把值钉住——改名必须是一次有意的、
// 带迁移的动作,而不是顺手"整理命名"。
func TestTopologyConstants_WireNamesAreStable(t *testing.T) {
cases := []struct {
name string
got string
want string
}{
{"LikeExchange", LikeExchange, "like.events"},
{"LikeQueue", LikeQueue, "like.events"},
{"LikeBindingKey", LikeBindingKey, "like.*"},
{"LikeLikeRK", LikeLikeRK, "like.like"},
{"LikeUnlikeRK", LikeUnlikeRK, "like.unlike"},
{"CommentExchange", CommentExchange, "comment.events"},
{"CommentQueue", CommentQueue, "comment.events"},
{"CommentBindingKey", CommentBindingKey, "comment.*"},
{"CommentPublishRK", CommentPublishRK, "comment.publish"},
{"CommentDeleteRK", CommentDeleteRK, "comment.delete"},
{"SocialExchange", SocialExchange, "social.events"},
{"SocialQueue", SocialQueue, "social.events"},
{"SocialBindingKey", SocialBindingKey, "social.*"},
{"SocialFollowRK", SocialFollowRK, "social.follow"},
{"SocialUnfollowRK", SocialUnfollowRK, "social.unfollow"},
{"PopularityExchange", PopularityExchange, "video.popularity.events"},
{"PopularityQueue", PopularityQueue, "video.popularity.events"},
{"PopularityBindingKey", PopularityBindingKey, "video.popularity.*"},
{"PopularityUpdateRK", PopularityUpdateRK, "video.popularity.update"},
{"TimelineExchange", TimelineExchange, "video.timeline.events"},
{"TimelineQueue", TimelineQueue, "video.timeline.update.queue"},
{"TimelineBindingKey", TimelineBindingKey, "video.timeline.*"},
{"TimelinePublishRK", TimelinePublishRK, "video.timeline.publish"},
{"NotificationLikeQueue", NotificationLikeQueue, "notification.like"},
{"NotificationCommentQueue", NotificationCommentQueue, "notification.comment"},
{"NotificationSocialQueue", NotificationSocialQueue, "notification.social"},
// 停泊链路(批次 0)。failed.events 是运维真正会去查的队列名,
// dlx.events 沿用了改造前就存在的名字,因此改名同样等同于一次迁移。
{"DLXExchange", DLXExchange, "dlx.events"},
{"ParkedQueue", ParkedQueue, "failed.events"},
{"ParkedKeyPrefix", ParkedKeyPrefix, "parked."},
// 重试链路(批次 2 启用,但名字属于线缆契约,与常量同批锁定)。
{"RetryExchangePrefix", RetryExchangePrefix, "retry.events."},
{"RetryQueueSuffix", RetryQueueSuffix, ".retry"},
}
for _, c := range cases {
if c.got != c.want {
t.Errorf("%s = %q, want %q(broker 线缆契约被改动,需配套迁移)", c.name, c.got, c.want)
}
}
}