created debit loop

This commit is contained in:
Radu Ioan Fericean
2012-05-05 12:21:53 +03:00
parent 5aef01c4c8
commit 41c25b5055
2 changed files with 22 additions and 5 deletions

View File

@@ -25,27 +25,42 @@ import (
"time"
)
const (
DEBIT_PERIOD = 10 * time.Second
)
type Session struct {
uuid, cstmId, subject, destination string
startTime time.Time // destination: startTime
stopDebit chan byte
}
func NewSession(ev *Event) *Session {
func NewSession(ev *Event) (s *Session) {
startTime, err := time.Parse(time.RFC1123, ev.Fields[START_TIME])
if err != nil {
log.Print("Error parsing answer event start time, using time.Now!")
startTime = time.Now()
}
return &Session{uuid: ev.Fields[UUID],
s = &Session{uuid: ev.Fields[UUID],
cstmId: ev.Fields[CSTMID],
subject: ev.Fields[SUBJECT],
destination: ev.Fields[DESTINATION],
startTime: startTime}
startTime: startTime,
stopDebit: make(chan byte)}
go s.StartDebitLoop()
return
}
func (s *Session) StartDebitLoop() {
for {
select {
case <-s.stopDebit:
log.Print("Put back!")
return
default:
}
log.Print("Debit")
time.Sleep(DEBIT_PERIOD)
}
}
@@ -77,5 +92,5 @@ func (s *Session) GetSessionCost() (callCosts *timespans.CallCost, err error) {
}
func (s *Session) Close() {
s.stopDebit <- 1
}

View File

@@ -53,6 +53,7 @@ var (
func TestSessionDurationSingle(t *testing.T) {
s := NewSession(newEvent)
defer s.Close()
twoSeconds, _ := time.ParseDuration("2s")
if d := s.GetSessionDurationFrom(s.startTime.Add(twoSeconds)); d.Seconds() < 2 || d.Seconds() > 3 {
t.Errorf("Wrong session duration %v", d)
@@ -61,6 +62,7 @@ func TestSessionDurationSingle(t *testing.T) {
func TestSessionCostSingle(t *testing.T) {
s := NewSession(newEvent)
defer s.Close()
twoSeconds, _ := time.ParseDuration("60s")
if cc, err := s.GetSessionCostFrom(s.startTime.Add(twoSeconds)); err != nil {
t.Errorf("Get cost returned error %v", err)