forked from documize/glick
/
rpc_test.go
155 lines (129 loc) · 3.25 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package glick_test
import (
"net"
"net/rpc"
"net/rpc/jsonrpc"
"sync"
"testing"
"time"
"github.com/documize/glick"
test "github.com/documize/glick/_test"
)
func TestRPC(t *testing.T) {
tisOut := func() interface{} {
return interface{}(&test.IntStr{})
}
// set up the server
if err := rpc.Register(&test.CI{}); err != nil {
t.Error(err.Error())
return
}
for i := 0; i < 2; i++ {
endPt := "localhost:808"
endPt += string('8' + i)
var up sync.WaitGroup
up.Add(1)
// start the protocol server
go func(ii int, ep string) {
listener, err := net.Listen("tcp", ep)
if err != nil {
t.Error(err.Error())
return
}
up.Done()
for {
conn, err := listener.Accept()
if err != nil {
t.Error(err.Error())
return
}
if ii == 0 {
go rpc.ServeConn(conn)
} else {
go jsonrpc.ServeConn(conn)
}
}
}(i, endPt)
up.Wait()
// run the client code
var useJSON bool
if i > 0 {
useJSON = true
}
client(t, useJSON, tisOut, endPt)
clientBad(t, useJSON, tisOut, endPt)
}
}
func client(t *testing.T, useJSON bool, tisOut func() interface{}, endPt string) {
l, nerr := glick.New(nil)
if nerr != nil {
t.Error(nerr)
}
api := "ab"
act := "cdef"
if err := l.RegAPI(api, test.IntStr{}, tisOut, 2*time.Second); err != nil {
t.Error(err)
return
}
if err := l.RegPlugin(api, act,
glick.PluginRPC(useJSON, "CI.CopyIntX", endPt, tisOut), nil); err != nil {
t.Error("unable to create JsonRPC " + err.Error())
return
}
par := test.IntStr{I: 42}
if ret, err := l.Run(nil, api, act, par); err != nil {
t.Error("unable to run plugin " + err.Error())
} else {
if ret.(*test.IntStr).I != 42 {
t.Error("RPC integer copy did not work")
}
}
par.I = 4
if _, err := l.Run(nil, api, act, par); err == nil {
t.Error("over-long plugin did not timeout")
}
if err := l.RegPlugin(api, "bep",
glick.PluginRPC(useJSON, "", "localhost:8080", tisOut), nil); err == nil {
t.Error("able to create empty end-point method")
return
}
if err := l.RegPlugin(api, "bep",
glick.PluginRPC(useJSON, "CI.CopyIntX", "", tisOut), nil); err == nil {
t.Error("able to create empty endpoint")
return
}
}
func clientBad(t *testing.T, useJSON bool, tisOut func() interface{}, endPt string) {
l, nerr := glick.New(nil)
if nerr != nil {
t.Error(nerr)
}
api := "ab"
act := "cdef"
if err := l.RegAPI(api, test.IntStr{}, tisOut, 2*time.Second); err != nil {
t.Error(err)
return
}
if err := l.RegPlugin(api, act,
glick.PluginRPC(useJSON, "CI.CopyIntX", endPt, tisOut), nil); err != nil {
t.Error("unable to create JsonRPC " + err.Error())
return
}
par := test.IntStr{I: 42}
if err := l.RegPlugin(api, "errEP",
glick.PluginRPC(useJSON, "CI.CopyIntX", "localhost:9999", tisOut), nil); err != nil {
t.Error("error on valid (if unused) endpoint")
return
}
if _, err := l.Run(nil, api, "errEP", par); err == nil {
t.Error("did not error on unpopulated end-point")
}
noPoint := func() interface{} { return interface{}(42) }
if err := l.RegAPI("noPoint", 42, noPoint, 0); err != nil {
t.Error(err)
}
if err := l.RegPlugin("noPoint", "errEP",
glick.PluginRPC(useJSON, "CI.CopyIntX", "localhost:9999", noPoint), nil); err == nil {
t.Error("a non-pointer return should error")
}
}