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

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