func TestClientNilResult(t *testing.T) { c, s := net.Pipe() defer c.Close() registry := makeRegistry() server := birpc.NewEndpoint(jsonmsg.NewCodec(s), registry) server_err := make(chan error) go func() { server_err <- server.Serve() }() client := birpc.NewEndpoint(jsonmsg.NewCodec(c), nil) client_err := make(chan error) go func() { client_err <- client.Serve() }() // Synchronous calls args := &WordLengthRequest{"xyzzy"} err := client.Call("WordLength.Len", args, nil) if err != nil { t.Errorf("unexpected error from call: %v", err.Error()) } c.Close() err = <-server_err if err != io.EOF { t.Fatalf("unexpected error from peer ServeCodec: %v", err) } err = <-client_err if err != io.ErrClosedPipe { t.Fatalf("unexpected error from local ServeCodec: %v", err) } }
func TestServerSimple(t *testing.T) { c, s := net.Pipe() defer c.Close() registry := makeRegistry() server := birpc.NewEndpoint(jsonmsg.NewCodec(s), registry) server_err := make(chan error) go func() { server_err <- server.Serve() }() io.WriteString(c, PALINDROME) var reply WordLength_LowLevelReply dec := json.NewDecoder(c) if err := dec.Decode(&reply); err != nil && err != io.EOF { t.Fatalf("decode failed: %s", err) } t.Logf("reply msg: %#v", reply) if reply.Error != nil { t.Fatalf("unexpected error response: %v", reply.Error) } if reply.Result.Length != 15 { t.Fatalf("got wrong answer: %v", reply.Result.Length) } c.Close() err := <-server_err if err != io.EOF { t.Fatalf("unexpected error from ServeCodec: %v", err) } }
func TestNonPointerRequest(t *testing.T) { c, s := net.Pipe() defer c.Close() registry := birpc.NewRegistry() registry.RegisterService(NonPointerRequest{}) server := birpc.NewEndpoint(jsonmsg.NewCodec(s), registry) server_err := make(chan error) go func() { server_err <- server.Serve() }() io.WriteString(c, `{"id": "42", "fn": "NonPointerRequest.NonPointer", "args": 13}`+"\n") var reply LowLevelReply dec := json.NewDecoder(c) if err := dec.Decode(&reply); err != nil && err != io.EOF { t.Fatalf("decode failed: %s", err) } t.Logf("reply msg: %#v", reply) if reply.Error != nil { t.Fatalf("unexpected error response: %v", reply.Error) } if string(reply.Result) != `13` { t.Fatalf("got wrong answer: %v", reply.Result) } c.Close() err := <-server_err if err != io.EOF { t.Fatalf("unexpected error from ServeCodec: %v", err) } }
func TestUnmarshalArgsError(t *testing.T) { c, s := net.Pipe() defer c.Close() registry := birpc.NewRegistry() registry.RegisterService(WordLength{}) server := birpc.NewEndpoint(jsonmsg.NewCodec(s), registry) server_err := make(chan error) go func() { server_err <- server.Serve() }() const REQ = `{"id": "42", "fn": "WordLength.Len", "args": "evil"}` + "\n" io.WriteString(c, REQ) var reply LowLevelReply dec := json.NewDecoder(c) if err := dec.Decode(&reply); err != nil && err != io.EOF { t.Fatalf("decode failed: %s", err) } t.Logf("reply msg: %#v", reply) if reply.Error == nil { t.Fatalf("expected an error") } if reply.Result != nil { t.Fatalf("got unexpected result: %v", reply.Result) } c.Close() err := <-server_err if err != io.EOF { t.Fatalf("unexpected error from ServeCodec: %v", err) } }
func TestServerError(t *testing.T) { c, s := net.Pipe() defer c.Close() registry := birpc.NewRegistry() registry.RegisterService(Failing{}) server := birpc.NewEndpoint(jsonmsg.NewCodec(s), registry) server_err := make(chan error) go func() { server_err <- server.Serve() }() const REQ = `{"id": "42", "fn": "Failing.Fail", "args": {}}` + "\n" io.WriteString(c, REQ) var reply LowLevelReply dec := json.NewDecoder(c) if err := dec.Decode(&reply); err != nil && err != io.EOF { t.Fatalf("decode failed: %s", err) } t.Logf("reply msg: %#v", reply) if reply.Error == nil { t.Fatalf("expected an error") } if g, e := reply.Error.Msg, "intentional"; g != e { t.Fatalf("unexpected error response: %q != %q", g, e) } if reply.Result != nil { t.Fatalf("got unexpected result: %v", reply.Result) } c.Close() err := <-server_err if err != io.EOF { t.Fatalf("unexpected error from ServeCodec: %v", err) } }
func TestFillArgsError(t *testing.T) { c, s := net.Pipe() defer c.Close() registry := birpc.NewRegistry() // just need anything with >2 parameters registry.RegisterService(&EndpointPeer{}) jsonCodec := jsonmsg.NewCodec(s) codec := BadFillArgsCodec{jsonCodec} server := birpc.NewEndpoint(codec, registry) server_err := make(chan error) go func() { server_err <- server.Serve() }() io.WriteString(c, `{"id":"42","fn":"EndpointPeer.Poke","args":{}}`) var reply LowLevelReply dec := json.NewDecoder(c) if err := dec.Decode(&reply); err != nil && err != io.EOF { t.Fatalf("decode failed: %s", err) } t.Logf("reply msg: %#v", reply) if reply.Error == nil { t.Fatalf("expected an error") } if reply.Result != nil { t.Fatalf("got unexpected result: %v", reply.Result) } c.Close() err := <-server_err if err != io.EOF { t.Fatalf("unexpected error from ServeCodec: %v", err) } }
func TestServerEndpointArg(t *testing.T) { peer := &EndpointPeer{} registry := birpc.NewRegistry() registry.RegisterService(peer) c, s := net.Pipe() defer c.Close() server := birpc.NewEndpoint(jsonmsg.NewCodec(s), registry) server_err := make(chan error) go func() { server_err <- server.Serve() }() io.WriteString(c, `{"id":"42","fn":"EndpointPeer.Poke","args":{}}`) var reply LowLevelReply dec := json.NewDecoder(c) if err := dec.Decode(&reply); err != nil && err != io.EOF { t.Fatalf("decode failed: %s", err) } t.Logf("reply msg: %#v", reply) if reply.Error != nil { t.Fatalf("unexpected error response: %v", reply.Error) } c.Close() err := <-server_err if err != io.EOF { t.Fatalf("unexpected error from ServeCodec: %v", err) } if peer.seen == nil { t.Fatalf("peer never saw a birpc.Endpoint") } }