|
|
|
@@ -16,7 +16,8 @@ import ( |
|
|
|
// shouldRetry — 渠道选择删除后的验证测试 |
|
|
|
// --------------------------------------------------------------------------- |
|
|
|
|
|
|
|
func newTestContext() *gin.Context { |
|
|
|
func newTestContext(t *testing.T) *gin.Context { |
|
|
|
t.Helper() |
|
|
|
gin.SetMode(gin.TestMode) |
|
|
|
w := httptest.NewRecorder() |
|
|
|
c, _ := gin.CreateTestContext(w) |
|
|
|
@@ -25,7 +26,7 @@ func newTestContext() *gin.Context { |
|
|
|
} |
|
|
|
|
|
|
|
func TestShouldRetry_NilError_ReturnsFalse(t *testing.T) { |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
assert.False(t, shouldRetry(c, nil, 3)) |
|
|
|
} |
|
|
|
|
|
|
|
@@ -33,7 +34,7 @@ func TestShouldRetry_5xxErrorWithRetryLeft_ReturnsTrue(t *testing.T) { |
|
|
|
// 渠道选择删除后,所有请求都能正常重试 |
|
|
|
// 之前设置了 specific_channel_id 时 shouldRetry 会返回 false |
|
|
|
// 现在即使不设置任何 channel 相关 context,5xx 也应该重试 |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
apiErr := types.NewErrorWithStatusCode( |
|
|
|
errors.New("upstream error"), |
|
|
|
types.ErrorCodeBadResponseStatusCode, |
|
|
|
@@ -43,7 +44,7 @@ func TestShouldRetry_5xxErrorWithRetryLeft_ReturnsTrue(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestShouldRetry_5xxErrorZeroRetryLeft_ReturnsFalse(t *testing.T) { |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
apiErr := types.NewErrorWithStatusCode( |
|
|
|
errors.New("upstream error"), |
|
|
|
types.ErrorCodeBadResponseStatusCode, |
|
|
|
@@ -53,7 +54,7 @@ func TestShouldRetry_5xxErrorZeroRetryLeft_ReturnsFalse(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestShouldRetry_SkipRetryError_ReturnsFalse(t *testing.T) { |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
apiErr := types.NewError( |
|
|
|
errors.New("skip retry"), |
|
|
|
types.ErrorCodeBadResponseStatusCode, |
|
|
|
@@ -64,7 +65,7 @@ func TestShouldRetry_SkipRetryError_ReturnsFalse(t *testing.T) { |
|
|
|
|
|
|
|
func TestShouldRetry_ChannelError_ReturnsTrue(t *testing.T) { |
|
|
|
// channel: 前缀的错误码会触发立即重试 |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
apiErr := types.NewError( |
|
|
|
errors.New("channel error"), |
|
|
|
"channel:test", |
|
|
|
@@ -74,7 +75,7 @@ func TestShouldRetry_ChannelError_ReturnsTrue(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestShouldRetry_2xxStatus_ReturnsFalse(t *testing.T) { |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
apiErr := types.NewErrorWithStatusCode( |
|
|
|
errors.New("ok"), |
|
|
|
types.ErrorCodeBadResponseStatusCode, |
|
|
|
@@ -84,7 +85,7 @@ func TestShouldRetry_2xxStatus_ReturnsFalse(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestShouldRetry_429Status_ReturnsTrue(t *testing.T) { |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
apiErr := types.NewErrorWithStatusCode( |
|
|
|
errors.New("rate limited"), |
|
|
|
types.ErrorCodeBadResponseStatusCode, |
|
|
|
@@ -98,12 +99,12 @@ func TestShouldRetry_429Status_ReturnsTrue(t *testing.T) { |
|
|
|
// --------------------------------------------------------------------------- |
|
|
|
|
|
|
|
func TestShouldRetryTaskRelay_NilError_ReturnsFalse(t *testing.T) { |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
assert.False(t, shouldRetryTaskRelay(c, 1, nil, 3)) |
|
|
|
} |
|
|
|
|
|
|
|
func TestShouldRetryTaskRelay_5xxWithRetryLeft_ReturnsTrue(t *testing.T) { |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
taskErr := &dto.TaskError{ |
|
|
|
Error: errors.New("server error"), |
|
|
|
StatusCode: http.StatusInternalServerError, |
|
|
|
@@ -112,7 +113,7 @@ func TestShouldRetryTaskRelay_5xxWithRetryLeft_ReturnsTrue(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestShouldRetryTaskRelay_5xxZeroRetryLeft_ReturnsFalse(t *testing.T) { |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
taskErr := &dto.TaskError{ |
|
|
|
Error: errors.New("server error"), |
|
|
|
StatusCode: http.StatusInternalServerError, |
|
|
|
@@ -121,7 +122,7 @@ func TestShouldRetryTaskRelay_5xxZeroRetryLeft_ReturnsFalse(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestShouldRetryTaskRelay_429_ReturnsTrue(t *testing.T) { |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
taskErr := &dto.TaskError{ |
|
|
|
Error: errors.New("rate limited"), |
|
|
|
StatusCode: http.StatusTooManyRequests, |
|
|
|
@@ -130,7 +131,7 @@ func TestShouldRetryTaskRelay_429_ReturnsTrue(t *testing.T) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestShouldRetryTaskRelay_400_ReturnsFalse(t *testing.T) { |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
taskErr := &dto.TaskError{ |
|
|
|
Error: errors.New("bad request"), |
|
|
|
StatusCode: http.StatusBadRequest, |
|
|
|
@@ -141,7 +142,7 @@ func TestShouldRetryTaskRelay_400_ReturnsFalse(t *testing.T) { |
|
|
|
func TestShouldRetryTaskRelay_LocalError_ReturnsFalse(t *testing.T) { |
|
|
|
// LocalError + 非 5xx 状态码(如 403)时,LocalError 检查生效返回 false |
|
|
|
// 注意:5xx 分支在 LocalError 检查之前,所以 localError+5xx 仍会重试 |
|
|
|
c := newTestContext() |
|
|
|
c := newTestContext(t) |
|
|
|
taskErr := &dto.TaskError{ |
|
|
|
Error: errors.New("local error"), |
|
|
|
StatusCode: http.StatusForbidden, |
|
|
|
|