github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/internal/test/router/handler_test.go (about)

     1  package router
     2  
     3  import (
     4  	"fmt"
     5  	"hash/crc32"
     6  	"reflect"
     7  	"sync"
     8  	"testing"
     9  
    10  	"github.com/volts-dev/volts/router"
    11  )
    12  
    13  var i int = 0
    14  
    15  // 可缓存
    16  var (
    17  	val  = reflect.ValueOf(&ctrl{Name: "Test", MidWare: &midWare{name: "User"}})
    18  	pool sync.Pool
    19  )
    20  
    21  func init() {
    22  	pool.New = func() interface{} {
    23  		return reflect.New(val.Elem().Type())
    24  	}
    25  	//go func() {
    26  	for i := 0; i < 100; i++ {
    27  		pool.Put(reflect.New(val.Elem().Type()).Elem())
    28  	}
    29  	//}()
    30  
    31  }
    32  
    33  func TestHandlerReflectInterfaceChangedMetohd(t *testing.T) {
    34  	var aa interface{}
    35  	aa = &ctrl{Name: "111"}
    36  	val := ValueOf(aa)
    37  
    38  	p := val.Interface()
    39  	if n, ok := p.(iwrapper); ok {
    40  		fmt.Println(n.String())
    41  	}
    42  	/*
    43  		ei := (*emptyInterface)(unsafe.Pointer(&p))
    44  		ptr0 := uintptr(ei.word)
    45  		fmt.Println(ptr0)
    46  
    47  		fn := val.MethodByName("String")
    48  		wrapper := &wrapper{
    49  			Func: fn.Interface().(func(...interface{}) string),
    50  		}
    51  		fmt.Println(wrapper.String())
    52  	*/
    53  }
    54  
    55  func BenchmarkCreatByMakeFunc(b *testing.B) {
    56  	var creator func() interface{}
    57  	// swap is the implementation passed to MakeFunc.
    58  	// It must work in terms of reflect.Values so that it is possible
    59  	// to write code without knowing beforehand what the types
    60  	// will be.
    61  	swap := func(in []reflect.Value) []reflect.Value {
    62  		//newV := pool.Get().(reflect.Value)
    63  		newV := reflect.New(val.Elem().Type())
    64  		//newV.Field(0).Elem().Set(reflect.ValueOf("creator"))
    65  		return []reflect.Value{newV}
    66  	}
    67  	fnVal := reflect.ValueOf(&creator).Elem()
    68  	// Make a function of the right type.
    69  	v := reflect.MakeFunc(fnVal.Type(), swap)
    70  	fnVal.Set(v)
    71  	var itf interface{}
    72  	for n := 0; n < b.N; n++ {
    73  		itf = creator()
    74  	}
    75  	//fmt.Println("xxx", creator())
    76  	fmt.Println("xxx", itf)
    77  
    78  }
    79  
    80  func BenchmarkCreate(b *testing.B) {
    81  	var itf interface{}
    82  	for n := 0; n < b.N; n++ {
    83  		itf = &ctrl{Name: "Test", MidWare: &midWare{name: "User"}}
    84  	}
    85  	fmt.Println("xxx", itf)
    86  }
    87  
    88  // 测试interface和reflect变更
    89  func TestHandler(t *testing.T) {
    90  	var c interface{}
    91  	c = &ctrl{Name: "Test", MidWare: &midWare{name: "User"}}
    92  	val := reflect.ValueOf(c)
    93  	//mVal := val.Method(0)
    94  
    95  	//if m, ok := val.Interface().(iMidWare); ok {
    96  	//	fn = m.String
    97  	//}
    98  
    99  	var creator func() interface{}
   100  	// swap is the implementation passed to MakeFunc.
   101  	// It must work in terms of reflect.Values so that it is possible
   102  	// to write code without knowing beforehand what the types
   103  	// will be.
   104  	swap := func(in []reflect.Value) []reflect.Value {
   105  		//newV := reflect.New(val.Elem().Type())
   106  		//newV.Field(0).Elem().Set(reflect.ValueOf("creator"))
   107  		return []reflect.Value{val}
   108  	}
   109  	fnVal := reflect.ValueOf(&creator).Elem()
   110  	// Make a function of the right type.
   111  	v := reflect.MakeFunc(fnVal.Type(), swap)
   112  	fnVal.Set(v)
   113  
   114  	for i := 0; i < 3; i++ {
   115  		//c.(*ctrl).Name = fmt.Sprintf("%s%d New", c.(*ctrl).Name, i)
   116  		c = &ctrl{Name: fmt.Sprintf("%s%d New", "Test", i), MidWare: &midWare{name: "User"}}
   117  		fmt.Println(creator())
   118  	}
   119  
   120  }
   121  
   122  func Aaa() {
   123  
   124  }
   125  
   126  func TestHandler4(t *testing.T) {
   127  	name := router.GetFuncName(Aaa)
   128  	hash := int(crc32.ChecksumIEEE([]byte(name)))
   129  	fmt.Println(name)
   130  	fmt.Println(hash)
   131  }
   132  
   133  func TestHandler3(t *testing.T) {
   134  	//var c interface{}
   135  	//c = &ctrl{Name: "Test", MidWare: &midWare{name: "User"}}
   136  	//val := reflect.ValueOf(c)
   137  
   138  	fmt.Println(reflect.TypeOf(&ctrl{}).Elem().Name())
   139  
   140  	fn := ctrl.String
   141  	v := reflect.ValueOf(fn)
   142  
   143  	rawT := v.Type().In(0)
   144  	newV := reflect.New(rawT)
   145  	model := newV.Interface()
   146  	mV := reflect.ValueOf(model.(*ctrl).String)
   147  	fmt.Println(mV.Type().Name(), mV.Type().String())
   148  	//mm := newV.Elem().FieldByName("String").Interface().(func(...interface{}) string)
   149  	//mmModel := mV.Interface()
   150  	//mm := mmModel.(func(...interface{}) string)
   151  
   152  	// 实现函数
   153  	var creator func(...interface{}) string
   154  
   155  	swap := func(in []reflect.Value) []reflect.Value {
   156  		//newV := reflect.New(val.Elem().Type())
   157  		//newV.Field(0).Elem().Set(reflect.ValueOf("creator"))
   158  		return []reflect.Value{val}
   159  	}
   160  	fnVal := reflect.ValueOf(&creator).Elem()
   161  	// Make a function of the right type.
   162  	vv := reflect.MakeFunc(mV.Type(), swap)
   163  	fnVal.Set(vv)
   164  
   165  	for i := 0; i < 3; i++ {
   166  		//c.(*ctrl).Name = fmt.Sprintf("%s%d New", c.(*ctrl).Name, i)
   167  		model = &ctrl{Name: fmt.Sprintf("%s%d New", "Test", i), MidWare: &midWare{name: "User"}}
   168  		fmt.Println(model.(*ctrl))
   169  	}
   170  
   171  }