Change how tearDownAllSuite is run to aviod deadlock when running with failfast

pull/943/head
ariley 2020-04-20 20:16:18 +01:00 committed by Boyan Soubachov
parent f7ef284eb4
commit 097ec799df
1 changed files with 17 additions and 19 deletions

View File

@ -7,7 +7,6 @@ import (
"reflect" "reflect"
"regexp" "regexp"
"runtime/debug" "runtime/debug"
"sync"
"testing" "testing"
"time" "time"
@ -84,7 +83,6 @@ func (suite *Suite) Run(name string, subtest func()) bool {
func Run(t *testing.T, suite TestingSuite) { func Run(t *testing.T, suite TestingSuite) {
defer failOnPanic(t) defer failOnPanic(t)
testsSync := &sync.WaitGroup{}
suite.SetT(t) suite.SetT(t)
var suiteSetupDone bool var suiteSetupDone bool
@ -96,6 +94,7 @@ func Run(t *testing.T, suite TestingSuite) {
tests := []testing.InternalTest{} tests := []testing.InternalTest{}
methodFinder := reflect.TypeOf(suite) methodFinder := reflect.TypeOf(suite)
suiteName := methodFinder.Elem().Name()
for i := 0; i < methodFinder.NumMethod(); i++ { for i := 0; i < methodFinder.NumMethod(); i++ {
method := methodFinder.Method(i) method := methodFinder.Method(i)
@ -110,35 +109,21 @@ func Run(t *testing.T, suite TestingSuite) {
continue continue
} }
suiteName := methodFinder.Elem().Name()
if !suiteSetupDone { if !suiteSetupDone {
if stats != nil { if stats != nil {
stats.Start = time.Now() stats.Start = time.Now()
} }
defer func() {
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
testsSync.Wait()
tearDownAllSuite.TearDownSuite()
}
if suiteWithStats, measureStats := suite.(WithStats); measureStats {
stats.End = time.Now()
suiteWithStats.HandleStats(suiteName, stats)
}
}()
if setupAllSuite, ok := suite.(SetupAllSuite); ok { if setupAllSuite, ok := suite.(SetupAllSuite); ok {
setupAllSuite.SetupSuite() setupAllSuite.SetupSuite()
} }
suiteSetupDone = true suiteSetupDone = true
} }
test := testing.InternalTest{ test := testing.InternalTest{
Name: method.Name, Name: method.Name,
F: func(t *testing.T) { F: func(t *testing.T) {
defer testsSync.Done()
parentT := suite.T() parentT := suite.T()
suite.SetT(t) suite.SetT(t)
defer failOnPanic(t) defer failOnPanic(t)
@ -174,8 +159,21 @@ func Run(t *testing.T, suite TestingSuite) {
}, },
} }
tests = append(tests, test) tests = append(tests, test)
testsSync.Add(1)
} }
if suiteSetupDone {
defer func() {
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
tearDownAllSuite.TearDownSuite()
}
if suiteWithStats, measureStats := suite.(WithStats); measureStats {
stats.End = time.Now()
suiteWithStats.HandleStats(suiteName, stats)
}
}()
}
runTests(t, tests) runTests(t, tests)
} }