mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-14 20:59:53 +05:00
first *rpc_cgr tests
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
50
utils/rpc_params.go
Normal file
50
utils/rpc_params.go
Normal file
@@ -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
|
||||
}
|
||||
32
utils/rpc_params_test.go
Normal file
32
utils/rpc_params_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user