2021-01-29 19:31:11 -05:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-06-18 19:02:46 -04:00
|
|
|
/*
|
|
|
|
Split into multiple package, need to be tested separately
|
|
|
|
|
2022-06-28 08:26:01 -04:00
|
|
|
func TestSelectLeastLoad(t *testing.T) {
|
|
|
|
settings := &StrategyLeastLoadConfig{
|
|
|
|
HealthCheck: &HealthPingConfig{
|
|
|
|
SamplingCount: 10,
|
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
MaxRTT: int64(time.Millisecond * time.Duration(800)),
|
|
|
|
}
|
|
|
|
strategy := NewLeastLoadStrategy(settings)
|
|
|
|
// std 40
|
|
|
|
strategy.PutResult("a", time.Millisecond*time.Duration(60))
|
|
|
|
strategy.PutResult("a", time.Millisecond*time.Duration(140))
|
|
|
|
strategy.PutResult("a", time.Millisecond*time.Duration(60))
|
|
|
|
strategy.PutResult("a", time.Millisecond*time.Duration(140))
|
|
|
|
// std 60
|
|
|
|
strategy.PutResult("b", time.Millisecond*time.Duration(40))
|
|
|
|
strategy.PutResult("b", time.Millisecond*time.Duration(160))
|
|
|
|
strategy.PutResult("b", time.Millisecond*time.Duration(40))
|
|
|
|
strategy.PutResult("b", time.Millisecond*time.Duration(160))
|
|
|
|
// std 0, but >MaxRTT
|
|
|
|
strategy.PutResult("c", time.Millisecond*time.Duration(1000))
|
|
|
|
strategy.PutResult("c", time.Millisecond*time.Duration(1000))
|
|
|
|
strategy.PutResult("c", time.Millisecond*time.Duration(1000))
|
|
|
|
strategy.PutResult("c", time.Millisecond*time.Duration(1000))
|
|
|
|
expected := "a"
|
|
|
|
actual := strategy.SelectAndPick([]string{"a", "b", "c", "untested"})
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("expected: %v, actual: %v", expected, actual)
|
|
|
|
}
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
|
|
|
|
2022-06-28 08:26:01 -04:00
|
|
|
func TestSelectLeastLoadWithCost(t *testing.T) {
|
|
|
|
settings := &StrategyLeastLoadConfig{
|
|
|
|
HealthCheck: &HealthPingConfig{
|
|
|
|
SamplingCount: 10,
|
|
|
|
},
|
|
|
|
Costs: []*StrategyWeight{
|
|
|
|
{Match: "a", Value: 9},
|
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
}
|
|
|
|
strategy := NewLeastLoadStrategy(settings, nil)
|
|
|
|
// std 40, std+c 120
|
|
|
|
strategy.PutResult("a", time.Millisecond*time.Duration(60))
|
|
|
|
strategy.PutResult("a", time.Millisecond*time.Duration(140))
|
|
|
|
strategy.PutResult("a", time.Millisecond*time.Duration(60))
|
|
|
|
strategy.PutResult("a", time.Millisecond*time.Duration(140))
|
|
|
|
// std 60
|
|
|
|
strategy.PutResult("b", time.Millisecond*time.Duration(40))
|
|
|
|
strategy.PutResult("b", time.Millisecond*time.Duration(160))
|
|
|
|
strategy.PutResult("b", time.Millisecond*time.Duration(40))
|
|
|
|
strategy.PutResult("b", time.Millisecond*time.Duration(160))
|
|
|
|
expected := "b"
|
|
|
|
actual := strategy.SelectAndPick([]string{"a", "b", "untested"})
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("expected: %v, actual: %v", expected, actual)
|
|
|
|
}
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
2021-06-18 19:02:46 -04:00
|
|
|
*/
|
2021-01-29 19:31:11 -05:00
|
|
|
func TestSelectLeastExpected(t *testing.T) {
|
|
|
|
strategy := &LeastLoadStrategy{
|
|
|
|
settings: &StrategyLeastLoadConfig{
|
|
|
|
Baselines: nil,
|
|
|
|
Expected: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nodes := []*node{
|
2021-06-18 19:02:46 -04:00
|
|
|
{Tag: "a", RTTDeviationCost: 100},
|
|
|
|
{Tag: "b", RTTDeviationCost: 200},
|
|
|
|
{Tag: "c", RTTDeviationCost: 300},
|
|
|
|
{Tag: "d", RTTDeviationCost: 350},
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
|
|
|
expected := 3
|
|
|
|
ns := strategy.selectLeastLoad(nodes)
|
|
|
|
if len(ns) != expected {
|
|
|
|
t.Errorf("expected: %v, actual: %v", expected, len(ns))
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 01:32:07 -05:00
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
func TestSelectLeastExpected2(t *testing.T) {
|
|
|
|
strategy := &LeastLoadStrategy{
|
|
|
|
settings: &StrategyLeastLoadConfig{
|
|
|
|
Baselines: nil,
|
|
|
|
Expected: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nodes := []*node{
|
2021-06-18 19:02:46 -04:00
|
|
|
{Tag: "a", RTTDeviationCost: 100},
|
|
|
|
{Tag: "b", RTTDeviationCost: 200},
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
|
|
|
expected := 2
|
|
|
|
ns := strategy.selectLeastLoad(nodes)
|
|
|
|
if len(ns) != expected {
|
|
|
|
t.Errorf("expected: %v, actual: %v", expected, len(ns))
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 01:32:07 -05:00
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
func TestSelectLeastExpectedAndBaselines(t *testing.T) {
|
|
|
|
strategy := &LeastLoadStrategy{
|
|
|
|
settings: &StrategyLeastLoadConfig{
|
|
|
|
Baselines: []int64{200, 300, 400},
|
|
|
|
Expected: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nodes := []*node{
|
2021-06-18 19:02:46 -04:00
|
|
|
{Tag: "a", RTTDeviationCost: 100},
|
|
|
|
{Tag: "b", RTTDeviationCost: 200},
|
|
|
|
{Tag: "c", RTTDeviationCost: 250},
|
|
|
|
{Tag: "d", RTTDeviationCost: 300},
|
|
|
|
{Tag: "e", RTTDeviationCost: 310},
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
2022-11-03 08:30:28 -04:00
|
|
|
expected := 3
|
2021-01-29 19:31:11 -05:00
|
|
|
ns := strategy.selectLeastLoad(nodes)
|
|
|
|
if len(ns) != expected {
|
|
|
|
t.Errorf("expected: %v, actual: %v", expected, len(ns))
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 01:32:07 -05:00
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
func TestSelectLeastExpectedAndBaselines2(t *testing.T) {
|
|
|
|
strategy := &LeastLoadStrategy{
|
|
|
|
settings: &StrategyLeastLoadConfig{
|
|
|
|
Baselines: []int64{200, 300, 400},
|
|
|
|
Expected: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nodes := []*node{
|
2021-06-18 19:02:46 -04:00
|
|
|
{Tag: "a", RTTDeviationCost: 500},
|
|
|
|
{Tag: "b", RTTDeviationCost: 600},
|
|
|
|
{Tag: "c", RTTDeviationCost: 700},
|
|
|
|
{Tag: "d", RTTDeviationCost: 800},
|
|
|
|
{Tag: "e", RTTDeviationCost: 900},
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
|
|
|
expected := 3
|
|
|
|
ns := strategy.selectLeastLoad(nodes)
|
|
|
|
if len(ns) != expected {
|
|
|
|
t.Errorf("expected: %v, actual: %v", expected, len(ns))
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 01:32:07 -05:00
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
func TestSelectLeastLoadBaselines(t *testing.T) {
|
|
|
|
strategy := &LeastLoadStrategy{
|
|
|
|
settings: &StrategyLeastLoadConfig{
|
|
|
|
Baselines: []int64{200, 400, 600},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nodes := []*node{
|
2021-06-18 19:02:46 -04:00
|
|
|
{Tag: "a", RTTDeviationCost: 100},
|
|
|
|
{Tag: "b", RTTDeviationCost: 200},
|
|
|
|
{Tag: "c", RTTDeviationCost: 300},
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
2022-11-03 08:30:28 -04:00
|
|
|
expected := 1
|
2021-01-29 19:31:11 -05:00
|
|
|
ns := strategy.selectLeastLoad(nodes)
|
|
|
|
if len(ns) != expected {
|
|
|
|
t.Errorf("expected: %v, actual: %v", expected, len(ns))
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 01:32:07 -05:00
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
func TestSelectLeastLoadBaselinesNoQualified(t *testing.T) {
|
|
|
|
strategy := &LeastLoadStrategy{
|
|
|
|
settings: &StrategyLeastLoadConfig{
|
|
|
|
Baselines: []int64{200, 400, 600},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nodes := []*node{
|
2021-06-18 19:02:46 -04:00
|
|
|
{Tag: "a", RTTDeviationCost: 800},
|
|
|
|
{Tag: "b", RTTDeviationCost: 1000},
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
|
|
|
expected := 0
|
|
|
|
ns := strategy.selectLeastLoad(nodes)
|
|
|
|
if len(ns) != expected {
|
|
|
|
t.Errorf("expected: %v, actual: %v", expected, len(ns))
|
|
|
|
}
|
|
|
|
}
|