GetMaxSessionDuration fix

should return no more than initial call descriptor duration
This commit is contained in:
Radu Ioan Fericean
2014-02-07 19:54:09 +02:00
parent dadf06f3ef
commit 77ea2754cb
4 changed files with 75 additions and 11 deletions

View File

@@ -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)
}

View File

@@ -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()

View File

@@ -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
}

View File

@@ -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)
}
}