From 710b6344fa77df4b38d6b6d5f4b02f077702dfff Mon Sep 17 00:00:00 2001 From: gezimbll Date: Thu, 24 Nov 2022 09:24:10 -0500 Subject: [PATCH] caches:improving coverage test --- engine/caches_test.go | 462 ++++++++++++++++++++++++++++++++++++++++++ engine/cdrs_test.go | 5 +- 2 files changed, 466 insertions(+), 1 deletion(-) create mode 100644 engine/caches_test.go diff --git a/engine/caches_test.go b/engine/caches_test.go new file mode 100644 index 000000000..5add5afd6 --- /dev/null +++ b/engine/caches_test.go @@ -0,0 +1,462 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ +package engine + +import ( + "reflect" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/ltcache" + "github.com/cgrates/rpcclient" +) + +func TestCachesReplicateRemove(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.CacheCfg().ReplicationConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg)} + cfg.CacheCfg().Partitions = map[string]*config.CacheParamCfg{ + "id": { + Replicate: true, + }, + } + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + clientconn := make(chan rpcclient.ClientConnector, 1) + clientconn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.CacheSv1ReplicateRemove: func(args, reply interface{}) error { + *reply.(*string) = "reply" + return nil + }, + }, + } + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg): clientconn, + }) + chS := CacheS{ + cfg: cfg, + dm: dm, + } + SetConnManager(connMgr) + if err := chS.ReplicateRemove("id", "itm_id"); err != nil { + + t.Error(err) + + } +} + +func TestCacheSSetWithReplicate(t *testing.T) { + Cache.Clear(nil) + args := &utils.ArgCacheReplicateSet{ + CacheID: "chID", + ItemID: "itemID", + Value: &utils.CachedRPCResponse{Result: "reply", Error: nil}, + GroupIDs: []string{"groupId", "groupId"}, + } + cfg := config.NewDefaultCGRConfig() + cfg.CacheCfg().ReplicationConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg)} + cfg.CacheCfg().Partitions = map[string]*config.CacheParamCfg{ + args.CacheID: { + Replicate: true, + }, + } + + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + clientconn := make(chan rpcclient.ClientConnector, 1) + clientconn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.CacheSv1ReplicateSet: func(args, reply interface{}) error { + *reply.(*string) = "reply" + return nil + }, + }, + } + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg): clientconn, + }) + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + ltcache.DefaultCacheInstance: { + MaxItems: 3, + TTL: time.Second * 1, + StaticTTL: true, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + }, + ) + casheS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + SetConnManager(connMgr) + + if err := casheS.SetWithReplicate(args); err != nil { + t.Error(err) + } +} + +func TestCacheSV1GetItemIDs(t *testing.T) { + args := &utils.ArgsGetCacheItemIDsWithAPIOpts{ + APIOpts: map[string]interface{}{}, + Tenant: "cgrates.org", + ArgsGetCacheItemIDs: utils.ArgsGetCacheItemIDs{ + CacheID: "cacheID", + ItemIDPrefix: "itemID", + }, + } + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + "cacheID": { + MaxItems: 3, + TTL: time.Second * 1, + StaticTTL: true, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + }) + tscache.Set("cacheID", "itemID", "", []string{}, true, "tId") + + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + reply := &[]string{} + exp := &[]string{"itemID"} + if err := chS.V1GetItemIDs(args, reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(reply, exp) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(exp), utils.ToJSON(reply)) + } +} + +func TestCacheSV1HasItem(t *testing.T) { + args := &utils.ArgsGetCacheItemWithAPIOpts{ + APIOpts: map[string]interface{}{}, + Tenant: "cgrates.org", + ArgsGetCacheItem: utils.ArgsGetCacheItem{ + CacheID: "cacheID", + ItemID: "itemID", + }, + } + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + "cacheID": { + MaxItems: 3, + TTL: time.Second * 1, + StaticTTL: true, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + }) + tscache.Set("cacheID", "itemID", "", []string{}, true, "tId") + + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + reply := utils.BoolPointer(false) + if err := chS.V1HasItem(args, reply); err != nil { + t.Error(err) + } +} + +func TestCacheSV1GetItemWithRemote(t *testing.T) { + args := &utils.ArgsGetCacheItemWithAPIOpts{ + + ArgsGetCacheItem: utils.ArgsGetCacheItem{ + CacheID: "cacheID", + ItemID: "itemId", + }, + } + cfg := config.NewDefaultCGRConfig() + cfg.CacheCfg().RemoteConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.RemoteConnsCfg)} + cfg.CacheCfg().Partitions = map[string]*config.CacheParamCfg{ + args.CacheID: { + Remote: true, + }, + } + clientconn := make(chan rpcclient.ClientConnector, 1) + clientconn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.CacheSv1GetItem: func(args, reply interface{}) error { + + return nil + }, + }, + } + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.RemoteConnsCfg): clientconn, + }, + ) + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + "cacheID": { + MaxItems: 3, + TTL: time.Second * 1, + StaticTTL: true, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + }) + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + SetConnManager(connMgr) + var reply interface{} = "str" + if err := chS.V1GetItemWithRemote(args, &reply); err != nil { + t.Error(err) + } +} + +func TestCacheSV1GetItem(t *testing.T) { + args := &utils.ArgsGetCacheItemWithAPIOpts{ + ArgsGetCacheItem: utils.ArgsGetCacheItem{ + CacheID: "cacheID", + ItemID: "itemID", + }, + } + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + "cacheID": { + MaxItems: 3, + TTL: time.Second * 1, + StaticTTL: true, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + }) + tscache.Set("cacheID", "itemID", "value", []string{}, true, "tId") + + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + var reply interface{} = "" + if err := chS.V1GetItem(args, &reply); err != nil { + t.Error(err) + } + +} + +func TestCacheSV1GetItemExpiryTime(t *testing.T) { + args := &utils.ArgsGetCacheItemWithAPIOpts{ + ArgsGetCacheItem: utils.ArgsGetCacheItem{ + CacheID: "cacheID", + ItemID: "itemID", + }, + } + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + "cacheID": { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + }) + tscache.Set("cacheID", "itemID", "value", []string{}, true, "tId") + + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + reply := now + loc, _ := time.LoadLocation("EST") + exp := now.Add(30 * time.Minute).In(loc).Minute() + if err := chS.V1GetItemExpiryTime(args, &reply); err != nil { + t.Error(err) + } else if reply.Minute() != exp { + t.Errorf("expected %+v,received %+v", exp, reply) + } + +} + +func TestCacheSV1RemoveItem(t *testing.T) { + args := &utils.ArgsGetCacheItemWithAPIOpts{ + ArgsGetCacheItem: utils.ArgsGetCacheItem{ + CacheID: "cacheID", + ItemID: "itemID", + }, + } + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + "cacheID": { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + }) + tscache.Set("cacheID", "itemID", "value", []string{}, true, "tId") + + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + reply := "error" + if err := chS.V1RemoveItem(args, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("expected %v,received %v", utils.OK, reply) + } +} + +func TestCacheSV1RemoveItems(t *testing.T) { + args := utils.NewAttrReloadCacheWithOpts() + args.DestinationIDs = []string{"dest1", "dest2", "dest3"} + args.ResourceIDs = []string{"res1", "res2", "res3"} + args.FilterIDs = []string{"fltr1", "filtr2", "filtr3"} + cfgCache := map[string]*ltcache.CacheConfig{ + utils.CacheDestinations: { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + }, + }, + utils.CacheResources: { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + utils.CacheFilters: { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + } + args2 := map[string][]string{ + utils.CacheDestinations: {"dest1", "dest2", "dest3"}, + utils.CacheResources: {"res1", "res2", "res3"}, + utils.CacheFilters: {"fltr1", "filtr2", "filtr3"}, + } + tscache := ltcache.NewTransCache(cfgCache) + + for keyId := range cfgCache { + if itemids, has := args2[keyId]; has { + for _, itemid := range itemids { + tscache.Set(keyId, itemid, "value", []string{}, true, "tId") + } + } + } + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + + reply := "error" + + if err := chS.V1RemoveItems(args, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("expected %v,received %v", utils.OK, reply) + } +} + +func TestCacheSV1Clear(t *testing.T) { + args := &utils.AttrCacheIDsWithAPIOpts{ + APIOpts: map[string]interface{}{}, + Tenant: "cgrates.org", + CacheIDs: []string{"cacheID", "cacheID2", "cacheID3"}, + } + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + + cfgCache := map[string]*ltcache.CacheConfig{ + "cacheID": { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + }, + }, + "cacheID2": { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + "cacheID#": { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + + }, + }, + } + tscache := ltcache.NewTransCache(cfgCache) + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + reply := "error" + if err := chS.V1Clear(args, &reply); err != nil { + t.Error(err) + } +} diff --git a/engine/cdrs_test.go b/engine/cdrs_test.go index a25981443..84ff39989 100644 --- a/engine/cdrs_test.go +++ b/engine/cdrs_test.go @@ -1525,8 +1525,11 @@ func TestV2StoreSessionCost2(t *testing.T) { Tenant: "cgrates.org", APIOpts: map[string]interface{}{}, } - if err := cdrS.V2StoreSessionCost(args, utils.StringPointer("reply")); err != nil { + reply := utils.StringPointer("reply") + if err := cdrS.V2StoreSessionCost(args, reply); err != nil { t.Error(err) + } else if *reply != utils.OK { + t.Errorf("expected %+v,received %+v", utils.OK, *reply) } }