Fixed cache test + context in cache apis

This commit is contained in:
porosnicuadrian
2021-04-26 11:38:46 +03:00
committed by Dan Christian Bogos
parent 54b1ab267f
commit 6ff4815643
3 changed files with 27 additions and 47 deletions

View File

@@ -38,15 +38,15 @@ func NewCacheSv1(cacheS *engine.CacheS) *CacheSv1 {
}
// GetItemIDs returns the IDs for cacheID with given prefix
func (chSv1 *CacheSv1) GetItemIDs(_ *context.Context, args *utils.ArgsGetCacheItemIDsWithAPIOpts,
func (chSv1 *CacheSv1) GetItemIDs(ctx *context.Context, args *utils.ArgsGetCacheItemIDsWithAPIOpts,
reply *[]string) error {
return chSv1.cacheS.V1GetItemIDs(args, reply)
return chSv1.cacheS.V1GetItemIDs(ctx, args, reply)
}
// HasItem verifies the existence of an Item in cache
func (chSv1 *CacheSv1) HasItem(_ *context.Context, args *utils.ArgsGetCacheItemWithAPIOpts,
func (chSv1 *CacheSv1) HasItem(ctx *context.Context, args *utils.ArgsGetCacheItemWithAPIOpts,
reply *bool) error {
return chSv1.cacheS.V1HasItem(args, reply)
return chSv1.cacheS.V1HasItem(ctx, args, reply)
}
// GetItemExpiryTime returns the expiryTime for an item

View File

@@ -19,65 +19,46 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package apis
import (
"reflect"
"testing"
"github.com/cgrates/birpc"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
func TestCacheHasItem(t *testing.T) {
func TestCacheHasItemAndGetItem(t *testing.T) {
cfg := config.NewDefaultCGRConfig()
data := engine.NewInternalDB(nil, nil, true)
client := make(chan birpc.ClientConnector, 1)
cfg.AdminSCfg().CachesConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches)}
connMngr := engine.NewConnManager(cfg, map[string]chan birpc.ClientConnector{
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches): client,
})
dm := engine.NewDataManager(data, cfg.CacheCfg(), connMngr)
attrPrf := &engine.AttributeWithAPIOpts{
APIAttributeProfile: &engine.APIAttributeProfile{
Tenant: utils.CGRateSorg,
ID: "TestGetAttributeProfile",
Attributes: []*engine.ExternalAttribute{
{
Path: "*req.RequestType",
Type: utils.MetaConstant,
Value: utils.MetaPrepaid,
},
},
},
APIOpts: map[string]interface{}{
utils.CacheOpt: utils.MetaLoad,
},
}
var reply string
adms := NewAdminSv1(cfg, dm, connMngr)
ctx, cancel := context.WithTimeout(context.Background(), 10)
expected := "SERVER_ERROR: context deadline exceeded"
if err := adms.SetAttributeProfile(ctx, attrPrf, &reply); err == nil || err.Error() != expected {
t.Errorf("Expected %+v, received %+v", expected, err)
}
cancel()
dm := engine.NewDataManager(data, cfg.CacheCfg(), nil)
ch := engine.NewCacheS(cfg, dm, nil)
ch.SetWithoutReplicate(utils.CacheAttributeProfiles, "cgrates.org:TestGetAttributeProfile", nil, nil, true, utils.NonTransactional)
cache := NewCacheSv1(ch)
var replyBool bool
args := &utils.ArgsGetCacheItemWithAPIOpts{
argsHasItem := &utils.ArgsGetCacheItemWithAPIOpts{
ArgsGetCacheItem: utils.ArgsGetCacheItem{
CacheID: utils.CacheAttributeProfiles,
ItemID: "cgrates.org:TestGetAttributeProfile",
},
}
if err := cache.HasItem(nil, args, &replyBool); err != nil {
if err := cache.HasItem(nil, argsHasItem, &replyBool); err != nil {
t.Error(err)
} else if replyBool {
} else if !replyBool {
t.Errorf("Unexpected replyBool returned")
}
argsGetItem := &utils.ArgsGetCacheItemIDsWithAPIOpts{
ArgsGetCacheItemIDs: utils.ArgsGetCacheItemIDs{
CacheID: utils.CacheAttributeProfiles,
},
}
var reply []string
expectedRPly := []string{"cgrates.org:TestGetAttributeProfile"}
if err := cache.GetItemIDs(nil, argsGetItem, &reply); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expectedRPly, reply) {
t.Errorf("Expected %+v, received %+v", expectedRPly, reply)
}
}

View File

@@ -253,8 +253,7 @@ func (chS *CacheS) Precache() (err error) {
}
// APIs start here
func (chS *CacheS) V1GetItemIDs(args *utils.ArgsGetCacheItemIDsWithAPIOpts,
func (chS *CacheS) V1GetItemIDs(_ *context.Context, args *utils.ArgsGetCacheItemIDsWithAPIOpts,
reply *[]string) (err error) {
itmIDs := chS.tCache.GetItemIDs(args.CacheID, args.ItemIDPrefix)
if len(itmIDs) == 0 {
@@ -264,7 +263,7 @@ func (chS *CacheS) V1GetItemIDs(args *utils.ArgsGetCacheItemIDsWithAPIOpts,
return
}
func (chS *CacheS) V1HasItem(args *utils.ArgsGetCacheItemWithAPIOpts,
func (chS *CacheS) V1HasItem(_ *context.Context, args *utils.ArgsGetCacheItemWithAPIOpts,
reply *bool) (err error) {
*reply = chS.tCache.HasItem(args.CacheID, args.ItemID)
return