mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 10:06:24 +05:00
Added tests for analyzers over birpc
This commit is contained in:
committed by
Dan Christian Bogos
parent
8ed0a145f9
commit
c2821394d0
@@ -28,6 +28,7 @@ import (
|
||||
"github.com/cgrates/cgrates/analyzers"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/cgrates/rpcclient"
|
||||
)
|
||||
|
||||
type conn interface {
|
||||
@@ -122,7 +123,7 @@ func newCapsBiRPCGOBCodec(conn conn, caps *engine.Caps, anz *analyzers.AnalyzerS
|
||||
if to != nil {
|
||||
tostr = to.String()
|
||||
}
|
||||
return analyzers.NewAnalyzerBiRPCCodec(r, anz, utils.MetaGOB, fromstr, tostr)
|
||||
return analyzers.NewAnalyzerBiRPCCodec(r, anz, rpcclient.BiRPCGOB, fromstr, tostr)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -140,7 +141,7 @@ func newCapsBiRPCJSONCodec(conn conn, caps *engine.Caps, anz *analyzers.Analyzer
|
||||
if to != nil {
|
||||
tostr = to.String()
|
||||
}
|
||||
return analyzers.NewAnalyzerBiRPCCodec(r, anz, utils.MetaJSON, fromstr, tostr)
|
||||
return analyzers.NewAnalyzerBiRPCCodec(r, anz, rpcclient.BiRPCJSON, fromstr, tostr)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -25,9 +25,12 @@ import (
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/cenkalti/rpc2"
|
||||
jsonrpc2 "github.com/cenkalti/rpc2/jsonrpc"
|
||||
"github.com/cgrates/cgrates/analyzers"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/cgrates/rpcclient"
|
||||
)
|
||||
|
||||
type mockServerCodec struct{}
|
||||
@@ -136,3 +139,108 @@ func TestNewCapsJSONCodec(t *testing.T) {
|
||||
t.Errorf("Expected: %v ,received:%v", exp, r)
|
||||
}
|
||||
}
|
||||
|
||||
type mockBiRPCCodec struct{}
|
||||
|
||||
func (mockBiRPCCodec) ReadHeader(r *rpc2.Request, _ *rpc2.Response) error {
|
||||
r.Seq = 0
|
||||
r.Method = utils.CoreSv1Ping
|
||||
return nil
|
||||
}
|
||||
func (mockBiRPCCodec) ReadRequestBody(interface{}) error { return utils.ErrNotImplemented }
|
||||
func (mockBiRPCCodec) ReadResponseBody(interface{}) error { return nil }
|
||||
func (mockBiRPCCodec) WriteRequest(*rpc2.Request, interface{}) error { return nil }
|
||||
func (mockBiRPCCodec) WriteResponse(*rpc2.Response, interface{}) error { return nil }
|
||||
func (mockBiRPCCodec) Close() error { return nil }
|
||||
|
||||
func TestNewCapsBiRPCCodec(t *testing.T) {
|
||||
mk := new(mockBiRPCCodec)
|
||||
cr := engine.NewCaps(0, utils.MetaBusy)
|
||||
if r := newCapsBiRPCCodec(mk, cr); !reflect.DeepEqual(mk, r) {
|
||||
t.Errorf("Expected: %v ,received:%v", mk, r)
|
||||
}
|
||||
cr = engine.NewCaps(1, utils.MetaBusy)
|
||||
exp := &capsBiRPCCodec{
|
||||
sc: mk,
|
||||
caps: cr,
|
||||
}
|
||||
codec := newCapsBiRPCCodec(mk, cr)
|
||||
if !reflect.DeepEqual(exp, codec) {
|
||||
t.Errorf("Expected: %v ,received:%v", exp, codec)
|
||||
}
|
||||
var err error
|
||||
r := new(rpc2.Request)
|
||||
expR := &rpc2.Request{
|
||||
Seq: 0,
|
||||
Method: utils.CoreSv1Ping,
|
||||
}
|
||||
if err = codec.ReadHeader(r, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !reflect.DeepEqual(r, expR) {
|
||||
t.Errorf("Expected: %v ,received:%v", expR, r)
|
||||
}
|
||||
|
||||
if err = codec.ReadRequestBody("args"); err == nil || err != utils.ErrNotImplemented {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = codec.ReadRequestBody("args"); err != utils.ErrMaxConcurentRPCExceededNoCaps {
|
||||
t.Errorf("Expected error: %v ,received: %v ", utils.ErrMaxConcurentRPCExceededNoCaps, err)
|
||||
}
|
||||
|
||||
if err = codec.WriteResponse(&rpc2.Response{
|
||||
Error: "error",
|
||||
Seq: 0,
|
||||
}, "reply"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = codec.ReadResponseBody(nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = codec.WriteRequest(&rpc2.Request{
|
||||
Seq: 0,
|
||||
Method: utils.CoreSv1Ping,
|
||||
}, "reply"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = codec.WriteResponse(&rpc2.Response{
|
||||
Error: utils.ErrMaxConcurentRPCExceededNoCaps.Error(),
|
||||
Seq: 0,
|
||||
}, "reply"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = codec.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewCapsGOBBiRPCCodec(t *testing.T) {
|
||||
conn := new(mockConn)
|
||||
cr := engine.NewCaps(0, utils.MetaBusy)
|
||||
anz := &analyzers.AnalyzerService{}
|
||||
exp := rpc2.NewGobCodec(conn)
|
||||
if r := newCapsBiRPCGOBCodec(conn, cr, nil); !reflect.DeepEqual(r, exp) {
|
||||
t.Errorf("Expected: %v ,received:%v", exp, r)
|
||||
}
|
||||
exp = analyzers.NewAnalyzerBiRPCCodec(rpc2.NewGobCodec(conn), anz, rpcclient.BiRPCGOB, utils.Local, utils.Local)
|
||||
if r := newCapsBiRPCGOBCodec(conn, cr, anz); !reflect.DeepEqual(r, exp) {
|
||||
t.Errorf("Expected: %v ,received:%v", exp, r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewCapsJSONBiRPCCodec(t *testing.T) {
|
||||
conn := new(mockConn)
|
||||
cr := engine.NewCaps(0, utils.MetaBusy)
|
||||
anz := &analyzers.AnalyzerService{}
|
||||
exp := jsonrpc2.NewJSONCodec(conn)
|
||||
if r := newCapsBiRPCJSONCodec(conn, cr, nil); !reflect.DeepEqual(r, exp) {
|
||||
t.Errorf("Expected: %v ,received:%v", exp, r)
|
||||
}
|
||||
exp = analyzers.NewAnalyzerBiRPCCodec(jsonrpc2.NewJSONCodec(conn), anz, rpcclient.BiRPCJSON, utils.Local, utils.Local)
|
||||
if r := newCapsBiRPCJSONCodec(conn, cr, anz); !reflect.DeepEqual(r, exp) {
|
||||
t.Errorf("Expected: %v ,received:%v", exp, r)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user