From 73d8147a5676973a826ebea400fde683d561ad0b Mon Sep 17 00:00:00 2001 From: rbarrabe Date: Fri, 25 Nov 2016 10:18:26 +0100 Subject: [PATCH 1/2] Update storage_mongo_datadb.go --- engine/storage_mongo_datadb.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 0b898de29..36c665a13 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -896,12 +896,15 @@ func (ms *MongoStorage) UpdateReverseDestination(oldDest, newDest *Destination, func (ms *MongoStorage) GetActions(key string, skipCache bool, transactionID string) (as Actions, err error) { if !skipCache { - if x, ok := cache.Get(utils.ACTION_PREFIX + key); ok { - if x != nil { - return x.(Actions), nil - } - return nil, utils.ErrNotFound - } + if x, err := cache.GetCloned(utils.ACTION_PREFIX + key); err != nil { + if err.Error() != utils.ItemNotFound { + return nil, err + } + } else if x == nil { + return nil, utils.ErrNotFound + } else { + return x.(Actions), nil + } } var result struct { Key string From d3f71279c49e2217f405ec7f63c7ca8ac581dfde Mon Sep 17 00:00:00 2001 From: Regis Date: Fri, 25 Nov 2016 13:56:16 +0100 Subject: [PATCH 2/2] GetClone for actions --- engine/action.go | 20 ++++++++------- engine/actions_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/engine/action.go b/engine/action.go index 824a902b6..a2fc70e3b 100644 --- a/engine/action.go +++ b/engine/action.go @@ -81,15 +81,9 @@ const ( ) func (a *Action) Clone() *Action { - return &Action{ - Id: a.Id, - ActionType: a.ActionType, - //BalanceType: a.BalanceType, - ExtraParameters: a.ExtraParameters, - ExpirationString: a.ExpirationString, - Weight: a.Weight, - Balance: a.Balance, - } + var clonedAction Action + utils.Clone(a, &clonedAction) + return &clonedAction } type actionTypeFunc func(*Account, *StatsQueueTriggered, *Action, Actions) error @@ -754,3 +748,11 @@ func (apl Actions) Less(j, i int) bool { func (apl Actions) Sort() { sort.Sort(apl) } + +func (apl *Actions) Clone() (interface{}, error) { + cln := new(Actions) + if err := utils.Clone(*apl, cln); err != nil { + return nil, err + } + return interface{}(cln), nil +} diff --git a/engine/actions_test.go b/engine/actions_test.go index e2734c52a..fa985e4e5 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -2318,6 +2318,63 @@ func TestValueFormulaDebit(t *testing.T) { } } +func TestClonedAction(t *testing.T) { + a := &Action{ + Id: "test1", + ActionType: TOPUP, + Balance: &BalanceFilter{ + ID: utils.StringPointer("*default"), + Value: &utils.ValueFormula{Static: 1}, + Type: utils.StringPointer(utils.MONETARY), + }, + Weight: float64(10), + } + + clone := a.Clone() + + if !reflect.DeepEqual(a, clone) { + t.Error("error cloning action: ", utils.ToIJSON(clone)) + } +} + +func TestClonedActions(t *testing.T) { + actions := &Actions{ + &Action{ + Id: "RECUR_FOR_V3HSILLMILLD1G", + ActionType: TOPUP, + Balance: &BalanceFilter{ + ID: utils.StringPointer("*default"), + Value: &utils.ValueFormula{Static: 1}, + Type: utils.StringPointer(utils.MONETARY), + }, + Weight: float64(30), + }, + &Action{ + Id: "RECUR_FOR_V3HSILLMILLD5G", + ActionType: DEBIT, + Balance: &BalanceFilter{ + ID: utils.StringPointer("*default"), + Value: &utils.ValueFormula{Static: 2}, + Type: utils.StringPointer(utils.MONETARY), + }, + Weight: float64(20), + }, + } + + clone, err := actions.Clone() + + if err != nil { + t.Error("error cloning actions: ", err) + } + + clonedActions := clone.(*Actions) + + if !reflect.DeepEqual(actions, clonedActions) { + t.Error("error cloning actions: ", utils.ToIJSON(clonedActions)) + } + +} + /**************** Benchmarks ********************************/ func BenchmarkUUID(b *testing.B) {