-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhystrix_test.go
141 lines (128 loc) · 4.25 KB
/
hystrix_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
package gohttpclient
import (
"bytes"
"io"
"net/http"
"net/url"
"testing"
"time"
"github.com/cep21/circuit"
"github.com/cep21/circuit/closers/hystrix"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
func TestHystrixHandler(t *testing.T) { //revive:disable:cyclomatic
// Test requests for the same domain name, first succeed 50 times,
// then error 50 times, repeat 3 times
option := NewHystrixOption()
option.CircuitManager = getTestCircuitManager()
handler := HystrixHandler(option)
requestTimes := 0
errResponseTimes := errors.New("requestTimes error")
handlerFunc := func(req *http.Request) (resp *http.Response, err error) {
requestTimes++
ok := (requestTimes >= 1 && requestTimes <= 50) ||
(requestTimes >= 101 && requestTimes <= 150) ||
(requestTimes >= 201 && requestTimes <= 250)
if !ok {
return nil, errResponseTimes
}
return &http.Response{
Body: io.NopCloser(bytes.NewBufferString("hello world")),
}, nil
}
req, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
for i := 1; i <= 119; i++ {
resp, err := handler(req, handlerFunc)
if i >= 1 && i <= 50 {
require.Nilf(t, err, "#%d", i)
require.NotNilf(t, resp, "#%d", i)
} else if i >= 51 && i <= 100 {
require.Equalf(t, errResponseTimes, err, "#%d", i)
require.Nilf(t, resp, "#%d", i)
} else if i >= 101 && i <= 119 {
require.NotNilf(t, err, "#%d", i)
require.Equalf(t, "circuit is open: concurrencyReached=false circuitOpen=true", err.Error(), "#%d", i)
require.Nilf(t, resp, "#%d", i)
// After the open circuit, there is no actual response, but the number of requests should be accumulated
requestTimes++
}
}
time.Sleep(500 * time.Millisecond)
for i := 120; i <= 300; i++ {
resp, err := handler(req, handlerFunc)
if i == 120 {
// This time the request is attempted, and the circuit breaker is closed when successful.
// However, this request does not record the total number of successful requests
// and clears the number of incorrect requests and successful requests
require.Nilf(t, err, "#%d", i)
require.NotNilf(t, resp, "#%d", i)
} else if i >= 121 && i <= 150 {
require.Nilf(t, err, "#%d", i)
require.NotNilf(t, resp, "#%d", i)
} else if i >= 151 && i <= 180 {
require.Equalf(t, errResponseTimes, err, "#%d", i)
require.Nilf(t, resp, "#%d", i)
} else if i >= 180 && i <= 300 {
require.NotNilf(t, err, "#%d", i)
require.Equalf(t, "circuit is open: concurrencyReached=false circuitOpen=true", err.Error(), "#%d", i)
require.Nilf(t, resp, "#%d", i)
// After the open circuit, there is no actual response, but the number of requests should be accumulated
requestTimes++
}
}
}
func TestGetURLStringEndWithHost(t *testing.T) {
cases := []struct {
Input string
Output string
}{
{"", ""},
{"https://examples.com/healthz?a=b", "https://examples.com"},
{"https://examples.com/healthz?a=b#pointer", "https://examples.com"},
{"https://[email protected]/healthz?a=b", "https://examples.com"},
}
for _, c := range cases {
u, err := url.Parse(c.Input)
require.Nil(t, err)
result := getURLStringEndWithHost(u)
require.Equal(t, c.Output, result)
}
}
func getTestCircuitManager() *circuit.Manager {
var defaultHystrixFactory = hystrix.Factory{
ConfigureOpener: hystrix.ConfigureOpener{
RequestVolumeThreshold: 20,
ErrorThresholdPercentage: 50,
},
ConfigureCloser: hystrix.ConfigureCloser{
SleepWindow: 300 * time.Millisecond,
HalfOpenAttempts: 1,
RequiredConcurrentSuccessful: 1,
},
}
var defaultCircuitManager = &circuit.Manager{
DefaultCircuitProperties: []circuit.CommandPropertiesConstructor{
defaultHystrixFactory.Configure,
func(_circuitName string) circuit.Config {
return circuit.Config{
General: circuit.GeneralConfig{
GoLostErrors: func(err error, panics interface{}) {
logrus.WithError(err).WithField("panic", panics).Warn("gohttpclient hystrix lost errros")
},
},
Execution: circuit.ExecutionConfig{
Timeout: -1,
MaxConcurrentRequests: -1,
},
Fallback: circuit.FallbackConfig{
MaxConcurrentRequests: -1,
},
Metrics: circuit.MetricsCollectors{},
}
},
},
}
return defaultCircuitManager
}