-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* shortcheck: run with -race Signed-off-by: Alexey Ermakov <[email protected]> * clone routes in the file watch data client as it is storing the same instances that it passes to the routing Signed-off-by: Arpad Ryszka <[email protected]> * add missing read lock and wait for the refresh goroutine to finish before test assertion Signed-off-by: Arpad Ryszka <[email protected]> * fix race in test code with legacy table tests Signed-off-by: Arpad Ryszka <[email protected]> * fix tests for audit logging during upgrade requests and connection checking during roundtripper retry Signed-off-by: Arpad Ryszka <[email protected]> * fix access log time sensitivity during testing with race Signed-off-by: Arpad Ryszka <[email protected]> * update timeout values for breaker tests for race testing Signed-off-by: Arpad Ryszka <[email protected]> * remove some time sensitive tests from under -race testing Signed-off-by: Arpad Ryszka <[email protected]> * fix race condition in test data client Signed-off-by: Arpad Ryszka <[email protected]> * adjust or disable some remaining time sensitive tests Signed-off-by: Arpad Ryszka <[email protected]> * exclude test writing to global var from race detection tests Signed-off-by: Arpad Ryszka <[email protected]> * split more time sensitive tests from race detection Signed-off-by: Arpad Ryszka <[email protected]> * split -race testing from shortcheck because some of the tests are not executed now with -race enabled Signed-off-by: Arpad Ryszka <[email protected]>
- Loading branch information
1 parent
4e2b8e7
commit 7b3f470
Showing
28 changed files
with
602 additions
and
415 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// +build !race | ||
|
||
package routestring_test | ||
|
||
import ( | ||
|
Oops, something went wrong.