diff --git a/engine/mapevent.go b/engine/mapevent.go index 053f6044c..6f0593e5b 100644 --- a/engine/mapevent.go +++ b/engine/mapevent.go @@ -155,7 +155,7 @@ func (me MapEvent) GetTimeIgnoreErrors(fldName string, tmz string) (t time.Time) // Clone returns the cloned map func (me MapEvent) Clone() (mp MapEvent) { if me == nil { - return nil + return } mp = make(MapEvent, len(me)) for k, v := range me { diff --git a/sessions/libsessions_test.go b/sessions/libsessions_test.go index 15e0797eb..fb539c270 100644 --- a/sessions/libsessions_test.go +++ b/sessions/libsessions_test.go @@ -81,8 +81,7 @@ func TestLibSessionSgetSessionTTL(t *testing.T) { }) sEv[utils.SessionTTL] = "notanumber" - _, err := getSessionTTL(&sEv, time.Duration(0), nil) - if err == nil { + if _, err := getSessionTTL(&sEv, time.Duration(0), nil); err == nil { t.Errorf("Expecting: NOT_FOUND, received: %+v", err) } sEv[utils.SessionTTL] = 0 diff --git a/sessions/session.go b/sessions/session.go index 892a48826..f58a8a9c5 100644 --- a/sessions/session.go +++ b/sessions/session.go @@ -247,7 +247,6 @@ func (sr *SRun) Clone() (clsr *SRun) { ExtraDuration: sr.ExtraDuration, LastUsage: sr.LastUsage, TotalUsage: sr.TotalUsage, - NextAutoDebit: sr.NextAutoDebit, } if sr.CD != nil { clsr.CD = sr.CD.Clone() @@ -255,6 +254,9 @@ func (sr *SRun) Clone() (clsr *SRun) { if sr.EventCost != nil { clsr.EventCost = sr.EventCost.Clone() } + if sr.NextAutoDebit != nil { + clsr.NextAutoDebit = utils.TimePointer(*sr.NextAutoDebit) + } return } diff --git a/sessions/session_test.go b/sessions/session_test.go index 7e7603c8e..77fda32ee 100644 --- a/sessions/session_test.go +++ b/sessions/session_test.go @@ -72,6 +72,8 @@ func TestSessionClone(t *testing.T) { t.Errorf("Expecting: %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) } //normal check + tTime := time.Now() + tTime2 := time.Date(2020, time.April, 18, 23, 0, 0, 0, time.UTC) session = &Session{ CGRID: "CGRID", Tenant: "cgrates.org", @@ -79,6 +81,24 @@ func TestSessionClone(t *testing.T) { ClientConnID: "ClientConnID", EventStart: engine.NewMapEvent(nil), DebitInterval: time.Duration(18), + SRuns: []*SRun{ + {Event: engine.NewMapEvent(nil), + CD: &engine.CallDescriptor{Category: "test"}, + EventCost: &engine.EventCost{CGRID: "testCGRID"}, + ExtraDuration: time.Duration(1), + LastUsage: time.Duration(2), + TotalUsage: time.Duration(3), + NextAutoDebit: &tTime, + }, + {Event: engine.NewMapEvent(nil), + CD: &engine.CallDescriptor{Category: "test2"}, + EventCost: &engine.EventCost{CGRID: "testCGRID2"}, + ExtraDuration: time.Duration(4), + LastUsage: time.Duration(5), + TotalUsage: time.Duration(6), + NextAutoDebit: &tTime2, + }, + }, } eOut = &Session{ CGRID: "CGRID", @@ -87,11 +107,45 @@ func TestSessionClone(t *testing.T) { ClientConnID: "ClientConnID", EventStart: engine.NewMapEvent(nil), DebitInterval: time.Duration(18), + SRuns: []*SRun{ + {Event: engine.NewMapEvent(nil), + CD: &engine.CallDescriptor{Category: "test"}, + EventCost: &engine.EventCost{CGRID: "testCGRID"}, + ExtraDuration: time.Duration(1), + LastUsage: time.Duration(2), + TotalUsage: time.Duration(3), + NextAutoDebit: &tTime, + }, + {Event: engine.NewMapEvent(nil), + CD: &engine.CallDescriptor{Category: "test2"}, + EventCost: &engine.EventCost{CGRID: "testCGRID2"}, + ExtraDuration: time.Duration(4), + LastUsage: time.Duration(5), + TotalUsage: time.Duration(6), + NextAutoDebit: &tTime2, + }, + }, } rcv = session.Clone() if !reflect.DeepEqual(eOut, rcv) && session.CGRID == "testID" { t.Errorf("Expecting: %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) } + //check clone + rcv.CGRID = "newCGRID" + + if session.CGRID == "newCGRID" { + t.Errorf("Expecting: CGRID, received: newCGRID") + } + rcv.SRuns[1].TotalUsage = time.Duration(10) + if session.SRuns[1].TotalUsage == time.Duration(10) { + t.Errorf("Expecting: %s, received: %s", time.Duration(3), time.Duration(10)) + } + tTimeNow := time.Now() + *rcv.SRuns[1].NextAutoDebit = tTimeNow + if *session.SRuns[1].NextAutoDebit == tTimeNow { + t.Errorf("Expecting: %s, received: %s", time.Date(2020, time.April, 18, 23, 0, 0, 0, time.UTC), tTimeNow) + + } } @@ -369,6 +423,7 @@ func TestSessionAsExternalSessions(t *testing.T) { utils.Usage: time.Duration(2 * time.Second), utils.Cost: 12.13, } + tTime := time.Date(2020, time.April, 18, 23, 0, 0, 0, time.UTC) s := &Session{ CGRID: "RandomCGRID", Tenant: "cgrates.org", @@ -376,8 +431,9 @@ func TestSessionAsExternalSessions(t *testing.T) { DebitInterval: time.Second, SRuns: []*SRun{ &SRun{ - Event: engine.NewMapEvent(ev), - TotalUsage: time.Duration(2 * time.Second), + Event: engine.NewMapEvent(ev), + TotalUsage: time.Duration(2 * time.Second), + NextAutoDebit: &tTime, }, }, } @@ -403,6 +459,7 @@ func TestSessionAsExternalSessions(t *testing.T) { }, NodeID: "ALL", DebitInterval: time.Second, + NextAutoDebit: tTime, // aSs[i].LoopIndex: sr.CD.LoopIndex, // aSs[i].DurationIndex: sr.CD.DurationIndex, // aSs[i].MaxRate: sr.CD.MaxRate, @@ -591,3 +648,76 @@ func TestSessionAsExternalSessions3(t *testing.T) { } } + +func TestSessiontotalUsage(t *testing.T) { + //empty check + session := new(Session) + rcv := session.totalUsage() + eOut := time.Duration(0) + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + //normal check + tTime := time.Now() + tTime2 := time.Date(2020, time.April, 18, 23, 0, 0, 0, time.UTC) + session = &Session{ + CGRID: "CGRID", + Tenant: "cgrates.org", + ResourceID: "resourceID", + ClientConnID: "ClientConnID", + EventStart: engine.NewMapEvent(nil), + DebitInterval: time.Duration(18), + SRuns: []*SRun{ + {Event: engine.NewMapEvent(nil), + CD: &engine.CallDescriptor{Category: "test"}, + EventCost: &engine.EventCost{CGRID: "testCGRID"}, + ExtraDuration: time.Duration(1), + LastUsage: time.Duration(2), + TotalUsage: time.Duration(5), + NextAutoDebit: &tTime, + }, + {Event: engine.NewMapEvent(nil), + CD: &engine.CallDescriptor{Category: "test2"}, + EventCost: &engine.EventCost{CGRID: "testCGRID2"}, + ExtraDuration: time.Duration(4), + LastUsage: time.Duration(5), + TotalUsage: time.Duration(6), + NextAutoDebit: &tTime2, + }, + }, + } + eOut = time.Duration(5) + rcv = session.totalUsage() + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } +} + +func TestSessionstopSTerminator(t *testing.T) { + //empty check + session := new(Session) + rcv := session.totalUsage() + eOut := time.Duration(0) + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + //normal check + session = &Session{ + sTerminator: &sTerminator{endChan: make(chan struct{})}, + } + session.stopSTerminator() + if session.sTerminator.endChan != nil { + t.Errorf("Expecting: nil, received: %s", utils.ToJSON(session.sTerminator.endChan)) + } +} + +func TestSessionstopDebitLoops(t *testing.T) { + session := &Session{ + debitStop: make(chan struct{}), + } + session.stopDebitLoops() + if session.debitStop != nil { + t.Errorf("Expecting: nil, received: %s", utils.ToJSON(session.debitStop)) + } + +}