-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrpc_msg_test.go
51 lines (46 loc) · 1.18 KB
/
rpc_msg_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
package rpcx
import (
"fmt"
"github.com/gen-iot/std"
"testing"
)
type exampleStruct struct {
Name string `json:"name"`
Age int `json:"age"`
Meta map[string]interface{} `json:"meta"`
}
func newExampleStruct() *exampleStruct {
return &exampleStruct{
Name: "suzhen",
Age: 100,
Meta: map[string]interface{}{
"k1": "v1",
"k2": 1,
"k3": []string{"a", "b", "c"},
},
}
}
func TestEncodeMessage_MSGPACK(t *testing.T) {
msg := &RawMsg{
Id: std.GenRandomUUID(),
MethodName: "sum",
Type: ReqMsg,
}
msg.SetErrorString("try set error")
std.AssertError(msg.SetData(newExampleStruct()), "set data error")
bytes, err := encodeRpcMsg(msg)
std.AssertError(err, "encodeRpcMsg")
fmt.Println("encode -> ", string(bytes))
buffer := std.NewByteBuffer()
buffer.Write(bytes)
outMsg, err := decodeRpcMsg(buffer, 1024*1024*4)
std.AssertError(err, "decodeRpcMsg")
outExampleStruct := new(exampleStruct)
err = outMsg.BindData(outExampleStruct)
std.AssertError(err, "bind data error")
fmt.Println(*outExampleStruct)
}
func TestUUID(t *testing.T) {
uuid := std.GenRandomUUID()
fmt.Println("uuid -> ", uuid, " len -> ", len(uuid))
}