From f08524837a5b8c52e5571d92a1f5aa73ea188021 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 19 Apr 2016 09:57:19 +0300 Subject: [PATCH] testing *cgr_rpc --- engine/action.go | 4 +++- engine/actions_test.go | 29 +++++++++++++++++++++++++++-- utils/rpc_params.go | 2 +- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/engine/action.go b/engine/action.go index 0d4e51fe4..8aeeb5676 100644 --- a/engine/action.go +++ b/engine/action.go @@ -666,7 +666,6 @@ func cgrRPCAction(account *Account, sq *StatsQueueTriggered, a *Action, acs Acti if err := json.Unmarshal([]byte(a.ExtraParameters), &req); err != nil { return err } - log.Printf("REQ: %+v", req) params, err := utils.GetRpcParams(req.Method) if err != nil { return err @@ -681,9 +680,12 @@ func cgrRPCAction(account *Account, sq *StatsQueueTriggered, a *Action, acs Acti } in, out := params.InParam, params.OutParam + log.Print("IN: ", reflect.TypeOf(in)) + log.Print(req.Param) if err := json.Unmarshal([]byte(req.Param), &in); err != nil { return err } + log.Print("IN: ", reflect.TypeOf(in)) return client.Call(req.Method, in, out) } diff --git a/engine/actions_test.go b/engine/actions_test.go index 738a25d72..02e941e8f 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -22,6 +22,7 @@ import ( "encoding/json" "fmt" "reflect" + "strings" "testing" "time" @@ -2174,8 +2175,32 @@ func (trpcp *TestRPCParameters) Hopa(in Attr, out *float64) error { return nil } -func (trpcp *TestRPCParameters) Call(string, interface{}, interface{}) error { - return nil +func (trpcp *TestRPCParameters) Call(serviceMethod string, args interface{}, reply interface{}) error { + parts := strings.Split(serviceMethod, ".") + if len(parts) != 2 { + return utils.ErrNotImplemented + } + // get method + method := reflect.ValueOf(trpcp).MethodByName(parts[1]) + if !method.IsValid() { + return utils.ErrNotImplemented + } + + // construct the params + params := []reflect.Value{reflect.ValueOf(args), reflect.ValueOf(reply)} + + ret := method.Call(params) + if len(ret) != 1 { + return utils.ErrServerError + } + if ret[0].Interface() == nil { + return nil + } + err, ok := ret[0].Interface().(error) + if !ok { + return utils.ErrServerError + } + return err } func TestCgrRpcAction(t *testing.T) { diff --git a/utils/rpc_params.go b/utils/rpc_params.go index ffd667b98..21a4aa5f4 100644 --- a/utils/rpc_params.go +++ b/utils/rpc_params.go @@ -33,7 +33,7 @@ func RegisterRpcParams(name string, obj rpcclient.RpcClientConnection) { if methodType.NumIn() == 3 { // if it has three parameters (one is self and two are rpc params) rpcParamsMap[name+"."+method.Name] = &RpcParams{ Object: obj, - InParam: reflect.New(methodType.In(1)).Interface(), + InParam: reflect.Zero(methodType.In(1)).Interface(), OutParam: reflect.New(methodType.In(2).Elem()).Interface(), } }