SMG.GetPassiveSessions command implementation

This commit is contained in:
DanB
2016-10-28 14:46:07 +02:00
parent 3f3ad29ab1
commit 2cb3faa033
2 changed files with 65 additions and 1 deletions

View File

@@ -722,6 +722,34 @@ func (smg *SMGeneric) setPassiveSession(s *SMGSession) {
smg.pSessionsMux.Unlock()
}
func (smg *SMGeneric) getPassiveSessions(originID, runID string) (pss map[string][]*SMGSession) {
smg.pSessionsMux.RLock()
if originID == "" {
if len(smg.passiveSessions) != 0 {
pss = smg.passiveSessions
}
} else {
pSSlc := smg.passiveSessions[originID]
if runID != "" {
var found bool
for _, s := range pSSlc {
if s.RunID == runID {
found = true
pSSlc = []*SMGSession{s}
}
}
if !found {
pSSlc = []*SMGSession{}
}
}
if len(pSSlc) != 0 {
pss = map[string][]*SMGSession{originID: pSSlc}
}
}
smg.pSessionsMux.RUnlock()
return
}
func (smg *SMGeneric) Timezone() string {
return smg.timezone
}
@@ -867,3 +895,20 @@ func (smg *SMGeneric) BiRPCV1SetPassiveSession(s SMGSession, reply *string) erro
*reply = utils.OK
return nil
}
type AttrGetPassiveSessions struct {
OriginID string
RunID string
}
func (smg *SMGeneric) BiRPCV1GetPassiveSessions(attrs AttrGetPassiveSessions, pSessions *map[string][]*SMGSession) error {
if attrs.RunID != "" && attrs.OriginID == "" {
return utils.ErrMandatoryIeMissing
}
pSS := smg.getPassiveSessions(attrs.OriginID, attrs.RunID)
if len(pSS) == 0 {
return utils.ErrNotFound
}
*pSessions = pSS
return nil
}

View File

@@ -239,7 +239,7 @@ func TestSMGActiveSessions(t *testing.T) {
}
}
func TestSetPassiveSession(t *testing.T) {
func TestGetSetPassiveSessions(t *testing.T) {
smg := NewSMGeneric(smgCfg, nil, nil, "UTC")
smGev := SMGenericEvent{
utils.EVENT_NAME: "TEST_EVENT",
@@ -269,10 +269,16 @@ func TestSetPassiveSession(t *testing.T) {
if len(smg.passiveSessions) != 0 {
t.Errorf("PassiveSessions: %+v", smg.passiveSessions)
}
if pSS := smg.getPassiveSessions("", ""); len(pSS) != 0 {
t.Errorf("PassiveSessions: %+v", pSS)
}
smg.setPassiveSession(smgSession)
if ss, hasIt := smg.passiveSessions[smGev.GetUUID()]; !hasIt || len(smg.passiveSessions) != 1 || len(ss) != 1 {
t.Errorf("PassiveSessions: %+v", smg.passiveSessions)
}
if pSS := smg.getPassiveSessions("", ""); len(pSS) != 1 {
t.Errorf("PassiveSessions: %+v", pSS)
}
// Update session
smGev = SMGenericEvent{
utils.EVENT_NAME: "TEST_EVENT",
@@ -341,4 +347,17 @@ func TestSetPassiveSession(t *testing.T) {
} else if ss[0].EventStart[utils.USAGE] != "2m33s" {
t.Errorf("SMGSession.EventStart: %+v", ss[0].EventStart[utils.USAGE])
}
// Test getPassiveSessions with filters
if pSS := smg.getPassiveSessions("", ""); len(pSS) != 2 {
t.Errorf("PassiveSessions: %+v", pSS)
}
if pSS := smg.getPassiveSessions("12345", ""); len(pSS) != 1 || len(pSS["12345"]) != 2 {
t.Errorf("PassiveSessions: %+v", pSS)
}
if pSS := smg.getPassiveSessions("12345", "second_test"); len(pSS) != 1 || len(pSS["12345"]) != 1 {
t.Errorf("PassiveSessions: %+v", pSS)
}
if pSS := smg.getPassiveSessions("aabbcc", ""); len(pSS) != 0 {
t.Errorf("PassiveSessions: %+v", pSS)
}
}