From 209f47d5eca1d739ba7e1347083263bc2f93e232 Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 20 Feb 2014 15:47:21 +0800 Subject: [PATCH] add test --- rpc/client.go | 2 - rpc/rpc_test.go | 102 ++++++++++++++++++++++++++++++++++++++++-------- rpc/server.go | 1 - 3 files changed, 86 insertions(+), 19 deletions(-) diff --git a/rpc/client.go b/rpc/client.go index 0a4b544..70025d1 100644 --- a/rpc/client.go +++ b/rpc/client.go @@ -142,8 +142,6 @@ func (c *Client) call(fn reflect.Value, name string, in []reflect.Value) []refle } func (c *Client) returnCallError(fn reflect.Value, err error) []reflect.Value { - println("return call error", err.Error()) - nOut := fn.Type().NumOut() out := make([]reflect.Value, nOut) for i := 0; i < nOut-1; i++ { diff --git a/rpc/rpc_test.go b/rpc/rpc_test.go index 6a4f024..4390d70 100644 --- a/rpc/rpc_test.go +++ b/rpc/rpc_test.go @@ -33,31 +33,101 @@ func newTestClient() *Client { return testClient } -func OnlineRpc(id int) (int, string, error) { - return id * 10, "abc", errors.New("hello world") +func test_Rpc1(id int) (int, string, error) { + return id * 10, "abc", nil } -func TestRpc(t *testing.T) { - defer func() { - e := recover() - if s, ok := e.(string); ok { - println(s) - } - - if err, ok := e.(error); ok { - println(err.Error()) - } - }() +func TestRpc1(t *testing.T) { s := newTestServer() - s.Register("online_rpc", OnlineRpc) + s.Register("rpc1", test_Rpc1) c := newTestClient() var r func(int) (int, string, error) - if err := c.MakeRpc("online_rpc", &r); err != nil { + if err := c.MakeRpc("rpc1", &r); err != nil { t.Fatal(err) } - r(10) + a, b, e := r(10) + if e != nil { + t.Fatal(e) + } + + if a != 100 || b != "abc" { + t.Fatal(a, b) + } +} + +func test_Rpc2(ids []int) ([]int, error) { + if ids == nil || len(ids) == 0 { + return nil, errors.New("nid ids") + } + + if len(ids) >= 2 { + return []int{}, nil + } + + return []int{ids[0] * 10}, nil +} + +func TestRpc2(t *testing.T) { + s := newTestServer() + + s.Register("rpc2", test_Rpc2) + + c := newTestClient() + + var r func(ids []int) ([]int, error) + c.MakeRpc("rpc2", &r) + + a, e := r(nil) + if e == nil { + t.Fatal("must error") + } + + a, e = r([]int{}) + if e == nil { + t.Fatal("must error") + } + + a, e = r([]int{1}) + if e != nil { + t.Fatal(e) + } else if a[0] != 10 { + t.Fatal(a[0]) + } + + a, e = r([]int{1, 2, 3}) + if e != nil { + t.Fatal(e) + } else if len(a) != 0 { + t.Fatal("must 0") + } +} + +func test_Rpc3(id int) error { + return errors.New("hello world") +} + +func TestRpc3(t *testing.T) { + s := newTestServer() + + s.Register("rpc3", test_Rpc3) + + c := newTestClient() + + var r func(int) error + if err := c.MakeRpc("rpc3", &r); err != nil { + t.Fatal(err) + } + + e := r(10) + if e != nil { + if e.Error() != "hello world" { + t.Fatal(e.Error()) + } + } else { + t.Fatal("must error") + } } diff --git a/rpc/server.go b/rpc/server.go index 7893760..de070f1 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -96,7 +96,6 @@ func (s *Server) Register(name string, f interface{}) (err error) { } func (s *Server) onConn(co net.Conn) { - println("onconn") c := new(conn) c.co = co