-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathregistryrace_test.go
157 lines (130 loc) · 2.99 KB
/
registryrace_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
package circuit
import (
"math/rand"
"testing"
"time"
)
func TestRegistryFuzzy(t *testing.T) {
if testing.Short() {
t.Skip()
}
const (
hostCount = 1200
customSettingsCount = 120
concurrentRequests = 2048
requestDurationMean = 120 * time.Microsecond
requestDurationDeviation = 60 * time.Microsecond
idleTTL = time.Second
duration = 3 * time.Second
)
genHost := func() string {
const (
minHostLength = 12
maxHostLength = 36
)
h := make([]byte, minHostLength+rand.Intn(maxHostLength-minHostLength))
for i := range h {
h[i] = 'a' + byte(rand.Intn(int('z'+1-'a')))
}
return string(h)
}
hosts := make([]string, hostCount)
for i := range hosts {
hosts[i] = genHost()
}
settings := []BreakerSettings{{IdleTTL: idleTTL}}
settingsMap := make(map[string]BreakerSettings)
for _, h := range hosts {
s := BreakerSettings{
Host: h,
Type: ConsecutiveFailures,
Failures: 5,
IdleTTL: idleTTL,
}
settings = append(settings, s)
settingsMap[h] = s
}
r := NewRegistry(settings...)
// the first customSettingsCount hosts can have corresponding custom settings
customSettings := make(map[string]BreakerSettings)
for _, h := range hosts[:customSettingsCount] {
s := settingsMap[h]
s.Failures = 15
s.IdleTTL = idleTTL
customSettings[h] = s
}
var syncToken struct{}
sync := make(chan struct{}, 1)
sync <- syncToken
synced := func(f func()) {
t := <-sync
f()
sync <- t
}
replaceHostSettings := func(settings map[string]BreakerSettings, old, nu string) {
if s, ok := settings[old]; ok {
delete(settings, old)
s.Host = nu
settings[nu] = s
}
}
replaceHost := func() {
synced(func() {
i := rand.Intn(len(hosts))
old := hosts[i]
nu := genHost()
hosts[i] = nu
replaceHostSettings(settingsMap, old, nu)
replaceHostSettings(customSettings, old, nu)
})
}
stop := make(chan struct{})
getSettings := func(useCustom bool) BreakerSettings {
var s BreakerSettings
synced(func() {
if useCustom {
s = customSettings[hosts[rand.Intn(customSettingsCount)]]
return
}
s = settingsMap[hosts[rand.Intn(hostCount)]]
})
return s
}
requestDuration := func() time.Duration {
mean := float64(requestDurationMean)
deviation := float64(requestDurationDeviation)
return time.Duration(rand.NormFloat64()*deviation + mean)
}
makeRequest := func(useCustom bool) {
s := getSettings(useCustom)
b := r.Get(s)
if b.settings != s {
t.Error("invalid breaker received")
t.Log(b.settings, s)
close(stop)
}
time.Sleep(requestDuration())
}
runAgent := func() {
for {
select {
case <-stop:
return
default:
}
// 1% percent chance for getting a host replaced:
if rand.Intn(100) == 0 {
replaceHost()
}
// 3% percent of the requests is custom:
makeRequest(rand.Intn(100) < 3)
}
}
time.AfterFunc(duration, func() {
close(stop)
})
for i := 0; i < concurrentRequests; i++ {
go runAgent()
}
<-stop
}