-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
86 lines (79 loc) · 1.67 KB
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package fastRPC
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
)
type customRPC struct {
}
func (rpc *customRPC) HandlerHelloWorld(args ...string) string {
builder := strings.Builder{}
builder.WriteString("Hello World")
for i := 0; i < len(args); i++ {
builder.WriteString(args[i])
}
return builder.String()
}
func TestNewFastRPCServer(t *testing.T) {
server, _ := NewFastRPCServer(&customRPC{}, nil)
err := server.Run()
if err != nil {
fmt.Println(err)
}
fmt.Println(server)
}
func TestCallFunc(t *testing.T) {
server, _ := NewFastRPCServer(&customRPC{}, nil)
result, err := server.callFunc("HandlerHelloWorld", []byte("hello hello hello"))
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(result))
}
type test struct {
Name string
Age int
Sex int
result map[string]interface{}
structTest test2
}
type test2 struct {
Name string
}
func TestConvertValueSlice(t *testing.T) {
slice := make([]int, 0, 100)
for i := 0; i < cap(slice); i++ {
slice = append(slice, 10)
}
value, err := convertValueToByte(reflect.ValueOf(slice))
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(value))
}
func TestConvertValueStruct(t *testing.T) {
t2 := test{
Name: "xiyang",
Age: 47,
Sex: 1,
result: map[string]interface{}{"xiyang": "hello"},
structTest: test2{
Name: "hello",
},
}
value, err := convertValueToByte(reflect.ValueOf(&t2))
sb := strings.Builder{}
sb.WriteString("{")
sb.WriteString(string(value))
sb.WriteString("}")
println("is json? ", json.Valid([]byte(sb.String())))
if err != nil {
fmt.Println(err.Error())
return
}
println(sb.String())
}