mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
add test for selectNodes()
This commit is contained in:
parent
5c2c76b85c
commit
6d417949ae
@ -45,8 +45,8 @@ func selectNodes(nodes []*Node, logger log.Logger, expected int, baselines []opt
|
|||||||
// go through all base line until find expected selects
|
// go through all base line until find expected selects
|
||||||
for _, b := range baselines {
|
for _, b := range baselines {
|
||||||
baseline := time.Duration(b)
|
baseline := time.Duration(b)
|
||||||
for i := 0; i < availableCount; i++ {
|
for i := count; i < availableCount; i++ {
|
||||||
if nodes[i].Weighted > baseline {
|
if nodes[i].Weighted >= baseline {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
count = i + 1
|
count = i + 1
|
||||||
|
39
balancer/balancer_select_test.go
Normal file
39
balancer/balancer_select_test.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package balancer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing-box/option"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSelectNodes(t *testing.T) {
|
||||||
|
nodes := []*Node{
|
||||||
|
{HealthCheckStats: HealthCheckStats{Weighted: 50}},
|
||||||
|
{HealthCheckStats: HealthCheckStats{Weighted: 70}},
|
||||||
|
{HealthCheckStats: HealthCheckStats{Weighted: 100}},
|
||||||
|
{HealthCheckStats: HealthCheckStats{Weighted: 110}},
|
||||||
|
{HealthCheckStats: HealthCheckStats{Weighted: 120}},
|
||||||
|
{HealthCheckStats: HealthCheckStats{Weighted: 150}},
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
expected int
|
||||||
|
baselines []option.Duration
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{expected: -1, baselines: nil, want: 1},
|
||||||
|
{expected: 0, baselines: nil, want: 1},
|
||||||
|
{expected: 1, baselines: nil, want: 1},
|
||||||
|
{expected: 9999, baselines: nil, want: len(nodes)},
|
||||||
|
{expected: 0, baselines: []option.Duration{80, 100}, want: 2},
|
||||||
|
{expected: 2, baselines: []option.Duration{50, 100}, want: 2},
|
||||||
|
{expected: 3, baselines: []option.Duration{50, 100, 150}, want: 5},
|
||||||
|
{expected: 9999, baselines: []option.Duration{50, 100, 150}, want: len(nodes)},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run("", func(t *testing.T) {
|
||||||
|
if got := selectNodes(nodes, nil, tt.expected, tt.baselines); len(got) != tt.want {
|
||||||
|
t.Errorf("selectNodes() = %v, want %v", len(got), tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user