Files
pay/internal/accounts/picker_test.go
T

48 lines
1.4 KiB
Go

package accounts_test
import (
"errors"
"testing"
"github.com/wangjia/pay/config"
"github.com/wangjia/pay/internal/accounts"
)
func cands(ids ...string) []config.AccountConfig {
out := make([]config.AccountConfig, 0, len(ids))
for _, id := range ids {
out = append(out, config.AccountConfig{AccountID: id, Channel: "fake", Region: "global", Enabled: true, Weight: 1})
}
return out
}
func TestRoundRobinCyclesInOrder(t *testing.T) {
rr := accounts.NewRoundRobin()
cs := cands("a1", "a2", "a3")
want := []string{"a1", "a2", "a3", "a1", "a2"}
for i, w := range want {
got, err := rr.Pick("fake|global", cs, accounts.PickHint{})
if err != nil || got.AccountID != w {
t.Fatalf("call %d: got %v err %v, want %s", i, got.AccountID, err, w)
}
}
}
func TestRoundRobinKeysAreIndependent(t *testing.T) {
rr := accounts.NewRoundRobin()
cs := cands("a1", "a2")
// 不同 key(渠道/区域)各自独立计数,互不串扰。
g1, _ := rr.Pick("alipay|cn", cs, accounts.PickHint{})
g2, _ := rr.Pick("wechat|cn", cs, accounts.PickHint{})
if g1.AccountID != "a1" || g2.AccountID != "a1" {
t.Fatalf("每个 key 首次都应从 a1 起, got %s / %s", g1.AccountID, g2.AccountID)
}
}
func TestStrategyEmptyCandidates(t *testing.T) {
rr := accounts.NewRoundRobin()
if _, err := rr.Pick("fake|global", nil, accounts.PickHint{}); !errors.Is(err, accounts.ErrNoAccount) {
t.Fatalf("空候选应 ErrNoAccount, got %v", err)
}
}