github.com/lipogem/lrpc/lrpc-go@v0.0.0-20201108094501-0e5384d0daca/tcp/tcp_test.go (about)

     1  package tcp
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/lipogem/lrpc/lrpc-go/fun"
     9  	"github.com/lipogem/lrpc/lrpc-go/val"
    10  )
    11  
    12  func plus(x int64, y float64) int64 {
    13  	return x + int64(y)
    14  }
    15  
    16  func TestTcp(t *testing.T) {
    17  	fn := fun.NewFun()
    18  	fn.Regist("plus", plus)
    19  	go func() {
    20  		Service(fn, "0.0.0.0:9009")
    21  	}()
    22  	time.Sleep(time.Microsecond)
    23  	con, e := NewConnection("127.0.0.1:9009")
    24  	if e != nil {
    25  		t.Error(e)
    26  	}
    27  	x := int64(1)
    28  	y := 3.14
    29  	q, e := fun.Make("plus", x, y)
    30  	if e != nil {
    31  		t.Error(e)
    32  	}
    33  	v, e := con.Invoke(q, reflect.Int64.String())
    34  	if e != nil || len(v) != 1 {
    35  		t.Error(e)
    36  	}
    37  	r := v[0].(int64)
    38  	if r != 4 {
    39  		t.Error(r)
    40  	}
    41  }
    42  
    43  func bubbleSort(arr []int32) {
    44  	for i := 0; i < len(arr)-1; i++ {
    45  		for j := 0; j < len(arr)-1-i; j++ {
    46  			if arr[j] > arr[j+1] {
    47  				temp := arr[j+1]
    48  				arr[j+1] = arr[j]
    49  				arr[j] = temp
    50  			}
    51  		}
    52  	}
    53  }
    54  
    55  func BenchmarkTcp1(t *testing.B) {
    56  	var p []int32
    57  	val.RegistObj(p)
    58  	fn := fun.NewFun()
    59  	fn.Regist("bubbleSort", bubbleSort)
    60  	go func() {
    61  		Service(fn, "0.0.0.0:9009")
    62  	}()
    63  	time.Sleep(time.Microsecond)
    64  	arr := make([]int32, 50000)
    65  	for i := 0; i < 50000; i++ {
    66  		arr[i] = int32(i)
    67  	}
    68  	con, e := NewConnection("127.0.0.1:9009")
    69  	if e != nil {
    70  		t.Error(e)
    71  	}
    72  	q, e := fun.Make("bubbleSort", arr)
    73  	if e != nil {
    74  		t.Error(e)
    75  	}
    76  	_, e = con.Invoke(q)
    77  	if e != nil {
    78  		t.Error(e)
    79  	}
    80  }
    81  
    82  func BenchmarkTcp2(t *testing.B) {
    83  	arr := make([]int32, 50000)
    84  	for i := 0; i < 50000; i++ {
    85  		arr[i] = int32(i)
    86  	}
    87  	bubbleSort(arr)
    88  }