This commit is contained in:
truewhile
2026-09-23 13:53:50 +08:00
parent 94ef0116b1
commit 44fde6c5a0
5 changed files with 466 additions and 23 deletions
+142
View File
@@ -1,9 +1,12 @@
package service
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"go.uber.org/zap"
)
@@ -160,3 +163,142 @@ func TestIntroDBFetchParsesBody(t *testing.T) {
t.Fatalf("spans = %#v", spans)
}
}
// 实测:从生产机连发 45 个请求有 15 个被返回 429,加间隔重试后其中 10 个成功。
// 所以限流值得一次重试,否则那部分播放会静默少掉「跳过片头」按钮。
func TestIntroDBFetchRetriesRateLimit(t *testing.T) {
var calls int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
if atomic.AddInt32(&calls, 1) == 1 {
w.WriteHeader(http.StatusTooManyRequests)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(introDBTVPayload))
}))
defer server.Close()
svc := NewIntroDBService(zap.NewNop()).SetBaseURL(server.URL).SetRetryDelay(0)
spans, err := svc.Fetch(t.Context(), 1396, 1, 1)
if err != nil {
t.Fatalf("fetch: %v", err)
}
if len(spans) != 2 {
t.Fatalf("spans = %#v, want the retried response", spans)
}
if got := atomic.LoadInt32(&calls); got != 2 {
t.Fatalf("calls = %d, want 2 (the original plus one retry)", got)
}
}
func TestIntroDBFetchGivesUpAfterRetryBudget(t *testing.T) {
var calls int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusTooManyRequests)
}))
defer server.Close()
svc := NewIntroDBService(zap.NewNop()).SetBaseURL(server.URL).SetRetryDelay(0)
if _, err := svc.Fetch(t.Context(), 1396, 1, 1); err == nil {
t.Fatal("a persistent 429 must surface as an error so the caller keeps its cache")
}
// 只重试一次:限流通常不是靠密集重试解决的,而调用方的等待预算有限。
if got := atomic.LoadInt32(&calls); got != 2 {
t.Fatalf("calls = %d, want 2 (one retry, then give up)", got)
}
}
func TestIntroDBFetchDoesNotRetryNotFound(t *testing.T) {
var calls int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
svc := NewIntroDBService(zap.NewNop()).SetBaseURL(server.URL).SetRetryDelay(0)
spans, err := svc.Fetch(t.Context(), 424242, 0, 0)
if err != nil || len(spans) != 0 {
t.Fatalf("spans = %#v err = %v, want a cached miss", spans, err)
}
if got := atomic.LoadInt32(&calls); got != 1 {
t.Fatalf("calls = %d, want 1: 404 means \"no data\", it is not worth retrying", got)
}
}
// 调用方预算不够时不能为了重试干等:Emby 只给 5 秒,等下去会把
// 「少一个跳过按钮」升级成「请求超时」。
func TestIntroDBFetchSkipsRetryWhenCallerBudgetIsSpent(t *testing.T) {
var calls int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusTooManyRequests)
}))
defer server.Close()
svc := NewIntroDBService(zap.NewNop()).SetBaseURL(server.URL).SetRetryDelay(time.Minute)
ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond)
defer cancel()
started := time.Now()
if _, err := svc.Fetch(ctx, 1396, 1, 1); err == nil {
t.Fatal("want an error when the budget is spent")
}
if elapsed := time.Since(started); elapsed > 2*time.Second {
t.Fatalf("Fetch waited %s, want it to give up promptly", elapsed)
}
if got := atomic.LoadInt32(&calls); got != 1 {
t.Fatalf("calls = %d, want 1 without a retry", got)
}
}
func TestIntroDBRetryWaitPrefersRetryAfterHeader(t *testing.T) {
svc := NewIntroDBService(zap.NewNop())
if got := svc.retryWait(2 * time.Second); got != 2*time.Second {
t.Fatalf("retryWait(2s) = %s, want the server's value", got)
}
if got := svc.retryWait(0); got != introDBRetryDelay {
t.Fatalf("retryWait(0) = %s, want the default %s", got, introDBRetryDelay)
}
// 服务端可以要求等很久,但一次播放不值得为它挂住几十秒。
if got := svc.retryWait(10 * time.Minute); got != introDBMaxRetryDelay {
t.Fatalf("retryWait(10m) = %s, want it capped at %s", got, introDBMaxRetryDelay)
}
}
func TestParseIntroDBRetryAfter(t *testing.T) {
cases := []struct {
value string
want time.Duration
}{
{"2", 2 * time.Second},
{" 3 ", 3 * time.Second},
{"", 0},
{"abc", 0},
{"-5", 0},
{"0", 0},
}
for _, tc := range cases {
if got := parseIntroDBRetryAfter(tc.value); got != tc.want {
t.Fatalf("parseIntroDBRetryAfter(%q) = %s, want %s", tc.value, got, tc.want)
}
}
}
func TestIntroDBRetryableStatus(t *testing.T) {
retryable := []int{http.StatusTooManyRequests, http.StatusServiceUnavailable}
for _, status := range retryable {
if !introDBRetryableStatus(status) {
t.Fatalf("status %d should be retryable", status)
}
}
// 404 由 fetchOnce 单独处理;500 之类的服务端故障重试也不会变好,
// 却会白占调用方的等待预算。
notRetryable := []int{http.StatusNotFound, http.StatusInternalServerError, http.StatusBadRequest}
for _, status := range notRetryable {
if introDBRetryableStatus(status) {
t.Fatalf("status %d should not be retryable", status)
}
}
}