This commit is contained in:
siddontang 2014-02-20 15:47:21 +08:00
parent eeed48eee2
commit 209f47d5ec
3 changed files with 86 additions and 19 deletions

View File

@ -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 { func (c *Client) returnCallError(fn reflect.Value, err error) []reflect.Value {
println("return call error", err.Error())
nOut := fn.Type().NumOut() nOut := fn.Type().NumOut()
out := make([]reflect.Value, nOut) out := make([]reflect.Value, nOut)
for i := 0; i < nOut-1; i++ { for i := 0; i < nOut-1; i++ {

View File

@ -33,31 +33,101 @@ func newTestClient() *Client {
return testClient return testClient
} }
func OnlineRpc(id int) (int, string, error) { func test_Rpc1(id int) (int, string, error) {
return id * 10, "abc", errors.New("hello world") return id * 10, "abc", nil
} }
func TestRpc(t *testing.T) { func TestRpc1(t *testing.T) {
defer func() {
e := recover()
if s, ok := e.(string); ok {
println(s)
}
if err, ok := e.(error); ok {
println(err.Error())
}
}()
s := newTestServer() s := newTestServer()
s.Register("online_rpc", OnlineRpc) s.Register("rpc1", test_Rpc1)
c := newTestClient() c := newTestClient()
var r func(int) (int, string, error) 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) 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")
}
} }

View File

@ -96,7 +96,6 @@ func (s *Server) Register(name string, f interface{}) (err error) {
} }
func (s *Server) onConn(co net.Conn) { func (s *Server) onConn(co net.Conn) {
println("onconn")
c := new(conn) c := new(conn)
c.co = co c.co = co