added action type functions and a simple test

This commit is contained in:
Radu Ioan Fericean
2012-06-27 14:43:40 +03:00
parent 039a3b40c3
commit 5e1ea9211a
4 changed files with 43 additions and 7 deletions

View File

@@ -63,7 +63,7 @@ func (s scheduler) loop() {
a0 := s.queue[0]
now := time.Now()
if a0.GetNextStartTime().Equal(now) || a0.GetNextStartTime().Before(now) {
a0.Execute()
go a0.Execute()
s.queue = append(s.queue, a0)
s.queue = s.queue[1:]
sort.Sort(s.queue)

View File

@@ -74,6 +74,19 @@ type Action struct {
MinuteBucket *MinuteBucket
}
type actionTypeFunc func(a *Action) error
var (
actionTypeFuncMap = map[string]actionTypeFunc{
"LOG": logAction,
}
)
func logAction(a *Action) (err error) {
log.Printf("%v %v %v", a.BalanceId, a.Units, a.MinuteBucket)
return
}
// Structure to store actions according to weight
type actionsorter []*Action
@@ -202,12 +215,18 @@ MONTHS:
return
}
func (at *ActionTiming) Execute() {
func (at *ActionTiming) Execute() (err error) {
aac, err := at.getActions()
if err != nil {
return
}
for _, a := range aac {
log.Print(a)
actionFunction, exists := actionTypeFuncMap[a.ActionType]
if !exists {
log.Printf("Function type %v not available, aborting execution!", a.ActionType)
return
}
err = actionFunction(a)
}
return
}

View File

@@ -161,3 +161,19 @@ func TestActionTimingHourMonthdaysMonths(t *testing.T) {
t.Errorf("Expected %v was %v", expected, st)
}
}
func TestLogFunction(t *testing.T) {
a := &Action{
ActionType: "LOG",
BalanceId: "test",
Units: 1.1,
MinuteBucket: &MinuteBucket{},
}
at := &ActionTiming{
actions: []*Action{a},
}
err := at.Execute()
if err != nil {
t.Errorf("Could not execute LOG action: %v", err)
}
}

View File

@@ -30,10 +30,11 @@ const (
INBOUND = "IN"
OUTBOUND = "OUT"
// Balance types
CREDIT = "MONETARY"
SMS = "SMS"
TRAFFIC = "INTERNET"
MINUTES = "MINUTES"
CREDIT = "MONETARY"
SMS = "SMS"
TRAFFIC = "INTERNET"
TRAFFIC_TIME = "INTERNET_TIME"
MINUTES = "MINUTES"
// Price types
PERCENT = "PERCENT"
ABSOLUTE = "ABSOLUTE"