From 884bc5b50efb0be7e89cd2d4252940fe0af9d28b Mon Sep 17 00:00:00 2001 From: TeoV Date: Tue, 29 Jan 2019 08:52:30 -0500 Subject: [PATCH] Complete sessions_test.go with more tests and add libsessions_test.go --- sessions/libsessions_test.go | 116 +++++++++++++++++++++++ sessions/sessions.go | 7 +- sessions/sessions_test.go | 173 +++++++++++++++++++++++++++++++++++ 3 files changed, 291 insertions(+), 5 deletions(-) create mode 100644 sessions/libsessions_test.go diff --git a/sessions/libsessions_test.go b/sessions/libsessions_test.go new file mode 100644 index 000000000..a8aa6e60f --- /dev/null +++ b/sessions/libsessions_test.go @@ -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 +*/ + +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) + } +} diff --git a/sessions/sessions.go b/sessions/sessions.go index 07a8dfdb4..4f4e54a1d 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -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() diff --git a/sessions/sessions_test.go b/sessions/sessions_test.go index b0ed41dd1..d7eb6f29c 100644 --- a/sessions/sessions_test.go +++ b/sessions/sessions_test.go @@ -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) + } + +}