-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy_least_rseponse_time_test.go
177 lines (160 loc) Β· 4.59 KB
/
strategy_least_rseponse_time_test.go
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
package main
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestLeastResponseTimeMovingAverageCalculator(t *testing.T) {
tests := []struct {
name string
windowSize int
inputs []time.Duration
expected time.Duration
}{
{
name: "moving average with 3 inputs",
windowSize: 3,
inputs: []time.Duration{100 * time.Millisecond, 200 * time.Millisecond, 300 * time.Millisecond},
expected: 200 * time.Millisecond,
},
{
name: "moving average with more inputs than window",
windowSize: 3,
inputs: []time.Duration{
100 * time.Millisecond,
200 * time.Millisecond,
300 * time.Millisecond,
400 * time.Millisecond,
},
expected: 300 * time.Millisecond, // Last 3 inputs: (200+300+400)/3
},
{
name: "moving average with single input",
windowSize: 1,
inputs: []time.Duration{150 * time.Millisecond},
expected: 150 * time.Millisecond,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
calculator := LeastResponseTimeMovingAverageCalculator(tt.windowSize)
var result time.Duration
for _, input := range tt.inputs {
result = calculator(input)
}
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}
func TestLeastResponseTimeWeightedAverageCalculator(t *testing.T) {
tests := []struct {
name string
weight float64
inputs []time.Duration
expected string
}{
{
name: "weighted average with multiple inputs",
weight: 0.8,
inputs: []time.Duration{
100 * time.Millisecond, // Initial value
200 * time.Millisecond, // New average: (0.8*200) + (0.2*100) = 180
150 * time.Millisecond, // New average: (0.8*150) + (0.2*180) = 156
50 * time.Millisecond, // New average: (0.8*50) + (0.2*156) = 71.2
},
expected: "71.2ms",
},
{
name: "weighted average with single input",
weight: 0.5,
inputs: []time.Duration{120 * time.Millisecond},
expected: "120ms",
},
{
name: "high weight favors recent inputs",
weight: 0.9,
inputs: []time.Duration{
100 * time.Millisecond, // Initial value
200 * time.Millisecond, // New average: (0.9*200) + (0.1*100) = 190
300 * time.Millisecond, // New average: (0.9*300) + (0.1*190) = 289
},
expected: "289ms",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
calculator := LeastResponseTimeWeightedAverageCalculator(tt.weight)
var result time.Duration
for _, input := range tt.inputs {
result = calculator(input)
}
if result.String() != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}
// mockLeastResponseTimeTransport simulates a RoundTripper with configurable delay.
type mockLeastResponseTimeTransport struct {
delay time.Duration
ID int
}
func (m *mockLeastResponseTimeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
time.Sleep(m.delay)
return &http.Response{StatusCode: 200}, nil
}
func TestLeastResponseTimeStrategy(t *testing.T) {
tests := []struct {
name string
transports []http.RoundTripper
calculator ResponseTimeCalculator
mockDelays []time.Duration
expectedID int
}{
{
name: "select fastest transport with least response time",
transports: []http.RoundTripper{
&mockLeastResponseTimeTransport{ID: 1, delay: 100 * time.Millisecond},
&mockLeastResponseTimeTransport{ID: 2, delay: 50 * time.Millisecond},
&mockLeastResponseTimeTransport{ID: 3, delay: 150 * time.Millisecond},
},
calculator: LeastResponseTimeLastResponseTimeCalculator,
mockDelays: []time.Duration{
100 * time.Millisecond,
50 * time.Millisecond,
150 * time.Millisecond,
},
expectedID: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
strat := NewLeastResponseTimeStrategy(tt.transports, time.Now, tt.calculator)
for _, transport := range strat.transports {
// Simulate a request to calculate response times
req, _ := http.NewRequest("GET", "https://example.com", nil)
_, err := transport.RoundTrip(req)
if err != nil {
t.Fatalf("unexpected error during RoundTrip: %v", err)
}
}
// Acquire the best transport
selectedTransport, err := strat.Acquire()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tp, ok := selectedTransport.(*leastResponseTimeRoundTripper)
if !ok {
t.Fatalf("unexpected type for transport")
}
mockTp, ok := tp.Unwrap().(*mockLeastResponseTimeTransport)
if !ok {
t.Fatalf("unexpected type for transport")
}
assert.Equal(t, tt.expectedID, mockTp.ID)
})
}
}