// backpressure_test.go Day 50:BackpressureShrink 单元验证—— // L2 水位收紧取 min / 键缺失与水位不足回退原值 / 1s 本地缓存窗口。 // (Day50 压测修复:openapi 账号桶 CapacityOf+RateOf 复用同一 shrink—— // 稳态吞吐由 rate 决定,仅收容量对持续流量无效。) package middleware import ( "context" "net/http" "net/http/httptest" "testing" "time" "github.com/gin-gonic/gin" "github.com/redis/go-redis/v9" ) func shrinkOf(t *testing.T, base int64) (func(*gin.Context) int64, redis.UniversalClient) { t.Helper() c := newMiniRedis(t) return BackpressureShrink(c, func(*gin.Context) int64 { return base }), c } // newBPContext 带 Request 的 gin 上下文(shrink 内部取 c.Request.Context())。 func newBPContext() *gin.Context { w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/", nil) return c } // TestBackpressureShrink_TightenToMin L2 且 bp:capbase 时保持 base(收紧语义是 min,不放大量)。 if err := c.Set(context.Background(), bpCapKey, "9999", 0).Err(); err != nil { t.Fatal(err) } time.Sleep(1100 * time.Millisecond) // 过本地缓存 TTL if got := shrink(newBPContext()); got != 6000 { t.Fatalf("bp:cap>base 时 = %d, 期望保持 6000", got) } } // TestBackpressureShrink_Fallback 键缺失/水位