/
t3
/
cli
Обзор
Документация
Войти
/
t3
/
cli
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
internal/controller/distribution_test.go
107 строк
2 KB
Ivan
fix user allocation
09 июл 2026, 13:00
09 июл 2026, 13:00
f431bcf
Код
Авторство
О чём код?
package controller import "testing" func TestDistributeUsersByWeight(t *testing.T) { tests := []struct { name string totalUsers int32 weights []int totalWeight int want []int32 }{ { name: "10 users with weights 2 and 1", totalUsers: 10, weights: []int{2, 1}, totalWeight: 3, want: []int32{7, 3}, }, { name: "100 users with weights 3 and 1", totalUsers: 100, weights: []int{3, 1}, totalWeight: 4, want: []int32{75, 25}, }, { name: "5 users with weights 1 and 1", totalUsers: 5, weights: []int{1, 1}, totalWeight: 2, want: []int32{3, 2}, }, { name: "1 user with weights 2 and 1", totalUsers: 1, weights: []int{2, 1}, totalWeight: 3, want: []int32{1, 0}, // largest remainder gives 1 to agent with biggest remainder }, { name: "3 users with weights 1, 2, 1", totalUsers: 3, weights: []int{1, 2, 1}, totalWeight: 4, want: []int32{1, 1, 1}, // each gets 1 (minimum guarantee) }, { name: "1000 users with weights 7, 2, 1", totalUsers: 1000, weights: []int{7, 2, 1}, totalWeight: 10, want: []int32{700, 200, 100}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { agents := make([]*agentConn, len(tt.weights)) for i, w := range tt.weights { agents[i] = &agentConn{weight: w} } result := DistributeUsersByWeight(tt.totalUsers, agents, tt.totalWeight) // Check total sum equals totalUsers var sum int32 for _, v := range result { sum += v } if sum != tt.totalUsers { t.Errorf("sum of distributed users = %d, want %d", sum, tt.totalUsers) } // Check expected distribution for i, v := range tt.want { if result[i] != v { t.Errorf("agent %d got %d users, want %d", i, result[i], v) } } }) } } func TestDistributeUsersByWeightWithNilAgent(t *testing.T) { agents := []*agentConn{ {weight: 2}, nil, // simulate failed connection {weight: 1}, } result := DistributeUsersByWeight(10, agents, 3) // Sum should equal 10 var sum int32 for _, v := range result { sum += v } if sum != 10 { t.Errorf("sum = %d, want 10", sum) } // Agent at index 1 (nil) should have 0 if result[1] != 0 { t.Errorf("nil agent at index 1 got %d, want 0", result[1]) } }