diff --git a/engine/account.go b/engine/account.go index 6499882cb..89bdecef1 100644 --- a/engine/account.go +++ b/engine/account.go @@ -835,7 +835,7 @@ func (account *Account) GetUniqueSharedGroupMembers(cd *CallDescriptor) (utils.S func (acc *Account) Clone() *Account { newAcc := &Account{ ID: acc.ID, - UnitCounters: acc.UnitCounters.Clone(), // not used when cloned (dryRun) + UnitCounters: acc.UnitCounters.Clone(), AllowNegative: acc.AllowNegative, Disabled: acc.Disabled, } diff --git a/engine/units_counter.go b/engine/units_counter.go index 15e911658..45f824509 100644 --- a/engine/units_counter.go +++ b/engine/units_counter.go @@ -51,10 +51,12 @@ func (uc *UnitCounter) Clone() (newUnit *UnitCounter) { } newUnit = &UnitCounter{ CounterType: uc.CounterType, - Counters: make(CounterFilters, len(uc.Counters)), } - for i, counter := range uc.Counters { - newUnit.Counters[i] = counter.Clone() + if uc.Counters != nil { + newUnit.Counters = make(CounterFilters, len(uc.Counters)) + for i, counter := range uc.Counters { + newUnit.Counters[i] = counter.Clone() + } } return newUnit } @@ -70,7 +72,7 @@ func (cfs *CounterFilter) Clone() *CounterFilter { } } -// Clone clones *UnitCounters +// Clone clones UnitCounters func (ucs UnitCounters) Clone() UnitCounters { if ucs == nil { return nil diff --git a/engine/units_counter_test.go b/engine/units_counter_test.go index b2dc0d00d..fc3ea7f4e 100644 --- a/engine/units_counter_test.go +++ b/engine/units_counter_test.go @@ -18,11 +18,134 @@ along with this program. If not, see package engine import ( + "reflect" "testing" "github.com/cgrates/cgrates/utils" ) +func TestUnitCounterClone(t *testing.T) { + var uc *UnitCounter + if rcv := uc.Clone(); rcv != nil { + t.Errorf("Expecting nil, received: %s", utils.ToJSON(rcv)) + } + uc = &UnitCounter{} + eOut := &UnitCounter{} + if rcv := uc.Clone(); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + + uc = &UnitCounter{ + CounterType: "testCounterType", + Counters: []*CounterFilter{ + &CounterFilter{ + Value: 0.7, + }, + &CounterFilter{ + Value: 0.8, + }, + }, + } + eOut = &UnitCounter{ + CounterType: "testCounterType", + Counters: []*CounterFilter{ + &CounterFilter{ + Value: 0.7, + }, + &CounterFilter{ + Value: 0.8, + }, + }, + } + if rcv := uc.Clone(); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } +} + +func TestCounterFilterClone(t *testing.T) { + var cfs *CounterFilter + if rcv := cfs.Clone(); rcv != nil { + t.Errorf("Expecting nil, received: %s", utils.ToJSON(rcv)) + } + cfs = &CounterFilter{} + eOut := &CounterFilter{} + if rcv := cfs.Clone(); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + cfs = &CounterFilter{ + Value: 0.7, + Filter: &BalanceFilter{ + Uuid: utils.StringPointer("testUuid"), + }, + } + eOut = &CounterFilter{ + Value: 0.7, + Filter: &BalanceFilter{ + Uuid: utils.StringPointer("testUuid"), + }, + } + if rcv := cfs.Clone(); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + +} + +func TestUnitCountersClone(t *testing.T) { + var ucs UnitCounters + if rcv := ucs.Clone(); rcv != nil { + t.Errorf("Expecting nil, received: %s", utils.ToJSON(rcv)) + } + ucs = UnitCounters{} + eOut := UnitCounters{} + if rcv := ucs.Clone(); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + ucs = UnitCounters{ + "string1": []*UnitCounter{ + &UnitCounter{ + CounterType: "testCounterType1.1", + }, + &UnitCounter{ + CounterType: "testCounterType1.2", + }, + }, + "string2": []*UnitCounter{ + &UnitCounter{ + CounterType: "testCounterType2.1", + }, + &UnitCounter{ + CounterType: "testCounterType2.2", + }, + }, + } + eOut = UnitCounters{ + "string1": []*UnitCounter{ + &UnitCounter{ + CounterType: "testCounterType1.1", + }, + &UnitCounter{ + CounterType: "testCounterType1.2", + }, + }, + "string2": []*UnitCounter{ + &UnitCounter{ + CounterType: "testCounterType2.1", + }, + &UnitCounter{ + CounterType: "testCounterType2.2", + }, + }, + } + rcv := ucs.Clone() + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + rcv["string1"][0].CounterType = "modified" + if ucs["string1"][0].CounterType == "modified" { + t.Error("Original struct was modified") + } +} + func TestUnitsCounterAddBalance(t *testing.T) { uc := &UnitCounter{ Counters: CounterFilters{&CounterFilter{Value: 1},