From b198d5fc181288c141e57afc06b298ba486373fa Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 18 Apr 2016 23:51:32 +0300 Subject: [PATCH] first *rpc_cgr tests --- engine/action.go | 22 ++++++++++++++---- engine/actions_test.go | 38 ++++++++++++++++++++++++++++++ utils/rpc_object_test.go | 28 ---------------------- utils/rpc_objects.go | 38 ------------------------------ utils/rpc_params.go | 50 ++++++++++++++++++++++++++++++++++++++++ utils/rpc_params_test.go | 32 +++++++++++++++++++++++++ 6 files changed, 138 insertions(+), 70 deletions(-) delete mode 100644 utils/rpc_object_test.go delete mode 100644 utils/rpc_objects.go create mode 100644 utils/rpc_params.go create mode 100644 utils/rpc_params_test.go diff --git a/engine/action.go b/engine/action.go index 4018a7c1c..0d4e51fe4 100644 --- a/engine/action.go +++ b/engine/action.go @@ -22,6 +22,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "net/smtp" "path" "reflect" @@ -665,12 +666,25 @@ func cgrRPCAction(account *Account, sq *StatsQueueTriggered, a *Action, acs Acti if err := json.Unmarshal([]byte(a.ExtraParameters), &req); err != nil { return err } - client, err := rpcclient.NewRpcClient(req.Method, req.Address, req.Attempts, 0, req.Transport, nil) + log.Printf("REQ: %+v", req) + params, err := utils.GetRpcParams(req.Method) if err != nil { - return nil, err + return err } - client.Call() - return nil + var client rpcclient.RpcClientConnection + if req.Address != utils.INTERNAL { + if client, err = rpcclient.NewRpcClient(req.Method, req.Address, req.Attempts, 0, req.Transport, nil); err != nil { + return err + } + } else { + client = params.Object + } + + in, out := params.InParam, params.OutParam + if err := json.Unmarshal([]byte(req.Param), &in); err != nil { + return err + } + return client.Call(req.Method, in, out) } // Structure to store actions according to weight diff --git a/engine/actions_test.go b/engine/actions_test.go index 9b3dc8f22..738a25d72 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -2159,6 +2159,44 @@ func TestActionCdrlogBalanceValue(t *testing.T) { } } +type TestRPCParameters struct { + status string +} + +type Attr struct { + Name string + Surname string + Age float64 +} + +func (trpcp *TestRPCParameters) Hopa(in Attr, out *float64) error { + trpcp.status = utils.OK + return nil +} + +func (trpcp *TestRPCParameters) Call(string, interface{}, interface{}) error { + return nil +} + +func TestCgrRpcAction(t *testing.T) { + trpcp := &TestRPCParameters{} + utils.RegisterRpcParams("", trpcp) + a := &Action{ + ExtraParameters: `{"Address": "internal", + "Transport": "*gob", + "Method": "TestRPCParameters.Hopa", + "Attempts":1, + "Async" :false, + "Param": "{\"Name\":\"n\", \"Surname\":\"s\", \"Age\":10.2}"}`, + } + if err := cgrRPCAction(nil, nil, a, nil); err != nil { + t.Error("error executing cgr action: ", err) + } + if trpcp.status != utils.OK { + t.Error("RPC not called!") + } +} + /**************** Benchmarks ********************************/ func BenchmarkUUID(b *testing.B) { diff --git a/utils/rpc_object_test.go b/utils/rpc_object_test.go deleted file mode 100644 index f42ea882a..000000000 --- a/utils/rpc_object_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import "testing" - -type RpcStruct struct{} - -func (rpc *RpcStruct) Hopa(normal string, out *float64) error { - return nil -} - -func (rpc *RpcStruct) Tropa(pointer *string, out *float64) error { - return nil -} - -func TestRPCObjectPointer(t *testing.T) { - RegisterRpcObject("", &RpcStruct{}) - if len(RpcObjects) != 2 { - t.Errorf("error registering rpc object: %v", RpcObjects) - } - x, found := RpcObjects["RpcStruct.Hopa"] - if !found { - t.Errorf("error getting rpcobject: %v (%+v)", RpcObjects, x) - } - x, found = RpcObjects["RpcStruct.Tropa"] - if !found { - t.Errorf("error getting rpcobject: %v (%+v)", RpcObjects, x) - } -} diff --git a/utils/rpc_objects.go b/utils/rpc_objects.go deleted file mode 100644 index 22028d274..000000000 --- a/utils/rpc_objects.go +++ /dev/null @@ -1,38 +0,0 @@ -package utils - -import "reflect" - -var RpcObjects map[string]interface{} - -type RpcObject struct { - Object interface{} - InParam interface{} - OutParam interface{} -} - -func init() { - RpcObjects = make(map[string]interface{}) -} - -func RegisterRpcObject(name string, rpcObject interface{}) { - objType := reflect.TypeOf(rpcObject) - if name == "" { - val := reflect.ValueOf(rpcObject) - name = objType.Name() - if val.Kind() == reflect.Ptr { - name = objType.Elem().Name() - } - } - for i := 0; i < objType.NumMethod(); i++ { - method := objType.Method(i) - methodType := method.Type - if methodType.NumIn() == 3 { // if it has three parameters (one is self and two are rpc params) - RpcObjects[name+"."+method.Name] = &RpcObject{ - Object: objType, - InParam: reflect.New(methodType.In(1)).Interface(), - OutParam: reflect.New(methodType.In(2).Elem()).Interface(), - } - } - - } -} diff --git a/utils/rpc_params.go b/utils/rpc_params.go new file mode 100644 index 000000000..ffd667b98 --- /dev/null +++ b/utils/rpc_params.go @@ -0,0 +1,50 @@ +package utils + +import ( + "reflect" + + "github.com/cgrates/rpcclient" +) + +var rpcParamsMap map[string]*RpcParams + +type RpcParams struct { + Object rpcclient.RpcClientConnection + InParam interface{} + OutParam interface{} +} + +func init() { + rpcParamsMap = make(map[string]*RpcParams) +} + +func RegisterRpcParams(name string, obj rpcclient.RpcClientConnection) { + objType := reflect.TypeOf(obj) + if name == "" { + val := reflect.ValueOf(obj) + name = objType.Name() + if val.Kind() == reflect.Ptr { + name = objType.Elem().Name() + } + } + for i := 0; i < objType.NumMethod(); i++ { + method := objType.Method(i) + methodType := method.Type + 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(), + OutParam: reflect.New(methodType.In(2).Elem()).Interface(), + } + } + + } +} + +func GetRpcParams(method string) (*RpcParams, error) { + x, found := rpcParamsMap[method] + if !found { + return nil, ErrNotFound + } + return x, nil +} diff --git a/utils/rpc_params_test.go b/utils/rpc_params_test.go new file mode 100644 index 000000000..a500fd3c2 --- /dev/null +++ b/utils/rpc_params_test.go @@ -0,0 +1,32 @@ +package utils + +import "testing" + +type RpcStruct struct{} + +func (rpc *RpcStruct) Hopa(normal string, out *float64) error { + return nil +} + +func (rpc *RpcStruct) Tropa(pointer *string, out *float64) error { + return nil +} + +func (rpc *RpcStruct) Call(string, interface{}, interface{}) error { + return nil +} + +func TestRPCObjectPointer(t *testing.T) { + RegisterRpcParams("", &RpcStruct{}) + if len(rpcParamsMap) != 2 { + t.Errorf("error registering rpc object: %v", rpcParamsMap) + } + x, found := rpcParamsMap["RpcStruct.Hopa"] + if !found { + t.Errorf("error getting rpcobject: %v (%+v)", rpcParamsMap, x) + } + x, found = rpcParamsMap["RpcStruct.Tropa"] + if !found { + t.Errorf("error getting rpcobject: %v (%+v)", rpcParamsMap, x) + } +}