Files
rpcx/client/client_test.go

104 lines
2.0 KiB
Go

package client
import (
"context"
"testing"
"time"
"github.com/smallnest/rpcx/_testutils"
"github.com/smallnest/rpcx/protocol"
"github.com/smallnest/rpcx/server"
)
type Args struct {
A int
B int
}
type Reply struct {
C int
}
type Arith int
func (t *Arith) Mul(ctx context.Context, args *Args, reply *Reply) error {
reply.C = args.A * args.B
return nil
}
type PBArith int
func (t *PBArith) Mul(ctx context.Context, args *testutils.ProtoArgs, reply *testutils.ProtoReply) error {
reply.C = args.A * args.B
return nil
}
func TestClient_IT(t *testing.T) {
s := server.Server{}
s.RegisterName("Arith", new(Arith), "")
s.RegisterName("PBArith", new(PBArith), "")
go s.Serve("tcp", "127.0.0.1:0")
defer s.Close()
time.Sleep(500 * time.Millisecond)
addr := s.Address().String()
client := &Client{
SerializeType: protocol.JSON,
CompressType: protocol.Gzip,
}
err := client.Connect("tcp", addr)
if err != nil {
t.Fatalf("failed to connect: %v", err)
}
defer client.Close()
args := &Args{
A: 10,
B: 20,
}
reply := &Reply{}
err = client.Call(context.Background(), "Arith", "Mul", args, reply)
if err != nil {
t.Fatalf("failed to call: %v", err)
}
if reply.C != 200 {
t.Fatalf("expect 200 but got %d", reply.C)
}
err = client.Call(context.Background(), "Arith", "Add", args, reply)
if err == nil {
t.Fatal("expect an error but got nil")
}
client.SerializeType = protocol.MsgPack
reply = &Reply{}
err = client.Call(context.Background(), "Arith", "Mul", args, reply)
if err != nil {
t.Fatalf("failed to call: %v", err)
}
if reply.C != 200 {
t.Fatalf("expect 200 but got %d", reply.C)
}
client.SerializeType = protocol.ProtoBuffer
pbArgs := &testutils.ProtoArgs{
A: 10,
B: 20,
}
pbReply := &testutils.ProtoReply{}
err = client.Call(context.Background(), "PBArith", "Mul", pbArgs, pbReply)
if err != nil {
t.Fatalf("failed to call: %v", err)
}
if pbReply.C != 200 {
t.Fatalf("expect 200 but got %d", pbReply.C)
}
}