From 7b554b0c6d0345518084d1ba37d626f53b71faf0 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 4 May 2015 11:24:02 +0300 Subject: [PATCH] fix for zero duration calldescriptors --- engine/calldesc.go | 23 +++++++++++------------ engine/calldesc_test.go | 11 +++++++++++ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/engine/calldesc.go b/engine/calldesc.go index 8809da0b5..38d9e97c1 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -405,6 +405,10 @@ func (cd *CallDescriptor) GetDuration() time.Duration { Creates a CallCost structure with the cost information calculated for the received CallDescriptor. */ func (cd *CallDescriptor) GetCost() (*CallCost, error) { + // check for 0 duration + if cd.TimeEnd.Sub(cd.TimeStart) == 0 { + return cd.CreateCallCost(), nil + } if cd.DurationIndex < cd.TimeEnd.Sub(cd.TimeStart) { cd.DurationIndex = cd.TimeEnd.Sub(cd.TimeStart) } @@ -429,18 +433,10 @@ func (cd *CallDescriptor) GetCost() (*CallCost, error) { cost += ts.getCost() } //startIndex := len(fmt.Sprintf("%s:%s:%s:", cd.Direction, cd.Tenant, cd.Category)) - cc := &CallCost{ - Direction: cd.Direction, - Category: cd.Category, - Tenant: cd.Tenant, - Account: cd.Account, - Destination: cd.Destination, - Subject: cd.Subject, - Cost: cost, - Timespans: timespans, - deductConnectFee: cd.LoopIndex == 0, - TOR: cd.TOR, - } + cc := cd.CreateCallCost() + cc.Cost = cost + cc.Timespans = timespans + // global rounding roundingDecimals, roundingMethod := cc.GetLongestRounding() cc.Cost = utils.Round(cc.Cost, roundingDecimals, roundingMethod) @@ -526,6 +522,9 @@ func (cd *CallDescriptor) GetMaxSessionDuration() (duration time.Duration, err e // Interface method used to add/substract an amount of cents or bonus seconds (as returned by GetCost method) // from user's money balance. func (cd *CallDescriptor) debit(account *Account, dryRun bool, goNegative bool) (cc *CallCost, err error) { + if cd.TimeEnd.Sub(cd.TimeStart) == 0 { + return cd.CreateCallCost(), nil + } if !dryRun { defer accountingStorage.SetAccount(account) } diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index c778d16ad..2c4f95ac1 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -229,6 +229,17 @@ func TestGetCost(t *testing.T) { } } +func TestGetCostZero(t *testing.T) { + t1 := time.Date(2012, time.February, 2, 17, 30, 0, 0, time.UTC) + t2 := time.Date(2012, time.February, 2, 17, 30, 0, 0, time.UTC) + cd := &CallDescriptor{Direction: "*out", Category: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2, LoopIndex: 0} + result, _ := cd.GetCost() + expected := &CallCost{Tenant: "vdf", Subject: "rif", Destination: "0256", Cost: 0} + if result.Cost != expected.Cost || result.GetConnectFee() != 0 { + t.Errorf("Expected %v was %v", expected, result) + } +} + func TestGetCostTimespans(t *testing.T) { t1 := time.Date(2013, time.October, 8, 9, 23, 2, 0, time.UTC) t2 := time.Date(2013, time.October, 8, 9, 24, 27, 0, time.UTC)