-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrpc_test.go
125 lines (110 loc) · 2.71 KB
/
rpc_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package sproto
import (
"errors"
"reflect"
"testing"
)
type FoobarRequest struct {
What *string `sproto:"string,0"`
}
type FoobarResponse struct {
Ok *bool `sproto:"boolean,0"`
What *string `sproto:"string,1"`
}
type FooResponse struct {
Ok *bool `sproto:"boolean,0"`
}
var protocols []*Protocol = []*Protocol{
{
Type: 1,
Name: "test.foobar",
MethodName: "Test.Foobar",
Request: reflect.TypeOf(&FoobarRequest{}),
Response: reflect.TypeOf(&FoobarResponse{}),
},
{
Type: 2,
Name: "test.foo",
MethodName: "Test.Foo",
Response: reflect.TypeOf(&FooResponse{}),
},
{
Type: 3,
Name: "test.bar",
MethodName: "Test.Bar",
},
}
func checkRequest(rpc *Rpc, name string, session int32, sp interface{}) (interface{}, error) {
chunk, err := rpc.RequestEncode(name, session, sp)
if err != nil {
return nil, err
}
mode, name1, session1, sp1, err := rpc.Dispatch(chunk)
if err != nil {
return nil, err
}
if mode != RpcRequestMode || name != name1 || session != session1 {
return nil, errors.New("dipatch failed: unmatch meta info")
}
return sp1, nil
}
func TestRpcRequest(t *testing.T) {
rpc, err := NewRpc(protocols)
if err != nil {
t.Fatalf("new rpc failed with error:%s", err)
}
// request & dispatch request
what := "hello"
sp, err := checkRequest(rpc, "test.foobar", 1, &FoobarRequest{What: &what})
if err != nil {
t.Fatalf("check request failed:%s", err)
}
req := sp.(*FoobarRequest)
if *req.What != what {
t.Fatalf("check failed: unmatch data")
}
// nil request
sp, err = checkRequest(rpc, "test.foo", 2, nil)
if err != nil {
t.Fatalf("check request failed:%s", err)
}
if sp != nil {
t.Fatalf("check failed: unmatch data")
}
// nil request
sp, err = checkRequest(rpc, "test.bar", 0, nil)
if err != nil {
t.Fatalf("check request failed:%s", err)
}
if sp != nil {
t.Fatalf("check failed: unmatch data")
}
}
func TestRpcResponse(t *testing.T) {
rpc, err := NewRpc(protocols)
if err != nil {
t.Fatalf("new rpc failed with error:%s", err)
}
// request & dispatch request
what := "hello"
_, err = rpc.RequestEncode("test.foobar", 18, &FoobarRequest{What: &what})
if err != nil {
t.Fatalf("request encode failed:%s", err)
}
// check response
chunk, err := rpc.ResponseEncode("test.foobar", 18, &FoobarResponse{Ok: Bool(true)})
if err != nil {
t.Fatalf("response encode failed:%s", err)
}
mode, name, session, sp, err := rpc.Dispatch(chunk)
if err != nil {
t.Fatalf("dispatch failed:%s", err)
}
if mode != RpcResponseMode || name != "test.foobar" || session != 18 {
t.Fatalf("dispatch failed:unmatch meta info")
}
response := sp.(*FoobarResponse)
if !*response.Ok {
t.Fatalf("dispatch failed:unmatch data")
}
}