tests working using minute buckets

This commit is contained in:
Radu Ioan Fericean
2012-02-22 21:31:11 +02:00
parent 1f6050d471
commit f8b080be30
6 changed files with 28 additions and 29 deletions

View File

@@ -2,7 +2,7 @@ package timespans
import (
"fmt"
"log"
//"log"
"math"
"time"
)
@@ -37,7 +37,7 @@ type CallDescriptor struct {
CstmId, Subject, DestinationPrefix string
TimeStart, TimeEnd time.Time
ActivationPeriods []*ActivationPeriod
storageGetter StorageGetter
storageGetter StorageGetter
}
/*
@@ -93,17 +93,18 @@ Splits the received timespan into sub time spans according to the activation per
func (cd *CallDescriptor) splitTimeSpan(firstSpan *TimeSpan) (timespans []*TimeSpan) {
timespans = append(timespans, firstSpan)
// split on (free) minute buckets
if userBudget, err := cd.storageGetter.GetUserBudget(cd.Subject); err == nil {
if userBudget, err := cd.storageGetter.GetUserBudget(cd.Subject); err == nil && userBudget != nil {
_, bucketList := userBudget.getSecondsForPrefix(cd.storageGetter, cd.DestinationPrefix)
for _, mb := range bucketList {
for i := 0; i < len(timespans); i++ {
if timespans[i].MinuteInfo == nil {
newTs := timespans[i].SplitByMinuteBucket(mb)
if newTs != nil {
timespans = append(timespans, newTs)
firstSpan = newTs // we move the firstspan to the newly created one for further spliting
break
}
if timespans[i].MinuteInfo != nil {
continue
}
newTs := timespans[i].SplitByMinuteBucket(mb)
if newTs != nil {
timespans = append(timespans, newTs)
firstSpan = newTs // we move the firstspan to the newly created one for further spliting
break
}
}
}
@@ -112,7 +113,6 @@ func (cd *CallDescriptor) splitTimeSpan(firstSpan *TimeSpan) (timespans []*TimeS
return // all the timespans are on minutes
}
if len(cd.ActivationPeriods) == 0 {
log.Print("Nothing to split, move along... ", cd)
return
}
@@ -132,7 +132,6 @@ func (cd *CallDescriptor) splitTimeSpan(firstSpan *TimeSpan) (timespans []*TimeS
newTs := timespans[i].SplitByActivationPeriod(ap)
if newTs != nil {
timespans = append(timespans, newTs)
log.Print("NewTS: ", newTs)
} else {
afterEnd = true
break

View File

@@ -17,7 +17,6 @@ func TestKyotoSplitSpans(t *testing.T) {
cd.RestoreFromStorage()
timespans := cd.splitInTimeSpans()
if len(timespans) != 2 {
t.Error("Wrong number of timespans: ", len(timespans))
}
}
@@ -32,6 +31,7 @@ func TestRedisSplitSpans(t *testing.T) {
cd.RestoreFromStorage()
timespans := cd.splitInTimeSpans()
if len(timespans) != 2 {
t.Error("Wrong number of timespans: ", len(timespans))
}
@@ -174,9 +174,9 @@ func TestMinutesCost(t *testing.T) {
t1 := time.Date(2012, time.February, 8, 22, 50, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 8, 23, 50, 21, 0, time.UTC)
cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0723", TimeStart: t1, TimeEnd: t2, storageGetter: getter}
cd := &CallDescriptor{CstmId: "vdf", Subject: "minutosu", DestinationPrefix: "0723", TimeStart: t1, TimeEnd: t2, storageGetter: getter}
result, _ := cd.GetCost()
expected := &CallCost{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0723", Cost: 0, ConnectFee: 0}
expected := &CallCost{CstmId: "vdf", Subject: "minutosu", DestinationPrefix: "0723", Cost: 0, ConnectFee: 0}
if result.Cost != expected.Cost || result.ConnectFee != expected.ConnectFee {
t.Errorf("Expected %v was %v", expected, result)
}

Binary file not shown.

View File

@@ -1,4 +1,4 @@
[
{"Id":"rif","Credit":21,"SmsCredit":0,"ResetDayOfTheMonth":10,"TariffPlanId":"","MinuteBuckets": [{"Seconds":10,"Priority":10,"Price":0.01,"DestinationId":"nationale"},
{"Id":"minutosu","Credit":21,"SmsCredit":0,"ResetDayOfTheMonth":10,"TariffPlanId":"","MinuteBuckets": [{"Seconds":10,"Priority":10,"Price":0.01,"DestinationId":"nationale"},
{"Seconds":100,"Priority":20,"Price":0,"DestinationId":"retea"}]}
]

View File

@@ -1,8 +1,8 @@
package timespans
import (
"time"
"fmt"
"time"
//"log"
)
@@ -13,12 +13,12 @@ type TimeSpan struct {
TimeStart, TimeEnd time.Time
ActivationPeriod *ActivationPeriod
Interval *Interval
MinuteInfo *MinuteInfo
MinuteInfo *MinuteInfo
}
type MinuteInfo struct {
DestinationId string
Quantity float64
Quantity float64
}
/*
@@ -128,7 +128,7 @@ func (ts *TimeSpan) SplitByMinuteBucket(mb *MinuteBucket) (newTs *TimeSpan) {
mb.Seconds -= s
return nil
}
secDuration,_ := time.ParseDuration(fmt.Sprintf("%vs", mb.Seconds))
secDuration, _ := time.ParseDuration(fmt.Sprintf("%vs", mb.Seconds))
newTimeEnd := ts.TimeStart.Add(secDuration)
newTs = &TimeSpan{TimeStart: newTimeEnd, TimeEnd: ts.TimeEnd}

View File

@@ -40,9 +40,9 @@ func TestUserBudgetStoreRestore(t *testing.T) {
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
rifsBudget := &UserBudget{Id: "rif", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
s := rifsBudget.store()
ub1 := &UserBudget{Id: "rif"}
ub1 := &UserBudget{Id: "other"}
ub1.restore(s)
if ub1.store() != s {
t.Errorf("Expected %q was %q", s, ub1.store())
@@ -55,7 +55,7 @@ func TestUserBudgetKyotoStore(t *testing.T) {
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
rifsBudget := &UserBudget{Id: "rif", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
getter.SetUserBudget(rifsBudget)
result, _ := getter.GetUserBudget(rifsBudget.Id)
if result.SmsCredit != rifsBudget.SmsCredit || len(result.MinuteBuckets) != len(rifsBudget.MinuteBuckets) {
@@ -69,7 +69,7 @@ func TestUserBudgetRedisStore(t *testing.T) {
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
rifsBudget := &UserBudget{Id: "rif", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
getter.SetUserBudget(rifsBudget)
result, _ := getter.GetUserBudget(rifsBudget.Id)
if result.SmsCredit != rifsBudget.SmsCredit || len(result.MinuteBuckets) != len(rifsBudget.MinuteBuckets) {
@@ -83,7 +83,7 @@ func TestUserBudgetMongoStore(t *testing.T) {
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
rifsBudget := &UserBudget{Id: "rif", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
getter.SetUserBudget(rifsBudget)
result, _ := getter.GetUserBudget(rifsBudget.Id)
if result.SmsCredit != rifsBudget.SmsCredit || len(result.MinuteBuckets) != len(rifsBudget.MinuteBuckets) {
@@ -99,7 +99,7 @@ func BenchmarkGetSecondForPrefix(b *testing.B) {
b2 := &MinuteBucket{Seconds: 100, Price: 1, Priority: 20, destination: retea}
tf1 := &TariffPlan{MinuteBuckets: []*MinuteBucket{b1, b2}}
ub1 := &UserBudget{Id: "rif", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: tf1, ResetDayOfTheMonth: 10}
ub1 := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: tf1, ResetDayOfTheMonth: 10}
b.StartTimer()
for i := 0; i < b.N; i++ {
ub1.getSecondsForPrefix(nil, "0723")
@@ -112,7 +112,7 @@ func BenchmarkUserBudgetKyotoStoreRestore(b *testing.B) {
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
rifsBudget := &UserBudget{Id: "rif", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
for i := 0; i < b.N; i++ {
getter.SetUserBudget(rifsBudget)
getter.GetUserBudget(rifsBudget.Id)
@@ -125,7 +125,7 @@ func BenchmarkUserBudgetRedisStoreRestore(b *testing.B) {
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
rifsBudget := &UserBudget{Id: "rif", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
for i := 0; i < b.N; i++ {
getter.SetUserBudget(rifsBudget)
getter.GetUserBudget(rifsBudget.Id)
@@ -138,7 +138,7 @@ func BenchmarkUserBudgetMongoStoreRestore(b *testing.B) {
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
rifsBudget := &UserBudget{Id: "rif", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
rifsBudget := &UserBudget{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, Credit: 21, tariffPlan: seara, ResetDayOfTheMonth: 10}
for i := 0; i < b.N; i++ {
getter.SetUserBudget(rifsBudget)
getter.GetUserBudget(rifsBudget.Id)