github.com/wfusion/gofusion@v1.1.14/test/cache/test.go (about) 1 package cache 2 3 import ( 4 "context" 5 "fmt" 6 "reflect" 7 "sync" 8 9 "github.com/stretchr/testify/suite" 10 "go.uber.org/atomic" 11 12 "github.com/wfusion/gofusion/common/utils" 13 "github.com/wfusion/gofusion/log" 14 "github.com/wfusion/gofusion/test" 15 ) 16 17 var ( 18 component = "cache" 19 ) 20 21 type Test struct { 22 test.Suite 23 24 once sync.Once 25 exits []func() 26 testName string 27 testsLefts atomic.Int64 28 } 29 30 func (t *Test) SetupTest() { 31 t.Catch(func() { 32 log.Info(context.Background(), fmt.Sprintf("------------ %s test case begin ------------", component)) 33 34 t.once.Do(func() { 35 t.exits = append(t.exits, t.Suite.Copy(t.ConfigFiles(), t.testName, 1)) 36 }) 37 38 t.exits = append(t.exits, t.Suite.Init(t.ConfigFiles(), t.testName, 1)) 39 }) 40 } 41 42 func (t *Test) TearDownTest() { 43 t.Catch(func() { 44 log.Info(context.Background(), fmt.Sprintf("------------ %s test case end ------------", component)) 45 if t.testsLefts.Add(-1) == 0 { 46 for i := len(t.exits) - 1; i >= 0; i-- { 47 t.exits[i]() 48 } 49 } 50 }) 51 } 52 53 func (t *Test) AppName() string { 54 return fmt.Sprintf("%s.%s", component, t.testName) 55 } 56 57 func (t *Test) Init(testingSuite suite.TestingSuite) { 58 methodFinder := reflect.TypeOf(testingSuite) 59 numMethod := methodFinder.NumMethod() 60 61 numTestLeft := int64(0) 62 for i := 0; i < numMethod; i++ { 63 method := methodFinder.Method(i) 64 ok, _ := test.MethodFilter(method.Name) 65 if !ok { 66 continue 67 } 68 numTestLeft++ 69 } 70 t.testName = utils.IndirectType(methodFinder).Name() 71 t.testsLefts.Add(numTestLeft) 72 }