-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathfailover_test.go
More file actions
120 lines (100 loc) · 3.59 KB
/
Copy pathfailover_test.go
File metadata and controls
120 lines (100 loc) · 3.59 KB
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
package unleash
import (
"fmt"
"testing"
"time"
)
func TestFailoverStrategy_ServerHintPollingFailsOver(t *testing.T) {
strategy := newFailoverStrategy(3, 10*time.Second)
now := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
event := &failoverRequest{
baseFailEvent: baseFailEvent{
occurredAt: now,
message: "Unleash has requested failover to polling",
},
}
if got := strategy.shouldFailover(event, now); !got {
t.Fatalf("expected failover on server polling hint")
}
}
func TestFailoverStrategy_HardHttpStatusCodesFailoverImmediately(t *testing.T) {
strategy := newFailoverStrategy(3, 10*time.Second)
now := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
for _, statusCode := range []int{401, 403, 404, 429} {
event := &httpStatusErrorEvent{
baseFailEvent: baseFailEvent{
occurredAt: now,
message: fmt.Sprintf("HTTP status error with status code %d", statusCode),
},
statusCode: statusCode,
}
if got := strategy.shouldFailover(event, now); !got {
t.Fatalf("expected failover on hard status code %d", statusCode)
}
}
}
func TestFailoverStrategy_SoftErrorsAccumulateWithinRelaxWindow(t *testing.T) {
strategy := newFailoverStrategy(3, 60*time.Second)
base := time.Date(1867, 11, 7, 0, 0, 0, 0, time.UTC)
mkEvent := func(offsetMs int) *httpStatusErrorEvent {
occurredAt := base.Add(time.Duration(offsetMs) * time.Millisecond)
return &httpStatusErrorEvent{
baseFailEvent: baseFailEvent{
occurredAt: occurredAt,
message: "HTTP status error with status code 503",
},
statusCode: 503,
}
}
if got := strategy.shouldFailover(mkEvent(0), base); got {
t.Fatalf("expected no failover on first soft error")
}
if got := strategy.shouldFailover(mkEvent(1000), base.Add(1000*time.Millisecond)); got {
t.Fatalf("expected no failover on second soft error")
}
if got := strategy.shouldFailover(mkEvent(2000), base.Add(2000*time.Millisecond)); !got {
t.Fatalf("expected failover on third soft error within relax window")
}
}
func TestFailoverStrategy_SoftErrorsPrunedByWindow(t *testing.T) {
strategy := newFailoverStrategy(3, 1*time.Second)
base := time.Date(1867, 11, 7, 0, 0, 0, 0, time.UTC)
mkEvent := func(offsetMs int) *httpStatusErrorEvent {
occurredAt := base.Add(time.Duration(offsetMs) * time.Millisecond)
return &httpStatusErrorEvent{
baseFailEvent: baseFailEvent{
occurredAt: occurredAt,
message: "HTTP status error with status code 502",
},
statusCode: 502,
}
}
if got := strategy.shouldFailover(mkEvent(0), base); got {
t.Fatalf("expected no failover on first soft error")
}
if got := strategy.shouldFailover(mkEvent(2000), base.Add(2000*time.Millisecond)); got {
t.Fatalf("expected no failover on second soft error after window")
}
if got := strategy.shouldFailover(mkEvent(3000), base.Add(3000*time.Millisecond)); got {
t.Fatalf("expected no failover on third soft error after pruning")
}
}
func TestFailoverStrategy_UnhandledStatusCodesNeverFailover(t *testing.T) {
strategy := newFailoverStrategy(1, 10*time.Second)
base := time.Date(1867, 11, 7, 0, 0, 0, 0, time.UTC)
mkEvent := func(code int, offsetMs int) *httpStatusErrorEvent {
occurredAt := base.Add(time.Duration(offsetMs) * time.Millisecond)
return &httpStatusErrorEvent{
baseFailEvent: baseFailEvent{
occurredAt: occurredAt,
message: fmt.Sprintf("HTTP status error with status code %d", code),
},
statusCode: code,
}
}
for _, statusCode := range []int{418, 418, 418, 418} {
if got := strategy.shouldFailover(mkEvent(statusCode, 0), base); got {
t.Fatalf("expected no failover on unhandled status code %d", statusCode)
}
}
}