github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/app/router/strategy_leastload_test.go (about) 1 package router 2 3 import ( 4 "testing" 5 ) 6 7 /* 8 Split into multiple package, need to be tested separately 9 10 func TestSelectLeastLoad(t *testing.T) { 11 settings := &StrategyLeastLoadConfig{ 12 HealthCheck: &HealthPingConfig{ 13 SamplingCount: 10, 14 }, 15 Expected: 1, 16 MaxRTT: int64(time.Millisecond * time.Duration(800)), 17 } 18 strategy := NewLeastLoadStrategy(settings) 19 // std 40 20 strategy.PutResult("a", time.Millisecond*time.Duration(60)) 21 strategy.PutResult("a", time.Millisecond*time.Duration(140)) 22 strategy.PutResult("a", time.Millisecond*time.Duration(60)) 23 strategy.PutResult("a", time.Millisecond*time.Duration(140)) 24 // std 60 25 strategy.PutResult("b", time.Millisecond*time.Duration(40)) 26 strategy.PutResult("b", time.Millisecond*time.Duration(160)) 27 strategy.PutResult("b", time.Millisecond*time.Duration(40)) 28 strategy.PutResult("b", time.Millisecond*time.Duration(160)) 29 // std 0, but >MaxRTT 30 strategy.PutResult("c", time.Millisecond*time.Duration(1000)) 31 strategy.PutResult("c", time.Millisecond*time.Duration(1000)) 32 strategy.PutResult("c", time.Millisecond*time.Duration(1000)) 33 strategy.PutResult("c", time.Millisecond*time.Duration(1000)) 34 expected := "a" 35 actual := strategy.SelectAndPick([]string{"a", "b", "c", "untested"}) 36 if actual != expected { 37 t.Errorf("expected: %v, actual: %v", expected, actual) 38 } 39 } 40 41 func TestSelectLeastLoadWithCost(t *testing.T) { 42 settings := &StrategyLeastLoadConfig{ 43 HealthCheck: &HealthPingConfig{ 44 SamplingCount: 10, 45 }, 46 Costs: []*StrategyWeight{ 47 {Match: "a", Value: 9}, 48 }, 49 Expected: 1, 50 } 51 strategy := NewLeastLoadStrategy(settings, nil) 52 // std 40, std+c 120 53 strategy.PutResult("a", time.Millisecond*time.Duration(60)) 54 strategy.PutResult("a", time.Millisecond*time.Duration(140)) 55 strategy.PutResult("a", time.Millisecond*time.Duration(60)) 56 strategy.PutResult("a", time.Millisecond*time.Duration(140)) 57 // std 60 58 strategy.PutResult("b", time.Millisecond*time.Duration(40)) 59 strategy.PutResult("b", time.Millisecond*time.Duration(160)) 60 strategy.PutResult("b", time.Millisecond*time.Duration(40)) 61 strategy.PutResult("b", time.Millisecond*time.Duration(160)) 62 expected := "b" 63 actual := strategy.SelectAndPick([]string{"a", "b", "untested"}) 64 if actual != expected { 65 t.Errorf("expected: %v, actual: %v", expected, actual) 66 } 67 } 68 */ 69 func TestSelectLeastExpected(t *testing.T) { 70 strategy := &LeastLoadStrategy{ 71 settings: &StrategyLeastLoadConfig{ 72 Baselines: nil, 73 Expected: 3, 74 }, 75 } 76 nodes := []*node{ 77 {Tag: "a", RTTDeviationCost: 100}, 78 {Tag: "b", RTTDeviationCost: 200}, 79 {Tag: "c", RTTDeviationCost: 300}, 80 {Tag: "d", RTTDeviationCost: 350}, 81 } 82 expected := 3 83 ns := strategy.selectLeastLoad(nodes) 84 if len(ns) != expected { 85 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 86 } 87 } 88 89 func TestSelectLeastExpected2(t *testing.T) { 90 strategy := &LeastLoadStrategy{ 91 settings: &StrategyLeastLoadConfig{ 92 Baselines: nil, 93 Expected: 3, 94 }, 95 } 96 nodes := []*node{ 97 {Tag: "a", RTTDeviationCost: 100}, 98 {Tag: "b", RTTDeviationCost: 200}, 99 } 100 expected := 2 101 ns := strategy.selectLeastLoad(nodes) 102 if len(ns) != expected { 103 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 104 } 105 } 106 107 func TestSelectLeastExpectedAndBaselines(t *testing.T) { 108 strategy := &LeastLoadStrategy{ 109 settings: &StrategyLeastLoadConfig{ 110 Baselines: []int64{200, 300, 400}, 111 Expected: 3, 112 }, 113 } 114 nodes := []*node{ 115 {Tag: "a", RTTDeviationCost: 100}, 116 {Tag: "b", RTTDeviationCost: 200}, 117 {Tag: "c", RTTDeviationCost: 250}, 118 {Tag: "d", RTTDeviationCost: 300}, 119 {Tag: "e", RTTDeviationCost: 310}, 120 } 121 expected := 3 122 ns := strategy.selectLeastLoad(nodes) 123 if len(ns) != expected { 124 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 125 } 126 } 127 128 func TestSelectLeastExpectedAndBaselines2(t *testing.T) { 129 strategy := &LeastLoadStrategy{ 130 settings: &StrategyLeastLoadConfig{ 131 Baselines: []int64{200, 300, 400}, 132 Expected: 3, 133 }, 134 } 135 nodes := []*node{ 136 {Tag: "a", RTTDeviationCost: 500}, 137 {Tag: "b", RTTDeviationCost: 600}, 138 {Tag: "c", RTTDeviationCost: 700}, 139 {Tag: "d", RTTDeviationCost: 800}, 140 {Tag: "e", RTTDeviationCost: 900}, 141 } 142 expected := 3 143 ns := strategy.selectLeastLoad(nodes) 144 if len(ns) != expected { 145 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 146 } 147 } 148 149 func TestSelectLeastLoadBaselines(t *testing.T) { 150 strategy := &LeastLoadStrategy{ 151 settings: &StrategyLeastLoadConfig{ 152 Baselines: []int64{200, 400, 600}, 153 Expected: 0, 154 }, 155 } 156 nodes := []*node{ 157 {Tag: "a", RTTDeviationCost: 100}, 158 {Tag: "b", RTTDeviationCost: 200}, 159 {Tag: "c", RTTDeviationCost: 300}, 160 } 161 expected := 1 162 ns := strategy.selectLeastLoad(nodes) 163 if len(ns) != expected { 164 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 165 } 166 } 167 168 func TestSelectLeastLoadBaselinesNoQualified(t *testing.T) { 169 strategy := &LeastLoadStrategy{ 170 settings: &StrategyLeastLoadConfig{ 171 Baselines: []int64{200, 400, 600}, 172 Expected: 0, 173 }, 174 } 175 nodes := []*node{ 176 {Tag: "a", RTTDeviationCost: 800}, 177 {Tag: "b", RTTDeviationCost: 1000}, 178 } 179 expected := 0 180 ns := strategy.selectLeastLoad(nodes) 181 if len(ns) != expected { 182 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 183 } 184 }