github.com/wfusion/gofusion@v1.1.14/test/cron/test.go (about)

     1  package cron
     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 = "cron"
    19  )
    20  
    21  type Test struct {
    22  	test.Suite
    23  
    24  	once  sync.Once
    25  	exits []func()
    26  
    27  	testName   string
    28  	testsLefts atomic.Int64
    29  }
    30  
    31  func (t *Test) SetupTest() {
    32  	t.Catch(func() {
    33  		log.Info(context.Background(), fmt.Sprintf("------------ %s test case begin ------------", component))
    34  
    35  		t.once.Do(func() {
    36  			t.exits = append(t.exits, t.Suite.Copy(t.ConfigFiles(), t.testName, 1))
    37  		})
    38  
    39  		t.exits = append(t.exits, t.Suite.Init(t.ConfigFiles(), t.testName, 1))
    40  	})
    41  }
    42  
    43  func (t *Test) TearDownTest() {
    44  	t.Catch(func() {
    45  		log.Info(context.Background(), fmt.Sprintf("------------ %s test case end ------------", component))
    46  		if t.testsLefts.Add(-1) == 0 {
    47  			for i := len(t.exits) - 1; i >= 0; i-- {
    48  				t.exits[i]()
    49  			}
    50  		}
    51  	})
    52  }
    53  
    54  func (t *Test) AppName() string {
    55  	return fmt.Sprintf("%s.%s", component, t.testName)
    56  }
    57  
    58  func (t *Test) Init(testingSuite suite.TestingSuite) {
    59  	methodFinder := reflect.TypeOf(testingSuite)
    60  	numMethod := methodFinder.NumMethod()
    61  
    62  	numTestLeft := int64(0)
    63  	for i := 0; i < numMethod; i++ {
    64  		method := methodFinder.Method(i)
    65  		ok, _ := test.MethodFilter(method.Name)
    66  		if !ok {
    67  			continue
    68  		}
    69  		numTestLeft++
    70  	}
    71  	t.testName = utils.IndirectType(methodFinder).Name()
    72  	t.testsLefts.Add(numTestLeft)
    73  }