mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
auth: Fix "race" - auth unit tests leaking goroutines
- We were leaking goroutines in auth-test - The go-routines were depending / modifying global test environment variables (simpleTokenTTLDefault) leading to races Removed the leaked go-routines, and expanded 'auth' package to be covered we leaked go-routines detection.
This commit is contained in:
@@ -24,11 +24,7 @@ running(leaking) after all tests.
|
||||
import "go.etcd.io/etcd/v3/pkg/testutil"
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
v := m.Run()
|
||||
if v == 0 && testutil.CheckLeakedGoroutine() {
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(v)
|
||||
testutil.MustTestMainWithLeakDetection(m)
|
||||
}
|
||||
|
||||
func TestSample(t *testing.T) {
|
||||
@@ -38,10 +34,6 @@ running(leaking) after all tests.
|
||||
|
||||
*/
|
||||
func CheckLeakedGoroutine() bool {
|
||||
if testing.Short() {
|
||||
// not counting goroutines for leakage in -short mode
|
||||
return false
|
||||
}
|
||||
gs := interestingGoroutines()
|
||||
if len(gs) == 0 {
|
||||
return false
|
||||
@@ -66,9 +58,6 @@ func CheckLeakedGoroutine() bool {
|
||||
// Waits for go-routines shutdown for 'd'.
|
||||
func CheckAfterTest(d time.Duration) error {
|
||||
http.DefaultTransport.(*http.Transport).CloseIdleConnections()
|
||||
if testing.Short() {
|
||||
return nil
|
||||
}
|
||||
var bad string
|
||||
badSubstring := map[string]string{
|
||||
").writeLoop(": "a Transport",
|
||||
@@ -140,3 +129,19 @@ func interestingGoroutines() (gs []string) {
|
||||
sort.Strings(gs)
|
||||
return gs
|
||||
}
|
||||
|
||||
// MustTestMainWithLeakDetection expands standard m.Run with leaked
|
||||
// goroutines detection.
|
||||
func MustTestMainWithLeakDetection(m *testing.M) {
|
||||
v := m.Run()
|
||||
|
||||
http.DefaultTransport.(*http.Transport).CloseIdleConnections()
|
||||
|
||||
// Let the other goroutines finalize.
|
||||
runtime.Gosched()
|
||||
|
||||
if v == 0 && CheckLeakedGoroutine() {
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user