50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package handler
|
||
|
||
import (
|
||
"testing"
|
||
|
||
"kefu-cloud/server/internal/model"
|
||
)
|
||
|
||
func TestPickLeastLoadedPrefersFewerSessions(t *testing.T) {
|
||
a := model.User{ID: 1}
|
||
b := model.User{ID: 2}
|
||
c := model.User{ID: 3}
|
||
candidates := []model.User{a, b, c}
|
||
counts := map[uint]int64{1: 3, 2: 1, 3: 1}
|
||
got := pickLeastLoadedAgent(candidates, counts)
|
||
if got == nil || got.ID != 2 {
|
||
t.Fatalf("期望 id=2(平局取更小 id),got=%v", got)
|
||
}
|
||
}
|
||
|
||
func TestPickRoundRobinAdvances(t *testing.T) {
|
||
candidates := []model.User{{ID: 10}, {ID: 20}, {ID: 30}}
|
||
last := uint(10)
|
||
got := pickRoundRobinAgent(candidates, &last)
|
||
if got == nil || got.ID != 20 {
|
||
t.Fatalf("期望下一位 20,got=%v", got)
|
||
}
|
||
last = 30
|
||
got = pickRoundRobinAgent(candidates, &last)
|
||
if got == nil || got.ID != 10 {
|
||
t.Fatalf("期望绕回 10,got=%v", got)
|
||
}
|
||
got = pickRoundRobinAgent(candidates, nil)
|
||
if got == nil || got.ID != 10 {
|
||
t.Fatalf("无游标时期望第一位 10,got=%v", got)
|
||
}
|
||
}
|
||
|
||
func TestNormalizeAssignStrategy(t *testing.T) {
|
||
if normalizeAssignStrategy("round_robin") != assignStrategyRoundRobin {
|
||
t.Fatal("round_robin")
|
||
}
|
||
if normalizeAssignStrategy("") != assignStrategyLeastLoad {
|
||
t.Fatal("default least_load")
|
||
}
|
||
if normalizeAssignStrategy("unknown") != assignStrategyLeastLoad {
|
||
t.Fatal("unknown -> least_load")
|
||
}
|
||
}
|