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:
Piotr Tabor
2020-10-01 16:03:05 +02:00
parent 0e5d81704f
commit 528f5315d6
8 changed files with 50 additions and 39 deletions

View File

@@ -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)
}