mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-16 13:49:53 +05:00
Improving coverage at engine
This commit is contained in:
committed by
Dan Christian Bogos
parent
8c903446b5
commit
bf2f6c0927
@@ -22,6 +22,7 @@ import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/birpc"
|
||||
"github.com/cgrates/birpc/context"
|
||||
@@ -66,9 +67,9 @@ func TestCacheSSetWithReplicateTrue(t *testing.T) {
|
||||
connMgr.AddInternalConn(utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg), utils.CacheSv1, clientconn)
|
||||
|
||||
stopchan := make(chan struct{}, 1)
|
||||
caps := NewCaps(1, utils.MetaBusy)
|
||||
sts := NewCapsStats(cfg.CoreSCfg().CapsStatsInterval, caps, stopchan)
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, sts)
|
||||
close(stopchan)
|
||||
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, nil)
|
||||
|
||||
if err := cacheS.SetWithReplicate(context.Background(), args); err != nil {
|
||||
t.Error(err)
|
||||
@@ -116,9 +117,9 @@ func TestCacheSSetWithReplicateFalse(t *testing.T) {
|
||||
connMgr := NewConnManager(cfg)
|
||||
|
||||
stopchan := make(chan struct{}, 1)
|
||||
caps := NewCaps(1, utils.MetaBusy)
|
||||
sts := NewCapsStats(cfg.CoreSCfg().CapsStatsInterval, caps, stopchan)
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, sts)
|
||||
close(stopchan)
|
||||
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, nil)
|
||||
|
||||
if err := cacheS.SetWithReplicate(context.Background(), args); err != nil {
|
||||
t.Error(err)
|
||||
@@ -156,9 +157,9 @@ func TestCacheSGetWithRemote(t *testing.T) {
|
||||
connMgr.AddInternalConn(utils.ConcatenatedKey(utils.MetaInternal, utils.RemoteConnsCfg), utils.CacheSv1GetItem, clientconn)
|
||||
|
||||
stopchan := make(chan struct{}, 1)
|
||||
caps := NewCaps(1, utils.MetaBusy)
|
||||
sts := NewCapsStats(cfg.CoreSCfg().CapsStatsInterval, caps, stopchan)
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, sts)
|
||||
close(stopchan)
|
||||
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, nil)
|
||||
|
||||
// first we have to set the value in order to get it from our mock
|
||||
cacheS.Set(context.Background(), utils.CacheAccounts, "itemId", "test_value_was_set", []string{}, true, utils.NonTransactional)
|
||||
@@ -182,7 +183,7 @@ func TestCacheSGetWithRemoteFalse(t *testing.T) {
|
||||
args := &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: "cacheID",
|
||||
CacheID: utils.CacheAccounts,
|
||||
ItemID: "itemId",
|
||||
},
|
||||
}
|
||||
@@ -199,12 +200,96 @@ func TestCacheSGetWithRemoteFalse(t *testing.T) {
|
||||
connMgr := NewConnManager(cfg)
|
||||
|
||||
stopchan := make(chan struct{}, 1)
|
||||
caps := NewCaps(1, utils.MetaBusy)
|
||||
sts := NewCapsStats(cfg.CoreSCfg().CapsStatsInterval, caps, stopchan)
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, sts)
|
||||
close(stopchan)
|
||||
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, nil)
|
||||
|
||||
var reply interface{} = utils.OK
|
||||
if err := cacheS.V1GetItemWithRemote(context.Background(), args, &reply); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expected error <%v>, received error <%v>", utils.ErrNotFound, err)
|
||||
}
|
||||
}
|
||||
func TestRemoveWithoutReplicate(t *testing.T) {
|
||||
Cache.Clear(nil)
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
db := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
dm := NewDataManager(db, cfg.CacheCfg(), nil)
|
||||
connMgr := NewConnManager(cfg)
|
||||
chS := NewCacheS(cfg, dm, connMgr, nil)
|
||||
|
||||
chS.tCache.Set(utils.CacheAccounts, "itemId", "value", nil, true, utils.NonTransactional)
|
||||
|
||||
chS.RemoveWithoutReplicate(utils.CacheAccounts, "itemId", true, utils.NonTransactional)
|
||||
if _, has := chS.tCache.Get(utils.CacheAccounts, "itemId"); has {
|
||||
t.Error("This itemId shouldn't exist")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestV1GetItemExpiryTimeFromCacheErr(t *testing.T) {
|
||||
Cache.Clear(nil)
|
||||
args := &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheAccounts,
|
||||
ItemID: "itemId",
|
||||
},
|
||||
}
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
db := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
dm := NewDataManager(db, cfg.CacheCfg(), nil)
|
||||
cfg.CacheCfg().RemoteConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.RemoteConnsCfg)}
|
||||
cfg.CacheCfg().Partitions = map[string]*config.CacheParamCfg{}
|
||||
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, nil)
|
||||
|
||||
var reply time.Time
|
||||
if err := cacheS.V1GetItemExpiryTime(context.Background(), args, &reply); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expected error <%v>, Received error <%v>", utils.ErrNotFound, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestV1GetItemErr(t *testing.T) {
|
||||
Cache.Clear(nil)
|
||||
args := &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheAccounts,
|
||||
ItemID: "itemId",
|
||||
},
|
||||
}
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
db := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
dm := NewDataManager(db, cfg.CacheCfg(), nil)
|
||||
cfg.CacheCfg().RemoteConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.RemoteConnsCfg)}
|
||||
cfg.CacheCfg().Partitions = map[string]*config.CacheParamCfg{}
|
||||
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, nil)
|
||||
|
||||
var reply interface{}
|
||||
if err := cacheS.V1GetItem(context.Background(), args, &reply); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expected error <%v>, Received error <%v>", utils.ErrNotFound, err)
|
||||
}
|
||||
|
||||
}
|
||||
func TestV1GetItemIDsErr(t *testing.T) {
|
||||
Cache.Clear(nil)
|
||||
args := &utils.ArgsGetCacheItemIDsWithAPIOpts{
|
||||
ArgsGetCacheItemIDs: utils.ArgsGetCacheItemIDs{
|
||||
CacheID: utils.CacheAccounts,
|
||||
ItemIDPrefix: "itemId",
|
||||
},
|
||||
}
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
db := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
dm := NewDataManager(db, cfg.CacheCfg(), nil)
|
||||
cfg.CacheCfg().RemoteConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.RemoteConnsCfg)}
|
||||
cfg.CacheCfg().Partitions = map[string]*config.CacheParamCfg{}
|
||||
|
||||
cacheS := NewCacheS(cfg, dm, connMgr, nil)
|
||||
|
||||
var reply []string
|
||||
if err := cacheS.V1GetItemIDs(context.Background(), args, &reply); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expected error <%v>, Received error <%v>", utils.ErrNotFound, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2942,58 +2942,62 @@ func TestCDRsProcessEventMockThdsEcCostIfaceUnmarshalErr(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
//unfinished (solo cover it)
|
||||
// func TestCDRsV1ProcessEventWithGetMockCacheErrResp(t *testing.T) {
|
||||
// testCache := Cache
|
||||
// tmpC := config.CgrConfig()
|
||||
// tmpCM := connMgr
|
||||
// defer func() {
|
||||
// Cache = testCache
|
||||
// config.SetCgrConfig(tmpC)
|
||||
// connMgr = tmpCM
|
||||
// }()
|
||||
func TestCDRsV1ProcessEventWithGetMockCacheErrResp(t *testing.T) {
|
||||
|
||||
// cfg := config.NewDefaultCGRConfig()
|
||||
// cfg.CacheCfg().Partitions[utils.CacheRPCResponses].Limit = 1
|
||||
testCache := Cache
|
||||
tmpC := config.CgrConfig()
|
||||
tmpCM := connMgr
|
||||
defer func() {
|
||||
Cache = testCache
|
||||
config.SetCgrConfig(tmpC)
|
||||
connMgr = tmpCM
|
||||
}()
|
||||
Cache.Clear(nil)
|
||||
|
||||
// data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
// dm := NewDataManager(data, cfg.CacheCfg(), nil)
|
||||
// fltrs := NewFilterS(cfg, nil, dm)
|
||||
// Cache = NewCacheS(cfg, dm, nil, nil)
|
||||
// newCDRSrv := NewCDRServer(cfg, dm, fltrs, nil)
|
||||
// cgrEv := &utils.CGREvent{
|
||||
// Tenant: "cgrates.org",
|
||||
// ID: "testID",
|
||||
// Event: map[string]interface{}{
|
||||
// utils.Cost: 123,
|
||||
// },
|
||||
// }
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
cfg.CacheCfg().Partitions[utils.CacheRPCResponses].Limit = 1
|
||||
config.SetCgrConfig(cfg)
|
||||
|
||||
// var evs []*utils.EventsWithOpts
|
||||
// Cache.Set(context.Background(), utils.CacheRPCResponses, "CDRsV1.ProcessEventWithGet:testID",
|
||||
// &utils.CachedRPCResponse{Result: &evs, Error: nil},
|
||||
// nil, true, utils.NonTransactional)
|
||||
data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
dm := NewDataManager(data, cfg.CacheCfg(), nil)
|
||||
fltrs := NewFilterS(cfg, nil, dm)
|
||||
Cache = NewCacheS(cfg, dm, nil, nil)
|
||||
newCDRSrv := NewCDRServer(cfg, dm, fltrs, nil)
|
||||
|
||||
// err := newCDRSrv.V1ProcessEventWithGet(context.Background(), cgrEv, &evs)
|
||||
// if err != nil {
|
||||
// t.Errorf("\nExpected <%+v> \n, received <%+v>", nil, err)
|
||||
// }
|
||||
cgrEv := &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "testID",
|
||||
Event: map[string]interface{}{
|
||||
utils.Cost: 123,
|
||||
},
|
||||
APIOpts: map[string]interface{}{},
|
||||
}
|
||||
|
||||
// expectedVal := &utils.EventsWithOpts{}
|
||||
// if val, ok := Cache.Get(utils.CacheRPCResponses, "CDRsV1.ProcessEventWithGet:testID"); !ok {
|
||||
// t.Error("Expected value", val)
|
||||
// } else {
|
||||
// valConverted, canCast := val.(*utils.CachedRPCResponse)
|
||||
// if !canCast {
|
||||
// t.Error("Should cast")
|
||||
// }
|
||||
// if valConverted.Error != nil {
|
||||
// t.Errorf("Expected error <%v>, Received error <%v>", expectedVal, valConverted.Error)
|
||||
// } else if valConverted.Error == nil {
|
||||
// evs = *valConverted.Result.(*[]*utils.EventsWithOpts)
|
||||
// }
|
||||
// if !reflect.DeepEqual(expectedVal, valConverted.Result) {
|
||||
// t.Errorf("Expected %v, received %v", utils.ToJSON(expectedVal), utils.ToJSON(valConverted))
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
evs := []*utils.EventsWithOpts{
|
||||
{
|
||||
Event: map[string]interface{}{
|
||||
utils.Cost: 666,
|
||||
},
|
||||
},
|
||||
}
|
||||
Cache.Set(context.Background(), utils.CacheRPCResponses, "CDRsV1.ProcessEventWithGet:testID",
|
||||
&utils.CachedRPCResponse{Result: &evs, Error: nil},
|
||||
nil, true, utils.NonTransactional)
|
||||
var reply []*utils.EventsWithOpts
|
||||
err := newCDRSrv.V1ProcessEventWithGet(context.Background(), cgrEv, &reply)
|
||||
if err != nil {
|
||||
t.Errorf("\nExpected <%+v> \n, received <%+v>", nil, err)
|
||||
}
|
||||
|
||||
expectedVal := []*utils.EventsWithOpts{
|
||||
{
|
||||
Event: map[string]interface{}{
|
||||
utils.Cost: 666,
|
||||
},
|
||||
},
|
||||
}
|
||||
if !reflect.DeepEqual(expectedVal, reply) {
|
||||
t.Errorf("Expected %v, received %v", utils.ToJSON(expectedVal), utils.ToJSON(reply))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -589,3 +589,31 @@ func TestCMDeadLock(t *testing.T) {
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func TestCMEnableDispatcher(t *testing.T) {
|
||||
Cache.Clear(nil)
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
cM := NewConnManager(cfg)
|
||||
data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
dm := NewDataManager(data, cfg.CacheCfg(), nil)
|
||||
fltrs := NewFilterS(cfg, nil, dm)
|
||||
Cache = NewCacheS(cfg, dm, nil, nil)
|
||||
newCDRSrv := NewCDRServer(cfg, dm, fltrs, nil)
|
||||
|
||||
srvcNames := []string{utils.AccountS, utils.ActionS, utils.AttributeS,
|
||||
utils.CacheS, utils.ChargerS, utils.ConfigS, utils.DispatcherS,
|
||||
utils.GuardianS, utils.RateS, utils.ResourceS, utils.RouteS,
|
||||
utils.SessionS, utils.StatS, utils.ThresholdS, utils.CDRs,
|
||||
utils.ReplicatorS, utils.EeS, utils.CoreS, utils.AnalyzerS,
|
||||
utils.AdminS, utils.LoaderS, utils.ServiceManager}
|
||||
|
||||
for _, name := range srvcNames {
|
||||
|
||||
newSrvcWName, err := NewServiceWithName(newCDRSrv, name, true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
cM.EnableDispatcher(newSrvcWName)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2069,3 +2069,56 @@ func TestFiltersFilterRuleIsValid(t *testing.T) {
|
||||
t.Error("filter should be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPassPartialErr(t *testing.T) {
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
dmFilterPass := NewDataManager(data, config.CgrConfig().CacheCfg(), nil)
|
||||
filterS := FilterS{
|
||||
cfg: cfg,
|
||||
dm: dmFilterPass,
|
||||
}
|
||||
passEvent := map[string]interface{}{
|
||||
"Account": "1007",
|
||||
}
|
||||
fEv := utils.MapStorage{}
|
||||
fEv.Set([]string{utils.MetaReq}, passEvent)
|
||||
prefixes := []string{utils.DynamicDataPrefix + utils.MetaReq}
|
||||
expErr := "NOT_FOUND:bad fltr"
|
||||
if _, _, err := filterS.LazyPass(context.Background(), "cgrates.org",
|
||||
[]string{"bad fltr"}, fEv, prefixes); err == nil || err.Error() != expErr {
|
||||
t.Errorf("Expected error <%v>, Received error <%v> ", expErr, err.Error())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNewFilterRuleErrSupportedFltrType(t *testing.T) {
|
||||
expErr := "Unsupported filter Type: unsupported"
|
||||
if _, err := NewFilterRule("unsupported", "", []string{}); err == nil || err.Error() != expErr {
|
||||
t.Errorf("Expected error <%v>, Received error <%v> ", expErr, err.Error())
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewFilterRuleErrNoFldName(t *testing.T) {
|
||||
expErr := "Element is mandatory for Type: *cronexp"
|
||||
if _, err := NewFilterRule(utils.MetaCronExp, "", []string{}); err == nil || err.Error() != expErr {
|
||||
t.Errorf("Expected error <%v>, Received error <%v> ", expErr, err.Error())
|
||||
|
||||
}
|
||||
}
|
||||
func TestNewFilterRuleErrNoVals(t *testing.T) {
|
||||
expErr := "Values is mandatory for Type: *cronexp"
|
||||
if _, err := NewFilterRule(utils.MetaCronExp, "~*req.AnswerTime", []string{}); err == nil || err.Error() != expErr {
|
||||
t.Errorf("Expected error <%v>, Received error <%v> ", expErr, err.Error())
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestPassNever(t *testing.T) {
|
||||
fltr := &FilterRule{}
|
||||
dDP := utils.MapStorage{}
|
||||
if ok, err := fltr.passNever(dDP); ok != false && err != nil {
|
||||
t.Errorf("Expected filter to never pass, unexpected error <%v>", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user