Source file
src/net/rpc/client_test.go
1
2
3
4
5 package rpc
6
7 import (
8 "errors"
9 "fmt"
10 "net"
11 "strings"
12 "testing"
13 )
14
15 type shutdownCodec struct {
16 responded chan int
17 closed bool
18 }
19
20 func (c *shutdownCodec) WriteRequest(*Request, any) error { return nil }
21 func (c *shutdownCodec) ReadResponseBody(any) error { return nil }
22 func (c *shutdownCodec) ReadResponseHeader(*Response) error {
23 c.responded <- 1
24 return errors.New("shutdownCodec ReadResponseHeader")
25 }
26 func (c *shutdownCodec) Close() error {
27 c.closed = true
28 return nil
29 }
30
31 func TestCloseCodec(t *testing.T) {
32 codec := &shutdownCodec{responded: make(chan int)}
33 client := NewClientWithCodec(codec)
34 <-codec.responded
35 client.Close()
36 if !codec.closed {
37 t.Error("client.Close did not close codec")
38 }
39 }
40
41
42
43 type R struct {
44 msg []byte
45 }
46
47 type S struct{}
48
49 func (s *S) Recv(nul *struct{}, reply *R) error {
50 *reply = R{[]byte("foo")}
51 return nil
52 }
53
54 func TestGobError(t *testing.T) {
55 defer func() {
56 err := recover()
57 if err == nil {
58 t.Fatal("no error")
59 }
60 if !strings.Contains(err.(error).Error(), "reading body unexpected EOF") {
61 t.Fatal("expected `reading body unexpected EOF', got", err)
62 }
63 }()
64 Register(new(S))
65
66 listen, err := net.Listen("tcp", "127.0.0.1:0")
67 if err != nil {
68 panic(err)
69 }
70 go Accept(listen)
71
72 client, err := Dial("tcp", listen.Addr().String())
73 if err != nil {
74 panic(err)
75 }
76
77 var reply Reply
78 err = client.Call("S.Recv", &struct{}{}, &reply)
79 if err != nil {
80 panic(err)
81 }
82
83 fmt.Printf("%#v\n", reply)
84 client.Close()
85
86 listen.Close()
87 }
88
View as plain text