140 lines
3.8 KiB
Go
140 lines
3.8 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestIsQueueDeadError(t *testing.T) {
|
|
tests := []struct {
|
|
err error
|
|
expected bool
|
|
}{
|
|
{nil, false},
|
|
{errors.New("订单不可操作"), true},
|
|
{errors.New("补充发货账号失败: 订单不可操作"), true},
|
|
{errors.New("订单不存在"), true},
|
|
{errors.New("队列已失效"), true},
|
|
{errors.New("该订单正在处理中,请勿重复提交"), true},
|
|
{errors.New("创建发货队列失败: 该订单正在处理中,请勿重复提交"), true},
|
|
{errors.New("账号尚未绑定"), false},
|
|
{errors.New("请求过于频繁"), false},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
got := isQueueDeadError(tc.err)
|
|
if got != tc.expected {
|
|
t.Errorf("isQueueDeadError(%v) = %v, expected %v", tc.err, got, tc.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsRateLimitError(t *testing.T) {
|
|
tests := []struct {
|
|
err error
|
|
expected bool
|
|
}{
|
|
{nil, false},
|
|
{errors.New("请求过于频繁,请稍后再试"), true},
|
|
{errors.New("系统繁忙,请稍后再试"), true},
|
|
{errors.New("Rate limit exceeded"), true},
|
|
{errors.New("Too Many Requests"), true},
|
|
{errors.New("账号尚未绑定"), false},
|
|
{errors.New("订单不存在"), false},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
got := isRateLimitError(tc.err)
|
|
if got != tc.expected {
|
|
t.Errorf("isRateLimitError(%v) = %v, expected %v", tc.err, got, tc.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsTransientDeliveryError(t *testing.T) {
|
|
tests := []struct {
|
|
err error
|
|
statusCode int
|
|
message string
|
|
expected bool
|
|
}{
|
|
{errors.New("dial tcp timeout"), 0, "", true},
|
|
{nil, 429, "", true},
|
|
{nil, 502, "", true},
|
|
{nil, 503, "", true},
|
|
{nil, 504, "", true},
|
|
{nil, 400, "绑定校验失败: 请求过于频繁,请稍后再试", true},
|
|
{nil, 400, "请求过于频繁", true},
|
|
{nil, 400, "系统繁忙,请稍后再试", true},
|
|
{nil, 400, "网络繁忙", true},
|
|
{nil, 400, "账号尚未绑定,请扫码完成绑定后再提交", false},
|
|
{nil, 400, "订单不可操作", false},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
got := isTransientDeliveryError(tc.err, tc.statusCode, tc.message)
|
|
if got != tc.expected {
|
|
t.Errorf("isTransientDeliveryError(%v, %d, %q) = %v, expected %v", tc.err, tc.statusCode, tc.message, got, tc.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSignProxyRetriesOnRateLimitAndSucceeds(t *testing.T) {
|
|
var callCount int32
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
count := atomic.AddInt32(&callCount, 1)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if count == 1 {
|
|
// 第一次调用返回频控限流
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"code": 1,
|
|
"message": "请求过于频繁,请稍后再试",
|
|
})
|
|
return
|
|
}
|
|
|
|
// 第二次重试成功
|
|
w.WriteHeader(http.StatusOK)
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"code": 0,
|
|
"message": "success",
|
|
"data": map[string]interface{}{
|
|
"gameAccount": map[string]interface{}{
|
|
"game_account": "12345678",
|
|
},
|
|
},
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
p := NewDLCProvider(server.URL, "dlc", client)
|
|
|
|
var out struct {
|
|
GameAccount map[string]interface{} `json:"gameAccount"`
|
|
}
|
|
err := p.signProxy("/public/games/account-bound", "POST", map[string]interface{}{
|
|
"bind_uuid": "test-uuid",
|
|
}, &out)
|
|
|
|
if err != nil {
|
|
t.Fatalf("signProxy 预期在退避重试后成功,但返回错误: %v", err)
|
|
}
|
|
|
|
finalCount := atomic.LoadInt32(&callCount)
|
|
if finalCount != 2 {
|
|
t.Fatalf("预期总共调用 2 次(1次失败 + 1次重试成功),实际调用: %d", finalCount)
|
|
}
|
|
|
|
if out.GameAccount == nil || out.GameAccount["game_account"] != "12345678" {
|
|
t.Fatalf("预期返回账号信息,实际: %v", out.GameAccount)
|
|
}
|
|
}
|