package rolesync import ( "context" "encoding/json" "errors" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gcfg" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/library/rabbitmq" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model" "testing" "time" ) func TestMessageRetryAndDeadLetter(t *testing.T) { originalSync, originalRetry, originalDead := syncRoleMessage, publishRoleRetry, publishRoleDeadLetter t.Cleanup(func() { syncRoleMessage, publishRoleRetry, publishRoleDeadLetter = originalSync, originalRetry, originalDead }) syncRoleMessage = func(_ context.Context, message *model.AgentRoleSyncMessage) error { message.ChildDeptIds = []int64{200} return errors.New("child failed") } for attempt := 0; attempt <= 5; attempt++ { retries, deadLetters := 0, 0 publishRoleRetry = func(_ context.Context, delay time.Duration, body []byte) error { retries++ var message model.AgentRoleSyncMessage if err := json.Unmarshal(body, &message); err != nil { t.Fatal(err) } if message.RetryCount != attempt+1 || len(message.ChildDeptIds) != 1 || message.ChildDeptIds[0] != 200 || delay != time.Duration(1<