github.com/tencent/goom@v1.0.1/internal/proxy/func_data_test.go (about)

     1  // Package proxy_test 对 proxy 包的测试
     2  package proxy_test
     3  
     4  import (
     5  	"fmt"
     6  	"runtime/debug"
     7  
     8  	"github.com/tencent/goom/internal/logger"
     9  )
    10  
    11  //go:noinline
    12  func Caller(i int) int {
    13  	if i <= 0 {
    14  		return 1
    15  	}
    16  
    17  	return i * Caller(i-1)
    18  }
    19  
    20  //go:noinline
    21  func Caller1(i int) int {
    22  	if i <= 0 {
    23  		return 1
    24  	}
    25  
    26  	return i
    27  }
    28  
    29  //go:noinline
    30  func Caller2(i int) int {
    31  	i++
    32  	return i
    33  }
    34  
    35  // nolint
    36  // Arg 测试参数
    37  type Arg struct {
    38  	field1 string
    39  	field2 map[string]int
    40  	inner  InnerArg
    41  }
    42  
    43  //go:noinline
    44  func Caller3(arg Arg) int {
    45  	return len(arg.field2)
    46  }
    47  
    48  //go:noinline
    49  func Caller4Eval() {
    50  	arg := &Arg{
    51  		field1: field1,
    52  		field2: map[string]int{},
    53  		inner: InnerArg{
    54  			field1: field1,
    55  			field2: make([]string, 0),
    56  			field3: &InnerField{field3: "ok"},
    57  		},
    58  	}
    59  	Caller4(arg)
    60  }
    61  
    62  //go:noinline
    63  func Caller4(arg *Arg) {
    64  	if len(arg.field2) > 0 {
    65  		fmt.Println(len(arg.field2), arg.field1)
    66  	}
    67  
    68  	if len(arg.inner.field2) > 0 {
    69  		fmt.Println(len(arg.inner.field2), arg.inner.field1)
    70  	}
    71  }
    72  
    73  //go:noinline
    74  func Caller5() int {
    75  	logger.Trace(string(debug.Stack()))
    76  	return 0
    77  }
    78  
    79  //go:noinline
    80  func Caller6(a int) func() int {
    81  	return func() int {
    82  		return a + 1
    83  	}
    84  }
    85  
    86  //go:noinline
    87  func Caller7(i int) {
    88  	logger.Trace("Caller 7 called")
    89  }
    90  
    91  // nolint
    92  // Result 测试参数
    93  type Result struct {
    94  	i     int
    95  	inner *InnerResult
    96  	m     map[string]int
    97  }
    98  
    99  // nolint
   100  // InnerResult 测试参数
   101  type InnerResult struct {
   102  	j int
   103  }
   104  
   105  //go:noinline
   106  func Caller8(i int) *Result {
   107  	return &Result{
   108  		i: i,
   109  		inner: &InnerResult{
   110  			j: i * 2,
   111  		},
   112  		m: make(map[string]int, 2),
   113  	}
   114  }
   115  
   116  //go:noinline
   117  func Caller9(i int) Result {
   118  	return Result{
   119  		i: i,
   120  		inner: &InnerResult{
   121  			j: i * 2,
   122  		},
   123  		m: make(map[string]int, 2),
   124  	}
   125  }
   126  
   127  // nolint
   128  // InnerArg 测试参数
   129  type InnerArg struct {
   130  	field1 string
   131  	field2 []string
   132  	field3 *InnerField
   133  }
   134  
   135  // nolint
   136  // InnerField 测试参数
   137  type InnerField struct {
   138  	field3 string
   139  }
   140  
   141  // nolint
   142  //
   143  //go:noinline
   144  func ForceStackExpand(i int) int {
   145  	if i <= 0 {
   146  		return 1
   147  	}
   148  
   149  	return i * ForceStackExpand(i-1)
   150  }
   151  
   152  // nolint
   153  var field1 = "field1"
   154  
   155  // nolint
   156  // TestCase 测试用例类型
   157  type TestCase struct {
   158  	funcName string
   159  	// nolint
   160  	funcDef    interface{}
   161  	eval       func()
   162  	trampoline func() interface{}
   163  	proxy      func(interface{}) interface{}
   164  	// nolint
   165  	makeFunc interface{}
   166  	// nolint
   167  	evalMakeFunc func(makeFunc interface{})
   168  }