diff --git a/apier/v1/filters_test.go b/apier/v1/filters_test.go index 24ed46731..902b7cd79 100644 --- a/apier/v1/filters_test.go +++ b/apier/v1/filters_test.go @@ -21,6 +21,7 @@ package v1 import ( "fmt" "reflect" + "sort" "testing" "github.com/cgrates/cgrates/config" @@ -42,17 +43,11 @@ func (ccM *ccMock) Call(serviceMethod string, args interface{}, reply interface{ } func TestFiltersSetFilterReloadCache(t *testing.T) { - tmp := engine.Cache - defer func() { - engine.Cache = tmp - }() - cfg := config.NewDefaultCGRConfig() cfg.GeneralCfg().DefaultCaching = utils.MetaNone cfg.ApierCfg().CachesConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches)} dataDB := engine.NewInternalDB(nil, nil, true) dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil) - engine.Cache = engine.NewCacheS(cfg, dm, nil) expArgs := &utils.AttrReloadCacheWithAPIOpts{ APIOpts: map[string]interface{}{ utils.CacheOpt: utils.MetaReload, @@ -234,4 +229,192 @@ func TestFiltersSetFilterReloadCache(t *testing.T) { if err := apierSv1.SetFilter(arg, &reply); err != nil { t.Error(err) } + + dm.DataDB().Flush(utils.EmptyString) +} + +func TestFiltersSetFilterReloadCache2(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + cfg.ApierCfg().CachesConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches)} + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil) + expArgs := &utils.AttrCacheIDsWithAPIOpts{ + APIOpts: map[string]interface{}{ + utils.CacheOpt: utils.MetaClear, + }, + Tenant: "cgrates.org", + CacheIDs: []string{utils.CacheFilters}, + } + ccM := &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.CacheSv1Clear: func(args, reply interface{}) error { + sort.Strings(args.(*utils.AttrCacheIDsWithAPIOpts).CacheIDs) + if !reflect.DeepEqual(args, expArgs) { + return fmt.Errorf("expected: <%+v>,\nreceived: <%+v>", utils.ToJSON(expArgs), utils.ToJSON(args)) + } + return nil + }, + }, + } + rpcInternal := make(chan rpcclient.ClientConnector, 1) + rpcInternal <- ccM + cM := engine.NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches): rpcInternal, + }) + apierSv1 := &APIerSv1{ + Config: cfg, + DataManager: dm, + ConnMgr: cM, + } + arg := &engine.FilterWithAPIOpts{ + Filter: &engine.Filter{ + ID: "FLTR_ID", + Rules: []*engine.FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Account", + Values: []string{"1001"}, + }, + }, + }, + APIOpts: map[string]interface{}{ + utils.CacheOpt: utils.MetaClear, + }, + } + var reply string + + if err := apierSv1.SetFilter(arg, &reply); err != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, err) + } + + attrPrf := &engine.AttributeProfileWithAPIOpts{ + AttributeProfile: &engine.AttributeProfile{ + FilterIDs: []string{"FLTR_ID"}, + ID: "ATTR_ID", + Contexts: []string{utils.MetaAny}, + Weight: 10, + Attributes: []*engine.Attribute{ + { + Path: "*req.Account", + Value: config.NewRSRParsersMustCompile("1003", ";"), + Type: utils.MetaConstant, + }, + }, + }, + APIOpts: map[string]interface{}{ + utils.CacheOpt: utils.MetaNone, + }, + } + + if err := apierSv1.SetAttributeProfile(attrPrf, &reply); err != nil { + t.Error(err) + } + + thPrf := &engine.ThresholdProfileWithAPIOpts{ + ThresholdProfile: &engine.ThresholdProfile{ + ID: "THD_ID", + FilterIDs: []string{"FLTR_ID"}, + MaxHits: 10, + Weight: 10, + }, + APIOpts: map[string]interface{}{ + utils.CacheOpt: utils.MetaNone, + }, + } + + if err := apierSv1.SetThresholdProfile(thPrf, &reply); err != nil { + t.Error(err) + } + + rsPrf := &engine.ResourceProfileWithAPIOpts{ + ResourceProfile: &engine.ResourceProfile{ + ID: "RES_ID", + FilterIDs: []string{"FLTR_ID"}, + Weight: 10, + }, + APIOpts: map[string]interface{}{ + utils.CacheOpt: utils.MetaNone, + }, + } + + if err := apierSv1.SetResourceProfile(rsPrf, &reply); err != nil { + t.Error(err) + } + + sqPrf := &engine.StatQueueProfileWithAPIOpts{ + StatQueueProfile: &engine.StatQueueProfile{ + ID: "SQ_ID", + FilterIDs: []string{"FLTR_ID"}, + Weight: 10, + }, + APIOpts: map[string]interface{}{ + utils.CacheOpt: utils.MetaNone, + }, + } + + if err := apierSv1.SetStatQueueProfile(sqPrf, &reply); err != nil { + t.Error(err) + } + + dpPrf := &DispatcherWithAPIOpts{ + DispatcherProfile: &engine.DispatcherProfile{ + ID: "DP_ID", + FilterIDs: []string{"FLTR_ID"}, + }, + APIOpts: map[string]interface{}{ + utils.CacheOpt: utils.MetaNone, + }, + } + + if err := apierSv1.SetDispatcherProfile(dpPrf, &reply); err != nil { + t.Error(err) + } + + chgPrf := &ChargerWithAPIOpts{ + ChargerProfile: &engine.ChargerProfile{ + ID: "CHG_ID", + FilterIDs: []string{"FLTR_ID"}, + RunID: "runID", + }, + APIOpts: map[string]interface{}{ + utils.CacheOpt: utils.MetaNone, + }, + } + + if err := apierSv1.SetChargerProfile(chgPrf, &reply); err != nil { + t.Error(err) + } + + arg = &engine.FilterWithAPIOpts{ + Filter: &engine.Filter{ + ID: "FLTR_ID", + Rules: []*engine.FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Account", + Values: []string{"1002"}, + }, + }, + }, + APIOpts: map[string]interface{}{ + utils.CacheOpt: utils.MetaClear, + }, + } + expArgs = &utils.AttrCacheIDsWithAPIOpts{ + APIOpts: map[string]interface{}{ + utils.CacheOpt: utils.MetaClear, + }, + Tenant: "cgrates.org", + CacheIDs: []string{utils.CacheAttributeFilterIndexes, utils.CacheThresholdFilterIndexes, + utils.CacheResourceFilterIndexes, utils.CacheStatFilterIndexes, + utils.CacheChargerFilterIndexes, utils.CacheFilters}, + } + sort.Strings(expArgs.CacheIDs) + + if err := apierSv1.SetFilter(arg, &reply); err != nil { + t.Error(err) + } + + dm.DataDB().Flush(utils.EmptyString) } diff --git a/apier/v1/libapier_test.go b/apier/v1/libapier_test.go index 9f5334405..2d2011e6a 100644 --- a/apier/v1/libapier_test.go +++ b/apier/v1/libapier_test.go @@ -122,6 +122,7 @@ func TestCallCache(t *testing.T) { cn := engine.NewConnManager(config.CgrConfig(), map[string]chan rpcclient.ClientConnector{ utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches): ch, }) + cn.Reload() apv1 := &APIerSv1{ ConnMgr: cn, Config: config.CgrConfig(),