-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_test.go
63 lines (51 loc) · 1.01 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
package bsonrpc
import (
"io"
"net/rpc"
"testing"
)
type duplex struct {
io.Reader
io.Writer
}
func (d duplex) Close() (err error) {
return
}
type TestParam struct {
Val1 string
Val2 int
}
type Test int
func (ts Test) Foo(in TestParam, out *TestParam) (err error) {
out.Val1 = in.Val1 + "world!"
out.Val2 = in.Val2 + 5
return
}
func basicServer(conn io.ReadWriteCloser) {
s := ServeConn(conn)
var ts Test
s.Register(&ts)
}
func TestBasicClientServer(t *testing.T) {
toServer, fromClient := io.Pipe()
toClient, fromServer := io.Pipe()
s := rpc.NewServer()
var ts Test
s.Register(&ts)
go s.ServeCodec(NewServerCodec(duplex{toServer, fromServer}))
cl := NewClient(duplex{toClient, fromClient})
var tp TestParam
tp.Val1 = "Hello "
tp.Val2 = 10
err := cl.Call("Test.Foo", tp, &tp)
if err != nil {
t.Error(err)
return
}
if tp.Val1 != "Hello world!" {
t.Errorf("tp.Val2: expected %q, got %q", "Hello world!", tp.Val1)
}
if tp.Val2 != 15 {
t.Errorf("tp.Val2: expected 15, got %d", tp.Val2)
}
}