From bfe42356b037e37a1fa7de2faaab90cbb37f5a79 Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 26 May 2017 18:22:42 +0200 Subject: [PATCH] RemoveStaleReferences only at end of Trim, better testing of EC.Trim --- engine/eventcost.go | 8 ++---- engine/eventcost_test.go | 61 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/engine/eventcost.go b/engine/eventcost.go index 3fbab3567..42b70d6b8 100644 --- a/engine/eventcost.go +++ b/engine/eventcost.go @@ -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 } diff --git a/engine/eventcost_test.go b/engine/eventcost_test.go index aaca55654..ac6ad4a1e 100644 --- a/engine/eventcost_test.go +++ b/engine/eventcost_test.go @@ -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)) + } +}