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