mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
RemoveStaleReferences only at end of Trim, better testing of EC.Trim
This commit is contained in:
@@ -316,7 +316,7 @@ func (ec *EventCost) ratingGetUUIDFomEventCost(oEC *EventCost, oRatingUUID strin
|
||||
|
||||
// accountingGetUUIDFromEventCost retrieves UUID based on data from another EventCost
|
||||
func (ec *EventCost) accountingGetUUIDFromEventCost(oEC *EventCost, oBalanceChargeUUID string) string {
|
||||
if oBalanceChargeUUID == "" {
|
||||
if oBalanceChargeUUID == "" || oBalanceChargeUUID == utils.META_NONE {
|
||||
return ""
|
||||
}
|
||||
oBC := oEC.Accounting[oBalanceChargeUUID].Clone()
|
||||
@@ -511,7 +511,7 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error
|
||||
var incrementsUsage time.Duration
|
||||
for i, cIt := range lastActiveCIl.Increments {
|
||||
incrementsUsage += cIt.TotalUsage()
|
||||
if incrementsUsage > atUsage {
|
||||
if incrementsUsage >= atUsage {
|
||||
lastActiveCItIdx = utils.IntPointer(i)
|
||||
break
|
||||
}
|
||||
@@ -559,12 +559,10 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error
|
||||
ec.Charges = append(ec.Charges, lastActiveCIl.Clone())
|
||||
lastActiveCIl = ec.Charges[len(ec.Charges)-1]
|
||||
lastActiveCIl.CompressFactor = 1
|
||||
|
||||
}
|
||||
srplsCIl := lastActiveCIl.Clone()
|
||||
srplsCIl.Increments = srplsIncrements
|
||||
srplusEC.Charges = append([]*ChargingInterval{srplsCIl}, srplusEC.Charges...)
|
||||
|
||||
lastActiveCIl.Increments = make([]*ChargingIncrement, len(lastActiveCIts))
|
||||
for i, incr := range lastActiveCIts {
|
||||
lastActiveCIl.Increments[i] = incr.Clone() // avoid pointer references to the other interval
|
||||
@@ -573,7 +571,6 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error
|
||||
lastActiveCIl.Increments[len(lastActiveCIl.Increments)-1].CompressFactor = laItCF // correct the compressFactor for the last increment
|
||||
}
|
||||
}
|
||||
ec.RemoveStaleReferences()
|
||||
ec.ResetCounters()
|
||||
if usage := ec.ComputeUsage(); usage < atUsage {
|
||||
return nil, errors.New("usage of EventCost smaller than requested")
|
||||
@@ -590,5 +587,6 @@ func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error
|
||||
incr.BalanceChargeUUID = srplusEC.accountingGetUUIDFromEventCost(ec, incr.BalanceChargeUUID)
|
||||
}
|
||||
}
|
||||
ec.RemoveStaleReferences() // data should be transfered by now, can clean the old one
|
||||
return
|
||||
}
|
||||
|
||||
@@ -920,7 +920,7 @@ func TestECTrimZeroAndFull(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestECTrimMiddle(t *testing.T) {
|
||||
func TestECTrimMiddle1(t *testing.T) {
|
||||
// trim in the middle of increments
|
||||
ec := testEC.Clone()
|
||||
eEC := testEC.Clone()
|
||||
@@ -1027,3 +1027,62 @@ func TestECTrimMiddle(t *testing.T) {
|
||||
//t.Errorf("Expecting: %s, received: %s", utils.ToJSON(eSrplsEC), utils.ToJSON(srplsEC))
|
||||
}
|
||||
}
|
||||
|
||||
// TestECTrimMUsage is targeting simpler testing of the durations trimmed/remainders
|
||||
func TestECTrimMUsage(t *testing.T) {
|
||||
ec := testEC.Clone()
|
||||
t.Logf("ec: %s", utils.ToJSON(ec))
|
||||
atUsage := time.Duration(5 * time.Second)
|
||||
srplsEC, _ := ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != atUsage {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(295*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
ec = testEC.Clone()
|
||||
atUsage = time.Duration(10 * time.Second)
|
||||
srplsEC, _ = ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != atUsage {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(290*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC))
|
||||
}
|
||||
ec = testEC.Clone()
|
||||
atUsage = time.Duration(15 * time.Second)
|
||||
srplsEC, _ = ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != time.Duration(20*time.Second) {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(280*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC))
|
||||
}
|
||||
ec = testEC.Clone()
|
||||
atUsage = time.Duration(25 * time.Second)
|
||||
srplsEC, _ = ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != time.Duration(30*time.Second) {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(270*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC))
|
||||
}
|
||||
ec = testEC.Clone()
|
||||
atUsage = time.Duration(38 * time.Second)
|
||||
srplsEC, _ = ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != atUsage {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(262*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC))
|
||||
}
|
||||
ec = testEC.Clone()
|
||||
atUsage = time.Duration(61 * time.Second)
|
||||
srplsEC, _ = ec.Trim(atUsage)
|
||||
if ec.ComputeUsage() != atUsage {
|
||||
t.Errorf("Wrongly trimmed EC: %s", utils.ToJSON(ec))
|
||||
}
|
||||
if srplsEC.ComputeUsage() != time.Duration(239*time.Second) {
|
||||
t.Errorf("Wrong surplusEC: %s", utils.ToJSON(srplsEC))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user