github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/chain/handler_test.go (about)

     1  //@author wuyong
     2  //@date   2020/8/21
     3  //@desc
     4  
     5  package chain
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  type InitRoomActionCtx struct {
    15  	LessonId int64
    16  }
    17  
    18  func (m *InitRoomActionCtx) ValidateData() (ok bool) {
    19  	time.Sleep(3 * time.Second)
    20  	fmt.Println("validate data")
    21  
    22  	return true
    23  }
    24  func (m *InitRoomActionCtx) GetCurRoomInfos() (err error) {
    25  	fmt.Println("getCurRoomInfos ok")
    26  
    27  	return
    28  }
    29  
    30  func TestRunMethod(t *testing.T) {
    31  	item := &InitRoomActionCtx{}
    32  	handlerList := &Handlers{}
    33  	vf := reflect.ValueOf(item)
    34  	vft := vf.Type()
    35  	mNum := vf.NumMethod()
    36  	for i := 0; i < mNum; i++ {
    37  		mName := vft.Method(i).Name
    38  		reflectMethodValue := vf.Method(i)
    39  		if !IsNoInParamAndOnlyErrorOutParamFunc(reflectMethodValue.Type()) {
    40  			continue
    41  		}
    42  
    43  		if funcHandle, ok := vf.Method(i).Interface().(func() error); ok {
    44  			handlerList.AddHandler(NewHandler(false, mName, funcHandle))
    45  		}
    46  	}
    47  	err := handlerList.RunHandler()
    48  	t.Logf("linkProcess cost %s", handlerList.FormatCost())
    49  	if err != nil {
    50  		t.Errorf("err[%s]", err.Error())
    51  		return
    52  	}
    53  	t.Logf("ok")
    54  }
    55  
    56  // func () (err error) filter
    57  func IsNoInParamAndOnlyErrorOutParamFunc(reflectType reflect.Type) (ok bool) {
    58  	return reflectType.NumIn() == 0 && reflectType.NumOut() == 1 && reflectType.Out(0).String() == "error"
    59  }
    60  
    61  type GetRoomInfoActionCtx struct {
    62  	LessonId   int64
    63  	LiveRoomId int64
    64  }
    65  
    66  func (m *GetRoomInfoActionCtx) ValidateData() (ok bool) {
    67  	time.Sleep(3 * time.Second)
    68  	fmt.Println("validate data")
    69  
    70  	return true
    71  }
    72  func (m *GetRoomInfoActionCtx) GetCurRoomInfos1() (err error) {
    73  	fmt.Println("getCurRoomInfos1 ok")
    74  	time.Sleep(1 * time.Second)
    75  
    76  	return
    77  }
    78  func (m *GetRoomInfoActionCtx) GetCurRoomInfos2() (err error) {
    79  	fmt.Println("getCurRoomInfos2 ok")
    80  	time.Sleep(2 * time.Second)
    81  
    82  	return
    83  }
    84  func (m *GetRoomInfoActionCtx) GetCurRoomInfos3() (err error) {
    85  	fmt.Println("getCurRoomInfos3 ok")
    86  	time.Sleep(3 * time.Second)
    87  
    88  	return
    89  }
    90  
    91  func TestConcurrencyRunMethod(t *testing.T) {
    92  	item := &GetRoomInfoActionCtx{}
    93  	handlerList := &Handlers{}
    94  	vf := reflect.ValueOf(item)
    95  	vft := vf.Type()
    96  	mNum := vf.NumMethod()
    97  	for i := 0; i < mNum; i++ {
    98  		mName := vft.Method(i).Name
    99  		reflectMethodValue := vf.Method(i)
   100  		if !IsNoInParamAndOnlyErrorOutParamFunc(reflectMethodValue.Type()) {
   101  			continue
   102  		}
   103  
   104  		if funcHandle, ok := vf.Method(i).Interface().(func() error); ok {
   105  			handlerList.AddHandler(NewHandler(false, mName, funcHandle))
   106  		}
   107  	}
   108  
   109  	//concurHandlerList := NewConcurrencyHandlers(false, *handlerList)
   110  	concurHandlerList := NewConcurrencyHandlers(true, *handlerList)
   111  	err := concurHandlerList.RunHandler()
   112  	t.Logf("handlerProcess cost %s", concurHandlerList.FormatCost())
   113  	if err != nil {
   114  		t.Errorf("err[%s]", err.Error())
   115  		return
   116  	}
   117  	t.Logf("ok")
   118  }