Skip to content

Commit

Permalink
Service remote tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cscatolini committed Apr 20, 2018
1 parent 7fbacbd commit 192192d
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 49 deletions.
5 changes: 4 additions & 1 deletion component/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ func suitableRemoteMethods(typ reflect.Type, nameFunc func(string) string) map[s
if nameFunc != nil {
mn = nameFunc(mn)
}
methods[mn] = &Remote{Method: method}
methods[mn] = &Remote{
Method: method,
HasArgs: method.Type.NumIn() > 1,
}
}
}
return methods
Expand Down
1 change: 1 addition & 0 deletions component/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type (
Remote struct {
Receiver reflect.Value // receiver of method
Method reflect.Method // method stub
HasArgs bool // if remote has no args we won't try to serialize received data into arguments
}

// Service implements a specific service, some of it's methods will be
Expand Down
3 changes: 2 additions & 1 deletion constants/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
ErrNatsMessagesBufferSizeZero = errors.New("pitaya.buffer.cluster.rpc.server.messages cant be zero")
ErrNatsNoRequestTimeout = errors.New("pitaya.cluster.rpc.client.nats.requesttimeout cant be empty")
ErrNatsPushBufferSizeZero = errors.New("pitaya.buffer.cluster.rpc.server.push cant be zero")
ErrNilCondition = errors.New("pitaya/timer: nil condition")
ErrNoNatsConnectionString = errors.New("you have to provide a nats url")
ErrNoServerTypeChosenForRPC = errors.New("no server type chosen for sending RPC, send a full route in the format server.service.component")
ErrNoServerWithID = errors.New("can't find any server with the provided ID")
Expand All @@ -48,6 +49,7 @@ var (
ErrRPCClientNotInitialized = errors.New("RPC client is not running")
ErrRPCLocal = errors.New("RPC must be to a different server type")
ErrRPCServerNotInitialized = errors.New("RPC server is not running")
ErrReplyShouldBeNotNull = errors.New("reply must not be null")
ErrReplyShouldBePtr = errors.New("reply must be a pointer")
ErrRequestOnNotify = errors.New("tried to request a notify route")
ErrRouterNotInitialized = errors.New("router is not initialized")
Expand All @@ -58,5 +60,4 @@ var (
ErrSessionNotFound = errors.New("session not found")
ErrSessionOnNotify = errors.New("current session working on notify mode")
ErrWrongValueType = errors.New("protobuf: convert on wrong type value")
ErrNilCondition = errors.New("pitaya/timer: nil condition")
)
3 changes: 3 additions & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func TestHandlerCallToFront(t *testing.T) {
resp []byte
}{
{"connector.testsvc.testrequestonlysessionreturnsptr", []byte(``), []byte(`{"code":200,"msg":"hello"}`)},
{"connector.testsvc.testrequestonlysessionreturnsptrnil", []byte(``), []byte(`{"Code":"PIT-000","Msg":"reply must not be null"}`)},
{"connector.testsvc.testrequestonlysessionreturnsrawnil", []byte(``), []byte(`{"Code":"PIT-000","Msg":"reply must not be null"}`)},
{"connector.testsvc.testrequestreturnsptr", []byte(`{"msg":"good"}`), []byte(`{"code":200,"msg":"good"}`)},
{"connector.testsvc.testrequestreturnsraw", []byte(`{"msg":"good"}`), []byte(`good`)},
{"connector.testsvc.testrequestreceivereturnsraw", []byte(`woow`), []byte(`woow`)},
Expand Down Expand Up @@ -245,6 +247,7 @@ func TestUserRPC(t *testing.T) {
{"back_to_front_error", "game.testsvc.testsendrpc", []byte(`{"route":"connector.testremotesvc.rpctestreturnserror","data":"thisthis"}`), []byte(`{"Code":"PIT-433","Msg":"test error","Metadata":{"some":"meta"}}`)},
{"same_server", "connector.testsvc.testsendrpc", []byte(`{"route":"connector.testremotesvc.rpctestrawptrreturnsptr","data":"thisthis"}`), []byte(`{"Code":"PIT-000","Msg":"you are making a rpc that may be processed locally, either specify a different server type or specify a server id"}`)},
{"front_to_back_ptr", "connector.testsvc.testsendrpcpointer", []byte(`{"route":"game.testremotesvc.rpctestptrreturnsptr","data":"thisthis"}`), []byte(`{"code":200,"msg":"got thisthis"}`)},
{"no_args", "connector.testsvc.testsendrpcnoargs", []byte(`{"route":"game.testremotesvc.rpctestnoargs"}`), []byte(`{"code":200,"msg":"got nothing"}`)},
{"not_found", "connector.testsvc.testsendrpcpointer", []byte(`{"route":"game.testremotesvc.rpctestnotfound","data":"thisthis"}`), []byte(`{"Code":"PIT-404","Msg":"route not found","Metadata":{"route":"testremotesvc.rpctestnotfound"}}`)},
}

Expand Down
28 changes: 28 additions & 0 deletions examples/testing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func (tr *TestRemoteSvc) RPCTestReturnsError(data []byte) (*TestResponse, error)

}

// RPCTestNoArgs remote for e2e tests
func (tr *TestRemoteSvc) RPCTestNoArgs() (*TestResponse, error) {
return &TestResponse{
Code: 200,
Msg: "got nothing",
}, nil
}

// Init inits testsvc
func (t *TestSvc) Init() {
t.group = pitaya.NewGroup("g1")
Expand All @@ -101,6 +109,11 @@ func (t *TestSvc) TestRequestOnlySessionReturnsPtr(s *session.Session) (*TestRes
}, nil
}

// TestRequestOnlySessionReturnsPtrNil handler for e2e tests
func (t *TestSvc) TestRequestOnlySessionReturnsPtrNil(s *session.Session) (*TestResponse, error) {
return nil, nil
}

// TestRequestReturnsPtr handler for e2e tests
func (t *TestSvc) TestRequestReturnsPtr(s *session.Session, in *TestRequest) (*TestResponse, error) {
return &TestResponse{
Expand All @@ -109,6 +122,11 @@ func (t *TestSvc) TestRequestReturnsPtr(s *session.Session, in *TestRequest) (*T
}, nil
}

// TestRequestOnlySessionReturnsRawNil handler for e2e tests
func (t *TestSvc) TestRequestOnlySessionReturnsRawNil(s *session.Session) ([]byte, error) {
return nil, nil
}

// TestRequestReturnsRaw handler for e2e tests
func (t *TestSvc) TestRequestReturnsRaw(s *session.Session, in *TestRequest) ([]byte, error) {
return []byte(in.Msg), nil
Expand Down Expand Up @@ -168,6 +186,16 @@ func (t *TestSvc) TestSendRPC(s *session.Session, msg *TestRPCRequest) (*TestRes
return rep, nil
}

// TestSendRPCNoArgs tests sending a RPC
func (t *TestSvc) TestSendRPCNoArgs(s *session.Session, msg *TestRPCRequest) (*TestResponse, error) {
rep := &TestResponse{}
err := pitaya.RPC(msg.Route, rep, []byte(nil))
if err != nil {
return nil, err
}
return rep, nil
}

func main() {

gob.Register(&TestRequest{})
Expand Down
50 changes: 26 additions & 24 deletions service/remote.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) TFG Co. All Rights Reserved.
//
// Copyright (c) TFG Co. All Rights Reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
Expand Down Expand Up @@ -213,22 +213,22 @@ func (r *RemoteService) handleRPCUser(req *protos.Request, rt *route.Route) {
r.sendReply(reply, response)
return
}

args, err := unmarshalRemoteArg(req.GetMsg().GetData())
if err != nil {
response := &protos.Response{
Error: &protos.Error{
Code: e.ErrBadRequestCode,
Msg: err.Error(),
},
}
r.sendReply(reply, response)
return
}

params := []reflect.Value{remote.Receiver}
for _, arg := range args {
params = append(params, reflect.ValueOf(arg))
if remote.HasArgs {
args, err := unmarshalRemoteArg(req.GetMsg().GetData())
if err != nil {
response := &protos.Response{
Error: &protos.Error{
Code: e.ErrBadRequestCode,
Msg: err.Error(),
},
}
r.sendReply(reply, response)
return
}
for _, arg := range args {
params = append(params, reflect.ValueOf(arg))
}
}

ret, err := util.Pcall(remote.Method, params)
Expand All @@ -250,15 +250,17 @@ func (r *RemoteService) handleRPCUser(req *protos.Request, rt *route.Route) {
}

buf := bytes.NewBuffer([]byte(nil))
if err := gob.NewEncoder(buf).Encode(ret); err != nil {
response := &protos.Response{
Error: &protos.Error{
Code: e.ErrUnknownCode,
Msg: err.Error(),
},
if ret != nil {
if err := gob.NewEncoder(buf).Encode(ret); err != nil {
response := &protos.Response{
Error: &protos.Error{
Code: e.ErrUnknownCode,
Msg: err.Error(),
},
}
r.sendReply(reply, response)
return
}
r.sendReply(reply, response)
return
}

response.Data = buf.Bytes()
Expand Down
Loading

0 comments on commit 192192d

Please sign in to comment.