172 lines
5.8 KiB
Go
172 lines
5.8 KiB
Go
|
|
package rabbitmq
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"errors"
|
|||
|
|
"reflect"
|
|||
|
|
"strings"
|
|||
|
|
"testing"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// —— 停泊链路 ——
|
|||
|
|
|
|||
|
|
// TestDeclareTopologies_DeclaresParkingBeforeBusinessTopology 守护"停泊链路先于业务拓扑"。
|
|||
|
|
//
|
|||
|
|
// 为什么顺序重要:停泊是业务队列的 DLX 目的地。链路晚一步,就有一段时间窗口
|
|||
|
|
// 里被拒绝的消息无处可去——broker 直接丢弃,而 worker 以为自己已经处理完了。
|
|||
|
|
func TestDeclareTopologies_DeclaresParkingBeforeBusinessTopology(t *testing.T) {
|
|||
|
|
f := &fakeDeclarer{}
|
|||
|
|
topo := []Topology{{Exchange: LikeExchange, Queue: LikeQueue, BindingKey: LikeBindingKey}}
|
|||
|
|
|
|||
|
|
if err := DeclareTopologies(f, topo); err != nil {
|
|||
|
|
t.Fatalf("DeclareTopologies: %v", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
want := []string{
|
|||
|
|
"exchange:" + DLXExchange,
|
|||
|
|
"queue:" + ParkedQueue,
|
|||
|
|
"bind:" + ParkedQueue + "<-" + DLXExchange + "/" + ParkedKeyPrefix + "#",
|
|||
|
|
}
|
|||
|
|
if len(f.calls) < len(want) {
|
|||
|
|
t.Fatalf("calls = %v, want at least %v", f.calls, want)
|
|||
|
|
}
|
|||
|
|
if !reflect.DeepEqual(f.calls[:len(want)], want) {
|
|||
|
|
t.Fatalf("前三个声明 = %v, want %v", f.calls[:len(want)], want)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
parked := f.indexOf("queue:" + ParkedQueue)
|
|||
|
|
// 老实现每队列还会声明一个 <queue>.dlx(批次 0 保留不动),
|
|||
|
|
// 这里只断言"停泊链路早于业务队列",不锁死批次 2 之后的对象集合。
|
|||
|
|
business := f.indexOf("queue:" + LikeQueue)
|
|||
|
|
if parked < 0 || business < 0 || parked > business {
|
|||
|
|
t.Fatalf("停泊队列下标=%d 业务队列下标=%d,停泊链路必须先声明", parked, business)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestDeclareTopologies_ParkingFailureStopsReplay(t *testing.T) {
|
|||
|
|
f := &fakeDeclarer{failOn: "exchange:" + DLXExchange, failErr: errors.New("ACCESS_REFUSED")}
|
|||
|
|
err := DeclareTopologies(f, []Topology{{Exchange: LikeExchange, Queue: LikeQueue, BindingKey: LikeBindingKey}})
|
|||
|
|
if err == nil {
|
|||
|
|
t.Fatal("期望返回错误")
|
|||
|
|
}
|
|||
|
|
if !strings.Contains(err.Error(), "declare parking") {
|
|||
|
|
t.Fatalf("err = %v, 期望包含 declare parking", err)
|
|||
|
|
}
|
|||
|
|
if got := f.indexOf("queue:" + LikeQueue); got >= 0 {
|
|||
|
|
t.Fatalf("停泊链路失败后不应继续声明业务队列,calls = %v", f.calls)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestDeclareParking_IsIdempotentOnReplay(t *testing.T) {
|
|||
|
|
f := &fakeDeclarer{}
|
|||
|
|
for i := 0; i < 3; i++ {
|
|||
|
|
if err := declareParking(f); err != nil {
|
|||
|
|
t.Fatalf("第 %d 次 declareParking: %v", i+1, err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if got := len(f.calls); got != 9 {
|
|||
|
|
t.Fatalf("三次声明共 %d 次调用, want 9(每代连接都重放,必须幂等)", got)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// —— 重试队列参数 ——
|
|||
|
|
|
|||
|
|
func TestRetryQueueArgs_TTLAndDefaultExchange(t *testing.T) {
|
|||
|
|
args := RetryQueueArgs("like.events", 5*time.Second)
|
|||
|
|
|
|||
|
|
if got, ok := args["x-message-ttl"].(int64); !ok || got != 5000 {
|
|||
|
|
t.Fatalf("x-message-ttl = %#v, want int64(5000)", args["x-message-ttl"])
|
|||
|
|
}
|
|||
|
|
// 空串就是默认交换机:按队列名精确投递,不可能被 topic 扇出成多份。
|
|||
|
|
if got := args["x-dead-letter-exchange"]; got != "" {
|
|||
|
|
t.Fatalf("x-dead-letter-exchange = %#v, want \"\"(默认交换机)", got)
|
|||
|
|
}
|
|||
|
|
if got := args["x-dead-letter-routing-key"]; got != "like.events" {
|
|||
|
|
t.Fatalf("x-dead-letter-routing-key = %#v, want 队列名", got)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestRetryQueueArgs_NonPositiveDelayFallsBackToDefault(t *testing.T) {
|
|||
|
|
for _, delay := range []time.Duration{0, -time.Second} {
|
|||
|
|
args := RetryQueueArgs("comment.events", delay)
|
|||
|
|
if got := args["x-message-ttl"]; got != int64(DefaultRetryDelay/time.Millisecond) {
|
|||
|
|
t.Fatalf("delay=%v 时 x-message-ttl = %#v, want 默认延迟", delay, got)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TestRetryArgs_OnlyOverridesDeadLetterExchange 防止有人"顺手"加上
|
|||
|
|
// x-dead-letter-routing-key:那会让第 1 次被拒绝时路由键也被改写,
|
|||
|
|
// 而消费者在第一次投递时依赖原路由键(见 §6.8.6 的 N13)。
|
|||
|
|
func TestRetryArgs_OnlyOverridesDeadLetterExchange(t *testing.T) {
|
|||
|
|
args := RetryArgs("social.events")
|
|||
|
|
if len(args) != 1 {
|
|||
|
|
t.Fatalf("args = %#v, want 只有 x-dead-letter-exchange", args)
|
|||
|
|
}
|
|||
|
|
if got := args["x-dead-letter-exchange"]; got != RetryExchange("social.events") {
|
|||
|
|
t.Fatalf("x-dead-letter-exchange = %#v, want %q", got, RetryExchange("social.events"))
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestRetryNames_PerQueue(t *testing.T) {
|
|||
|
|
if got := RetryExchange("like.events"); got != "retry.events.like.events" {
|
|||
|
|
t.Fatalf("RetryExchange = %q", got)
|
|||
|
|
}
|
|||
|
|
if got := RetryQueueName("like.events"); got != "like.events.retry" {
|
|||
|
|
t.Fatalf("RetryQueueName = %q", got)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// —— 停泊消息的解析 ——
|
|||
|
|
|
|||
|
|
func TestParseParkedInfo_FromHeaders(t *testing.T) {
|
|||
|
|
d := amqp.Delivery{
|
|||
|
|
RoutingKey: "parked.comment.events",
|
|||
|
|
Body: []byte(`{"event_id":"abc123","action":"publish"}`),
|
|||
|
|
Headers: amqp.Table{
|
|||
|
|
HeaderSourceQueue: "notification.comment",
|
|||
|
|
HeaderAttempts: int32(4),
|
|||
|
|
HeaderLastError: "db: connection refused",
|
|||
|
|
},
|
|||
|
|
}
|
|||
|
|
info := ParseParkedInfo(d)
|
|||
|
|
|
|||
|
|
if info.SourceQueue != "notification.comment" {
|
|||
|
|
t.Fatalf("SourceQueue = %q", info.SourceQueue)
|
|||
|
|
}
|
|||
|
|
if info.Attempts != 4 {
|
|||
|
|
t.Fatalf("Attempts = %d, want 4", info.Attempts)
|
|||
|
|
}
|
|||
|
|
if info.LastError != "db: connection refused" {
|
|||
|
|
t.Fatalf("LastError = %q", info.LastError)
|
|||
|
|
}
|
|||
|
|
if info.EventID != "abc123" {
|
|||
|
|
t.Fatalf("EventID = %q", info.EventID)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestParseParkedInfo_FallsBackToRoutingKey(t *testing.T) {
|
|||
|
|
d := amqp.Delivery{RoutingKey: "parked.social.events", Body: []byte(`{}`)}
|
|||
|
|
info := ParseParkedInfo(d)
|
|||
|
|
|
|||
|
|
if info.SourceQueue != "social.events" {
|
|||
|
|
t.Fatalf("SourceQueue = %q, want 从 parked. 前缀反推", info.SourceQueue)
|
|||
|
|
}
|
|||
|
|
if info.Attempts != 1 {
|
|||
|
|
t.Fatalf("Attempts = %d, want 默认 1", info.Attempts)
|
|||
|
|
}
|
|||
|
|
if info.EventID != "" {
|
|||
|
|
t.Fatalf("EventID = %q, want 空(坏消息没有意图键)", info.EventID)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestParseParkedInfo_MalformedBodyDoesNotPanic(t *testing.T) {
|
|||
|
|
info := ParseParkedInfo(amqp.Delivery{RoutingKey: "parked.x", Body: []byte("not json")})
|
|||
|
|
if info.EventID != "" {
|
|||
|
|
t.Fatalf("EventID = %q, want 空", info.EventID)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|