mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Complete sessions_test.go with more tests and add libsessions_test.go
This commit is contained in:
committed by
Dan Christian Bogos
parent
6d1837e53a
commit
884bc5b50e
116
sessions/libsessions_test.go
Normal file
116
sessions/libsessions_test.go
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestLibSessionSGetSetCGRID(t *testing.T) {
|
||||
sEv := engine.NewSafEvent(map[string]interface{}{
|
||||
utils.EVENT_NAME: "TEST_EVENT",
|
||||
utils.ToR: "*voice",
|
||||
utils.OriginID: "12345",
|
||||
utils.Account: "account1",
|
||||
utils.Subject: "subject1",
|
||||
utils.Destination: "+4986517174963",
|
||||
utils.Category: "call",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.RequestType: "*prepaid",
|
||||
utils.SetupTime: "2015-11-09 14:21:24",
|
||||
utils.AnswerTime: "2015-11-09 14:22:02",
|
||||
utils.Usage: "1m23s",
|
||||
utils.LastUsed: "21s",
|
||||
utils.PDD: "300ms",
|
||||
utils.SUPPLIER: "supplier1",
|
||||
utils.DISCONNECT_CAUSE: "NORMAL_DISCONNECT",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
})
|
||||
//Empty CGRID in event
|
||||
cgrID := GetSetCGRID(sEv)
|
||||
if len(cgrID) == 0 {
|
||||
t.Errorf("Unexpected cgrID: %+v", cgrID)
|
||||
}
|
||||
//populate CGRID in event
|
||||
sEv.Set(utils.CGRID, "someRandomVal")
|
||||
cgrID = GetSetCGRID(sEv)
|
||||
if cgrID != "someRandomVal" {
|
||||
t.Errorf("Expecting: someRandomVal, received: %+v", cgrID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibSessionSgetSessionTTL(t *testing.T) {
|
||||
sEv := engine.NewSafEvent(map[string]interface{}{
|
||||
utils.EVENT_NAME: "TEST_EVENT",
|
||||
utils.ToR: "*voice",
|
||||
utils.OriginID: "12345",
|
||||
utils.Account: "account1",
|
||||
utils.Subject: "subject1",
|
||||
utils.Destination: "+4986517174963",
|
||||
utils.Category: "call",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.RequestType: "*prepaid",
|
||||
utils.SetupTime: "2015-11-09 14:21:24",
|
||||
utils.AnswerTime: "2015-11-09 14:22:02",
|
||||
utils.Usage: "1m23s",
|
||||
utils.LastUsed: "21s",
|
||||
utils.PDD: "300ms",
|
||||
utils.SUPPLIER: "supplier1",
|
||||
utils.DISCONNECT_CAUSE: "NORMAL_DISCONNECT",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
utils.SessionTTL: "2s",
|
||||
})
|
||||
|
||||
//ttl is taken from event
|
||||
if ttl, err := getSessionTTL(sEv, time.Duration(0), nil); err != nil {
|
||||
t.Error(err)
|
||||
} else if ttl != time.Duration(2*time.Second) {
|
||||
t.Errorf("Expecting: %+v, received: %+v",
|
||||
time.Duration(2*time.Second), ttl)
|
||||
}
|
||||
//remove ttl from event
|
||||
sEv.Remove(utils.SessionTTL)
|
||||
if ttl, err := getSessionTTL(sEv, time.Duration(4*time.Second), nil); err != nil {
|
||||
t.Error(err)
|
||||
} else if ttl != time.Duration(4*time.Second) {
|
||||
t.Errorf("Expecting: %+v, received: %+v",
|
||||
time.Duration(4*time.Second), ttl)
|
||||
}
|
||||
|
||||
//add sessionTTLMaxDelay in event
|
||||
sEv.Set(utils.SessionTTLMaxDelay, "1s")
|
||||
if ttl, err := getSessionTTL(sEv, time.Duration(4*time.Second), nil); err != nil {
|
||||
t.Error(err)
|
||||
} else if ttl <= time.Duration(4*time.Second) {
|
||||
t.Errorf("Unexpected ttl : %+v", ttl)
|
||||
}
|
||||
|
||||
//remove sessionTTLMaxDelay from event
|
||||
sEv.Remove(utils.SessionTTLMaxDelay)
|
||||
if ttl, err := getSessionTTL(sEv, time.Duration(7*time.Second),
|
||||
utils.DurationPointer(time.Duration(2*time.Second))); err != nil {
|
||||
t.Error(err)
|
||||
} else if ttl <= time.Duration(7*time.Second) {
|
||||
t.Errorf("Unexpected ttl : %+v", ttl)
|
||||
}
|
||||
}
|
||||
@@ -811,13 +811,10 @@ func (sS *SessionS) getSessionIDsForPrefix(prefix string, pSessions bool) (cgrID
|
||||
ssIndx = sS.pSessionsIdx
|
||||
}
|
||||
idxMux.RLock()
|
||||
// map[OriginID:map[12372-1:map[*default:511654dc4da7ce4706276cb458437cdd81d0e2b3]]]
|
||||
for originID := range ssIndx[utils.OriginID] {
|
||||
if strings.HasPrefix(originID, prefix) {
|
||||
if _, hasDefaultRun := ssIndx[utils.OriginID][originID][utils.META_DEFAULT]; hasDefaultRun {
|
||||
cgrIDs = append(cgrIDs,
|
||||
ssIndx[utils.OriginID][originID].Slice()...)
|
||||
}
|
||||
cgrIDs = append(cgrIDs,
|
||||
ssIndx[utils.OriginID][originID].Slice()...)
|
||||
}
|
||||
}
|
||||
idxMux.RUnlock()
|
||||
|
||||
@@ -1042,3 +1042,176 @@ func TestSessionSV1ProcessEventReplyAsNavigableMap(t *testing.T) {
|
||||
t.Errorf("Expecting \n%+v\n, received: \n%+v", expected, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionStransitSState(t *testing.T) {
|
||||
sSCfg, _ := config.NewDefaultCGRConfig()
|
||||
sS := NewSessionS(sSCfg, nil, nil, nil, nil, nil, nil, nil, nil, nil, "UTC")
|
||||
sSEv := engine.NewSafEvent(map[string]interface{}{
|
||||
utils.EVENT_NAME: "TEST_EVENT",
|
||||
utils.ToR: "*voice",
|
||||
utils.OriginID: "111",
|
||||
utils.Direction: "*out",
|
||||
utils.Account: "account1",
|
||||
utils.Subject: "subject1",
|
||||
utils.Destination: "+4986517174963",
|
||||
utils.Category: "call",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.RequestType: "*prepaid",
|
||||
utils.SetupTime: "2015-11-09 14:21:24",
|
||||
utils.AnswerTime: "2015-11-09 14:22:02",
|
||||
utils.Usage: "1m23s",
|
||||
utils.LastUsed: "21s",
|
||||
utils.PDD: "300ms",
|
||||
utils.SUPPLIER: "supplier1",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
})
|
||||
s := &Session{
|
||||
CGRID: "session1",
|
||||
EventStart: sSEv,
|
||||
}
|
||||
//register the session as active
|
||||
sS.registerSession(s, false)
|
||||
//verify if was registered
|
||||
rcvS := sS.getSessions("session1", false)
|
||||
if !reflect.DeepEqual(rcvS[0], s) {
|
||||
t.Errorf("Expecting %+v, received: %+v", s, rcvS[0])
|
||||
}
|
||||
|
||||
//tranzit session from active to passive
|
||||
sS.transitSState("session1", true)
|
||||
//verify if the session was changed
|
||||
rcvS = sS.getSessions("session1", true)
|
||||
if !reflect.DeepEqual(rcvS[0], s) {
|
||||
t.Errorf("Expecting %+v, received: %+v", s, rcvS[0])
|
||||
}
|
||||
rcvS = sS.getSessions("session1", false)
|
||||
if len(rcvS) != 0 {
|
||||
t.Errorf("Expecting %+v, received: %+v", 0, len(rcvS))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionSgetSessionIDsForPrefix(t *testing.T) {
|
||||
sSCfg, _ := config.NewDefaultCGRConfig()
|
||||
sS := NewSessionS(sSCfg, nil, nil, nil, nil, nil, nil, nil, nil, nil, "UTC")
|
||||
sSEv := engine.NewSafEvent(map[string]interface{}{
|
||||
utils.EVENT_NAME: "TEST_EVENT",
|
||||
utils.ToR: "*voice",
|
||||
utils.OriginID: "111",
|
||||
utils.Direction: "*out",
|
||||
utils.Account: "account1",
|
||||
utils.Subject: "subject1",
|
||||
utils.Destination: "+4986517174963",
|
||||
utils.Category: "call",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.RequestType: "*prepaid",
|
||||
utils.SetupTime: "2015-11-09 14:21:24",
|
||||
utils.AnswerTime: "2015-11-09 14:22:02",
|
||||
utils.Usage: "1m23s",
|
||||
utils.LastUsed: "21s",
|
||||
utils.PDD: "300ms",
|
||||
utils.SUPPLIER: "supplier1",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
})
|
||||
s := &Session{
|
||||
CGRID: "session1",
|
||||
EventStart: sSEv,
|
||||
}
|
||||
//register the session as active
|
||||
sS.registerSession(s, false)
|
||||
//verify if was registered
|
||||
rcvS := sS.getSessions("session1", false)
|
||||
if !reflect.DeepEqual(rcvS[0], s) {
|
||||
t.Errorf("Expecting %+v, received: %+v", s, rcvS[0])
|
||||
}
|
||||
|
||||
rcv := sS.getSessionIDsForPrefix("1", false)
|
||||
exp := []string{"session1"}
|
||||
if !reflect.DeepEqual(rcv, exp) {
|
||||
t.Errorf("Expecting %+v, received: %+v", exp, rcv)
|
||||
}
|
||||
sSEv2 := engine.NewSafEvent(map[string]interface{}{
|
||||
utils.EVENT_NAME: "TEST_EVENT",
|
||||
utils.ToR: "*voice",
|
||||
utils.OriginID: "121",
|
||||
utils.Direction: "*out",
|
||||
utils.Account: "account1",
|
||||
utils.Subject: "subject1",
|
||||
utils.Destination: "+4986517174963",
|
||||
utils.Category: "call",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.RequestType: "*prepaid",
|
||||
utils.SetupTime: "2015-11-09 14:21:24",
|
||||
utils.AnswerTime: "2015-11-09 14:22:02",
|
||||
utils.Usage: "1m23s",
|
||||
utils.LastUsed: "21s",
|
||||
utils.PDD: "300ms",
|
||||
utils.SUPPLIER: "supplier1",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
})
|
||||
s2 := &Session{
|
||||
CGRID: "session2",
|
||||
EventStart: sSEv2,
|
||||
}
|
||||
//register the session as active
|
||||
sS.registerSession(s2, false)
|
||||
|
||||
//check for reverse
|
||||
rcv = sS.getSessionIDsForPrefix("1", false)
|
||||
exp = []string{"session1", "session2"}
|
||||
exp2 := []string{"session2", "session1"}
|
||||
if !reflect.DeepEqual(rcv, exp) && !reflect.DeepEqual(rcv, exp2) {
|
||||
t.Errorf("Expecting %+v, received: %+v", exp, rcv)
|
||||
}
|
||||
|
||||
rcv = sS.getSessionIDsForPrefix("12", false)
|
||||
exp = []string{"session2"}
|
||||
if !reflect.DeepEqual(rcv, exp) {
|
||||
t.Errorf("Expecting %+v, received: %+v", exp, rcv)
|
||||
}
|
||||
|
||||
sS.unregisterSession("session2", false)
|
||||
rcv = sS.getSessionIDsForPrefix("12", false)
|
||||
if rcv != nil {
|
||||
t.Errorf("Expecting nil, received: %+v", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionSregisterSessionWithTerminator(t *testing.T) {
|
||||
sSCfg, _ := config.NewDefaultCGRConfig()
|
||||
sS := NewSessionS(sSCfg, nil, nil, nil, nil, nil, nil, nil, nil, nil, "UTC")
|
||||
sSEv := engine.NewSafEvent(map[string]interface{}{
|
||||
utils.EVENT_NAME: "TEST_EVENT",
|
||||
utils.ToR: "*voice",
|
||||
utils.OriginID: "111",
|
||||
utils.Direction: "*out",
|
||||
utils.Account: "account1",
|
||||
utils.Subject: "subject1",
|
||||
utils.Destination: "+4986517174963",
|
||||
utils.Category: "call",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.RequestType: "*prepaid",
|
||||
utils.SetupTime: "2015-11-09 14:21:24",
|
||||
utils.AnswerTime: "2015-11-09 14:22:02",
|
||||
utils.Usage: "1m23s",
|
||||
utils.LastUsed: "21s",
|
||||
utils.PDD: "300ms",
|
||||
utils.SUPPLIER: "supplier1",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
utils.SessionTTL: "2s", //used in setSTerminator
|
||||
})
|
||||
s := &Session{
|
||||
CGRID: "session1",
|
||||
EventStart: sSEv,
|
||||
}
|
||||
//register the session as active with terminator
|
||||
sS.registerSession(s, false)
|
||||
|
||||
rcvS := sS.getSessions("session1", false)
|
||||
if !reflect.DeepEqual(rcvS[0], s) {
|
||||
t.Errorf("Expecting %+v, received: %+v", s, rcvS[0])
|
||||
} else if rcvS[0].sTerminator.ttl != time.Duration(2*time.Second) {
|
||||
t.Errorf("Expecting %+v, received: %+v",
|
||||
time.Duration(2*time.Second), rcvS[0].sTerminator.ttl)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user