From 77ea2754cb6429d75df594f148f291c69a2d8317 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 7 Feb 2014 19:54:09 +0200 Subject: [PATCH] GetMaxSessionDuration fix should return no more than initial call descriptor duration --- engine/calldesc.go | 17 +++++++++----- engine/calldesc_test.go | 51 +++++++++++++++++++++++++++++++++++++---- utils/coreutils.go | 8 +++++++ utils/utils_test.go | 10 ++++++++ 4 files changed, 75 insertions(+), 11 deletions(-) diff --git a/engine/calldesc.go b/engine/calldesc.go index 755a0a61f..13f5dce1c 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -21,6 +21,7 @@ package engine import ( "errors" "fmt" + "log" "log/syslog" "time" //"encoding/json" @@ -420,7 +421,6 @@ func (cd *CallDescriptor) GetCost() (*CallCost, error) { /* Returns the approximate max allowed session for user balance. It will try the max amount received in the call descriptor -and will decrease it by 10% for nine times. So if the user has little credit it will still allow 10% of the initial amount. If the user has no credit then it will return 0. If the user has postpayed plan it returns -1. */ @@ -449,15 +449,15 @@ func (origCd *CallDescriptor) GetMaxSessionDuration() (time.Duration, error) { return 0, err } //Logger.Debug(fmt.Sprintf("availableDuration: %v, availableCredit: %v", availableDuration, availableCredit)) - // check for zero balance - if availableCredit == 0 { - return availableDuration, nil - } initialDuration := cd.TimeEnd.Sub(cd.TimeStart) if initialDuration <= availableDuration { // there are enough minutes for requested interval return initialDuration, nil } + // check for zero balance + if availableCredit == 0 { + return utils.MinDuration(initialDuration, availableDuration), nil + } //Logger.Debug(fmt.Sprintf("initial Duration: %v", initialDuration)) // we must move the timestart for the interval with the available duration because // that was already checked @@ -482,7 +482,11 @@ func (origCd *CallDescriptor) GetMaxSessionDuration() (time.Duration, error) { } } } - return availableDuration, nil + log.Print(initialDuration, availableDuration, initialDuration < availableDuration) + if initialDuration < availableDuration { + return initialDuration, nil + } + return utils.MinDuration(initialDuration, availableDuration), nil } // Interface method used to add/substract an amount of cents or bonus seconds (as returned by GetCost method) @@ -527,6 +531,7 @@ func (cd *CallDescriptor) MaxDebit() (cc *CallCost, err error) { if err != nil || remainingDuration == 0 { return new(CallCost), errors.New("no more credit") } + log.Print("REM_DUR: ", remainingDuration) if remainingDuration > 0 { // for postpaying client returns -1 cd.TimeEnd = cd.TimeStart.Add(remainingDuration) } diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index 1e6ceb7db..b285b26dd 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -296,7 +296,7 @@ func TestMaxSessionTimeWithUserBalance(t *testing.T) { Destination: "0723", Amount: 1000} result, err := cd.GetMaxSessionDuration() - expected := 300 * time.Second + expected := time.Minute if result != expected || err != nil { t.Errorf("Expected %v was %v", expected, result) } @@ -314,7 +314,7 @@ func TestMaxSessionTimeWithUserBalanceAccount(t *testing.T) { Destination: "0723", Amount: 1000} result, err := cd.GetMaxSessionDuration() - expected := 300 * time.Second + expected := time.Minute if result != expected || err != nil { t.Errorf("Expected %v was %v", expected, result) } @@ -331,8 +331,8 @@ func TestMaxSessionTimeNoCredit(t *testing.T) { Destination: "0723", Amount: 5400} result, err := cd.GetMaxSessionDuration() - if result != 100*time.Second || err != nil { - t.Errorf("Expected %v was %v", 100, result) + if result != time.Minute || err != nil { + t.Errorf("Expected %v was %v", time.Minute, result) } } @@ -343,7 +343,8 @@ func TestMaxSessionModifiesCallDesc(t *testing.T) { Direction: "*out", TOR: "0", Tenant: "vdf", - Subject: "broker", + Subject: "minu_from_tm", + Account: "minu", Destination: "0723", Amount: 5400} initial := cd.Clone() @@ -353,6 +354,46 @@ func TestMaxSessionModifiesCallDesc(t *testing.T) { } } +func TestMaxDebitDurationNoGreatherThanInitialDuration(t *testing.T) { + cd := &CallDescriptor{ + TimeStart: time.Date(2013, 10, 21, 18, 34, 0, 0, time.UTC), + TimeEnd: time.Date(2013, 10, 21, 18, 35, 0, 0, time.UTC), + Direction: "*out", + TOR: "0", + Tenant: "vdf", + Subject: "minu_from_tm", + Account: "minu", + Destination: "0723", + Amount: 1000} + initialDuration := cd.TimeEnd.Sub(cd.TimeStart) + result, _ := cd.GetMaxSessionDuration() + if result > initialDuration { + t.Error("max session duration greather than initial duration", initialDuration, result) + } +} + +func TestDebitAndMaxDebit(t *testing.T) { + cd1 := &CallDescriptor{ + TimeStart: time.Date(2013, 10, 21, 18, 34, 0, 0, time.UTC), + TimeEnd: time.Date(2013, 10, 21, 18, 35, 0, 0, time.UTC), + Direction: "*out", + TOR: "0", + Tenant: "vdf", + Subject: "minu_from_tm", + Account: "minu", + Destination: "0723", + Amount: 5400} + cd2 := cd1.Clone() + cc1, err1 := cd1.Debit() + cc2, err2 := cd2.MaxDebit() + if err1 != nil || err2 != nil { + t.Error("Error debiting and/or maxdebiting: ", err1, err2) + } + if !reflect.DeepEqual(cc1, cc2) { + t.Errorf("Debit and MaxDebit differ: %+v != %+v", cc1, cc2) + } +} + /*********************************** BENCHMARKS ***************************************/ func BenchmarkStorageGetting(b *testing.B) { b.StopTimer() diff --git a/utils/coreutils.go b/utils/coreutils.go index cf1fce232..08506ac1c 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -187,3 +187,11 @@ func ParseDurationWithSecs(durStr string) (time.Duration, error) { func BalanceKey(tenant, account, direction string) string { return fmt.Sprintf("%s:%s:%s", direction, tenant, account) } + +// returns the minimum duration between the two +func MinDuration(d1, d2 time.Duration) time.Duration { + if d1 < d2 { + return d1 + } + return d2 +} diff --git a/utils/utils_test.go b/utils/utils_test.go index 676824f70..36f53d1a7 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -336,3 +336,13 @@ func TestParseDurationWithSecs(t *testing.T) { t.Error("Parsed different than expected") } } + +func TestMinDuration(t *testing.T) { + d1, _ := time.ParseDuration("1m") + d2, _ := time.ParseDuration("59s") + minD1 := MinDuration(d1, d2) + minD2 := MinDuration(d2, d1) + if minD1 != d2 || minD2 != d2 { + t.Error("Error getting min duration: ", minD1, minD2) + } +}