github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/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 func TestSelectLeastExpected2(t *testing.T) { 89 strategy := &LeastLoadStrategy{ 90 settings: &StrategyLeastLoadConfig{ 91 Baselines: nil, 92 Expected: 3, 93 }, 94 } 95 nodes := []*node{ 96 {Tag: "a", RTTDeviationCost: 100}, 97 {Tag: "b", RTTDeviationCost: 200}, 98 } 99 expected := 2 100 ns := strategy.selectLeastLoad(nodes) 101 if len(ns) != expected { 102 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 103 } 104 } 105 func TestSelectLeastExpectedAndBaselines(t *testing.T) { 106 strategy := &LeastLoadStrategy{ 107 settings: &StrategyLeastLoadConfig{ 108 Baselines: []int64{200, 300, 400}, 109 Expected: 3, 110 }, 111 } 112 nodes := []*node{ 113 {Tag: "a", RTTDeviationCost: 100}, 114 {Tag: "b", RTTDeviationCost: 200}, 115 {Tag: "c", RTTDeviationCost: 250}, 116 {Tag: "d", RTTDeviationCost: 300}, 117 {Tag: "e", RTTDeviationCost: 310}, 118 } 119 expected := 3 120 ns := strategy.selectLeastLoad(nodes) 121 if len(ns) != expected { 122 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 123 } 124 } 125 func TestSelectLeastExpectedAndBaselines2(t *testing.T) { 126 strategy := &LeastLoadStrategy{ 127 settings: &StrategyLeastLoadConfig{ 128 Baselines: []int64{200, 300, 400}, 129 Expected: 3, 130 }, 131 } 132 nodes := []*node{ 133 {Tag: "a", RTTDeviationCost: 500}, 134 {Tag: "b", RTTDeviationCost: 600}, 135 {Tag: "c", RTTDeviationCost: 700}, 136 {Tag: "d", RTTDeviationCost: 800}, 137 {Tag: "e", RTTDeviationCost: 900}, 138 } 139 expected := 3 140 ns := strategy.selectLeastLoad(nodes) 141 if len(ns) != expected { 142 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 143 } 144 } 145 func TestSelectLeastLoadBaselines(t *testing.T) { 146 strategy := &LeastLoadStrategy{ 147 settings: &StrategyLeastLoadConfig{ 148 Baselines: []int64{200, 400, 600}, 149 Expected: 0, 150 }, 151 } 152 nodes := []*node{ 153 {Tag: "a", RTTDeviationCost: 100}, 154 {Tag: "b", RTTDeviationCost: 200}, 155 {Tag: "c", RTTDeviationCost: 300}, 156 } 157 expected := 1 158 ns := strategy.selectLeastLoad(nodes) 159 if len(ns) != expected { 160 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 161 } 162 } 163 func TestSelectLeastLoadBaselinesNoQualified(t *testing.T) { 164 strategy := &LeastLoadStrategy{ 165 settings: &StrategyLeastLoadConfig{ 166 Baselines: []int64{200, 400, 600}, 167 Expected: 0, 168 }, 169 } 170 nodes := []*node{ 171 {Tag: "a", RTTDeviationCost: 800}, 172 {Tag: "b", RTTDeviationCost: 1000}, 173 } 174 expected := 0 175 ns := strategy.selectLeastLoad(nodes) 176 if len(ns) != expected { 177 t.Errorf("expected: %v, actual: %v", expected, len(ns)) 178 } 179 }