mirror of
https://github.com/truewhile/MeBox.git
synced 2026-11-16 12:54:22 +08:00
优化
This commit is contained in:
@@ -5,11 +5,13 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/truewhile/MeBox/internal/config"
|
||||
"github.com/truewhile/MeBox/internal/helper"
|
||||
"github.com/truewhile/MeBox/internal/model"
|
||||
)
|
||||
|
||||
@@ -132,3 +134,71 @@ func TestFetchMediaDoesNotRetryNotFound(t *testing.T) {
|
||||
t.Fatalf("上游请求次数 = %d,期望 1(404 不该重试)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMediaHTTPClientHasNoOverallTimeout 媒体客户端不能用「整体超时」。
|
||||
//
|
||||
// 这是本次故障的第二个根因:http.Client.Timeout 覆盖「读完 body」这一阶段。
|
||||
// 一个 4MB 的 m4a 在跨境链路上很容易超过 30 秒,超时后 net/http 直接掐断
|
||||
// 响应体,而调用方的 io.Copy 只是正常返回——表现为**静默截断**(播放器拿到
|
||||
// 半首歌,判「格式不支持」)或 502。所以媒体客户端必须没有整体超时。
|
||||
func TestMediaHTTPClientHasNoOverallTimeout(t *testing.T) {
|
||||
client := helper.NewMediaHTTPClient(false)
|
||||
if client.Timeout != 0 {
|
||||
t.Fatalf("媒体 client 的整体 Timeout = %v,必须为 0(大文件下载会被静默截断)", client.Timeout)
|
||||
}
|
||||
tr, ok := client.Transport.(*http.Transport)
|
||||
if !ok {
|
||||
t.Fatalf("Transport 类型 = %T", client.Transport)
|
||||
}
|
||||
if tr.ResponseHeaderTimeout <= 0 {
|
||||
t.Fatal("媒体 client 应设置 ResponseHeaderTimeout,避免上游不给首字节时无限挂起")
|
||||
}
|
||||
// 必须走 HTTP/1.1:腾讯 CDN 的 h2 边缘在长连接大文件上不回 WINDOW_UPDATE,
|
||||
// Go 的 h2 初始连接窗口是 1MiB,下载会稳定断在 1MiB/2MiB/3MiB 整数倍处。
|
||||
if tr.ForceAttemptHTTP2 {
|
||||
t.Error("媒体 client 必须关闭 HTTP/2(1MiB 窗口截断)")
|
||||
}
|
||||
if tr.TLSNextProto == nil || len(tr.TLSNextProto) != 0 {
|
||||
t.Error("媒体 client 需要清空 TLSNextProto,才能确保不用 h2")
|
||||
}
|
||||
}
|
||||
|
||||
// TestFetchMediaStreamsLargeBodyWithoutTruncation 大响应体必须完整送达。
|
||||
//
|
||||
// 用「慢速分块发送」模拟跨境链路上的大音频:总耗时超过规则请求的 30 秒超时,
|
||||
// 但媒体通路必须把每一个字节都交出来,绝不静默截断。
|
||||
func TestFetchMediaStreamsLargeBodyWithoutTruncation(t *testing.T) {
|
||||
const totalBytes = 24 << 20 // 24MB
|
||||
const chunk = 512 << 10
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "audio/mp4")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(totalBytes))
|
||||
payload := make([]byte, chunk)
|
||||
for off := 0; off < totalBytes; off += chunk {
|
||||
if _, err := w.Write(payload); err != nil {
|
||||
return
|
||||
}
|
||||
w.(http.Flusher).Flush()
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
svc, _ := newLoginTestService(t)
|
||||
book := &model.ReaderBook{Origin: srv.URL}
|
||||
|
||||
resp, err := svc.FetchMedia(t.Context(), book, srv.URL+"/big.m4a", "")
|
||||
if err != nil {
|
||||
t.Fatalf("FetchMedia 失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
got, readErr := io.Copy(io.Discard, resp.Body)
|
||||
if readErr != nil {
|
||||
t.Fatalf("读取响应体失败: %v", readErr)
|
||||
}
|
||||
if got != totalBytes {
|
||||
t.Fatalf("交付字节 = %d,期望 %d(少了 %d 字节 → 播放器会判格式不支持)",
|
||||
got, totalBytes, int64(totalBytes)-got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user