Files

120 lines
3.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// pacer_test.go Day 48:下发调度三件套单测——令牌桶限速/在途窗口/自动降速/匀速预算。
package dispatch
import (
"context"
"math"
"testing"
"time"
)
// TestTokenBucketRate 令牌桶:满桶直取;补令牌按 rate 时间等待。
func TestTokenBucketRate(t *testing.T) {
b := NewTokenBucket(2) // 2 个/秒
ctx := context.Background()
start := time.Now()
if err := b.Acquire(ctx, 2); err != nil { // 起步满桶
t.Fatal(err)
}
if el := time.Since(start); el > 50*time.Millisecond {
t.Fatalf("满桶不应等待: %v", el)
}
if err := b.Acquire(ctx, 1); err != nil { // 需补 1 个 ≈500ms
t.Fatal(err)
}
if el := time.Since(start); el < 300*time.Millisecond {
t.Fatalf("补令牌应等待 ≥300ms: %v", el)
}
}
// TestTokenBucketCtxCancel 等令牌期间 ctx 取消即失败。
func TestTokenBucketCtxCancel(t *testing.T) {
b := NewTokenBucket(1)
if err := b.Acquire(context.Background(), 1); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 80*time.Millisecond)
defer cancel()
if err := b.Acquire(ctx, 1); err == nil {
t.Fatal("ctx 超时应返回错误")
}
}
// TestPacerWindowBlocks 在途窗口:占满后阻塞,Release 后可再取。
func TestPacerWindowBlocks(t *testing.T) {
p := NewChannelPacer(true)
p.SetSpeed(7, 0) // 0=默认速率(桶大,不构成约束)
ctx := context.Background()
if err := p.Acquire(ctx, 7, PacerWindow); err != nil { // 占满窗口
t.Fatal(err)
}
ctx2, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer cancel()
if err := p.Acquire(ctx2, 7, 1); err == nil {
t.Fatal("窗口满应阻塞至超时")
}
p.Release(7, PacerWindow)
if err := p.Acquire(context.Background(), 7, 1); err != nil {
t.Fatalf("释放后应可取: %v", err)
}
}
// TestPacerDegradeOnSlowP50 p50>50ms 持续 30s→速率×0.9(下限 10%)。
func TestPacerDegradeOnSlowP50(t *testing.T) {
p := NewChannelPacer(true)
p.SetSpeed(7, 1000)
rate0 := p.Rate(7)
p.mu.Lock()
st := p.ch[7]
// 预置 128 个慢样本(p50=100ms>50)+首超时刻回拨 31s(免真等 30s)。
st.samples = make([]float64, PacerSamples)
for i := range st.samples {
st.samples[i] = 100.0
}
st.slowAt = time.Now().Add(-PacerSlowHold - time.Second)
p.mu.Unlock()
p.Observe(7, 100.0) // 触发降速判定
rate1 := p.Rate(7)
if math.Abs(rate1-rate0*0.9) > 0.01 {
t.Fatalf("降速后速率 = %v, 期望 %v", rate1, rate0*0.9)
}
// 快样本复位(p50 回落→计时清零)。
for i := 0; i < PacerSamples; i++ {
p.Observe(7, 1.0)
}
p.mu.Lock()
slowAt := p.ch[7].slowAt
p.mu.Unlock()
if !slowAt.IsZero() {
t.Fatal("p50 回落后 slowAt 应清零")
}
}
// TestPacerClosed 直通模式(timer 旧链路行为不变)。
func TestPacerClosed(t *testing.T) {
p := NewChannelPacer(false)
if err := p.Acquire(context.Background(), 1, 1_000_000); err != nil {
t.Fatal("关闭态应直通")
}
p.Release(1, 1_000_000) // 无状态不 panic
}
// TestPaceBudget 匀速投放:本轮=min(pending,Σspeed−inflight),下限 0。
func TestPaceBudget(t *testing.T) {
cases := []struct {
pending, sum, inflight, want int
}{
{5000, 3000, 0, 3000}, // 预算封顶
{1000, 3000, 0, 1000}, // 剩余封顶
{5000, 3000, 2500, 500}, // 在途挤压
{100, 3000, 3000, 0}, // 在途打满→0(防瞬时打满队列)
{10, 0, 0, 0}, // 无通道预算
}
for _, c := range cases {
if got := PaceBudget(c.pending, c.sum, c.inflight); got != c.want {
t.Fatalf("PaceBudget(%d,%d,%d)=%d 期望 %d", c.pending, c.sum, c.inflight, got, c.want)
}
}
}