Merge pull request #599 from rbarrabe/patch-4

GetClone for actions
This commit is contained in:
Dan Christian Bogos
2016-11-25 14:38:12 +01:00
committed by GitHub
3 changed files with 77 additions and 15 deletions

View File

@@ -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
}

View File

@@ -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) {

View File

@@ -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