add test for selectNodes()

This commit is contained in:
jebbs 2022-10-11 14:51:15 +08:00
parent 5c2c76b85c
commit 6d417949ae
2 changed files with 41 additions and 2 deletions

View File

@ -45,8 +45,8 @@ func selectNodes(nodes []*Node, logger log.Logger, expected int, baselines []opt
// go through all base line until find expected selects
for _, b := range baselines {
baseline := time.Duration(b)
for i := 0; i < availableCount; i++ {
if nodes[i].Weighted > baseline {
for i := count; i < availableCount; i++ {
if nodes[i].Weighted >= baseline {
break
}
count = i + 1

View 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)
}
})
}
}