From 1d0011b6dcd55ebdcc623e8f791c5a2108a0cdc7 Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 25 Nov 2016 14:23:32 +0100 Subject: [PATCH] SMGeneric fix compute mechanism in getSessionIDsForPrefix --- data/conf/samples/smg/cgrates.json | 2 +- engine/suretax.go | 4 - sessionmanager/session.go | 5 - sessionmanager/smg_it_test.go | 172 +++++++++++++++++++++++++++-- sessionmanager/smg_session.go | 1 - sessionmanager/smgeneric.go | 8 +- 6 files changed, 166 insertions(+), 26 deletions(-) diff --git a/data/conf/samples/smg/cgrates.json b/data/conf/samples/smg/cgrates.json index 7cd20ad35..211fc624c 100644 --- a/data/conf/samples/smg/cgrates.json +++ b/data/conf/samples/smg/cgrates.json @@ -5,7 +5,7 @@ // Starts rater, scheduler "general": { - "log_level": 8, + "log_level": 7, }, "listen": { diff --git a/engine/suretax.go b/engine/suretax.go index a6da68b6a..6294589f1 100644 --- a/engine/suretax.go +++ b/engine/suretax.go @@ -201,19 +201,15 @@ func SureTaxProcessCdr(cdr *CDR) error { defer resp.Body.Close() respBody, err := ioutil.ReadAll(resp.Body) if err != nil { - //utils.Logger.Debug(fmt.Sprintf("Unexpected response body received, error: %s\n", err.Error())) return err } if resp.StatusCode > 299 { - //utils.Logger.Debug(fmt.Sprintf("Unexpected code received: %d\n", resp.StatusCode)) return fmt.Errorf("Unexpected status code received: %d", resp.StatusCode) } - //utils.Logger.Debug(fmt.Sprintf("Received raw answer from SureTax: %s\n", string(respBody))) var respFull SureTaxResponse if err := json.Unmarshal(respBody, &respFull); err != nil { return err } - //utils.Logger.Debug(fmt.Sprintf("Received answer from SureTax: %+v\n", respFull)) var stResp STResponse if err := json.Unmarshal([]byte(respFull.D), &stResp); err != nil { return err diff --git a/sessionmanager/session.go b/sessionmanager/session.go index bf1257b03..79856c72f 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -128,7 +128,6 @@ func (s *Session) Close(ev engine.Event) error { if len(sr.CallCosts) == 0 { continue // why would we have 0 callcosts } - //utils.Logger.Debug(fmt.Sprintf("ALL CALLCOSTS: %s", utils.ToJSON(sr.CallCosts))) lastCC := sr.CallCosts[len(sr.CallCosts)-1] lastCC.Timespans.Decompress() // put credit back @@ -143,9 +142,7 @@ func (s *Session) Close(ev engine.Event) error { return err } hangupTime := startTime.Add(duration) - //utils.Logger.Debug(fmt.Sprintf("BEFORE REFUND: %s", utils.ToJSON(lastCC))) err = s.Refund(lastCC, hangupTime) - //utils.Logger.Debug(fmt.Sprintf("AFTER REFUND: %s", utils.ToJSON(lastCC))) if err != nil { return err } @@ -157,7 +154,6 @@ func (s *Session) Close(ev engine.Event) error { func (s *Session) Refund(lastCC *engine.CallCost, hangupTime time.Time) error { end := lastCC.Timespans[len(lastCC.Timespans)-1].TimeEnd refundDuration := end.Sub(hangupTime) - //utils.Logger.Debug(fmt.Sprintf("HANGUPTIME: %s REFUNDDURATION: %s", hangupTime.String(), refundDuration.String())) var refundIncrements engine.Increments for i := len(lastCC.Timespans) - 1; i >= 0; i-- { ts := lastCC.Timespans[i] @@ -214,7 +210,6 @@ func (s *Session) Refund(lastCC *engine.CallCost, hangupTime time.Time) error { return err } } - //utils.Logger.Debug(fmt.Sprintf("REFUND INCR: %s", utils.ToJSON(refundIncrements))) lastCC.Cost -= refundIncrements.GetTotalCost() lastCC.UpdateRatedUsage() lastCC.Timespans.Compress() diff --git a/sessionmanager/smg_it_test.go b/sessionmanager/smg_it_test.go index 51543d6b9..0ec04f4a2 100644 --- a/sessionmanager/smg_it_test.go +++ b/sessionmanager/smg_it_test.go @@ -635,6 +635,7 @@ func TestSMGVoiceSessionTTL(t *testing.T) { if err := smgRPC.Call("SMGenericV1.InitiateSession", smgEv, &maxUsage); err != nil { t.Error(err) } + time.Sleep(time.Duration(30 * time.Millisecond)) if maxUsage != 120 { t.Error("Bad max usage: ", maxUsage) } @@ -669,6 +670,7 @@ func TestSMGVoiceSessionTTL(t *testing.T) { if err := smgRPC.Call("SMGenericV1.UpdateSession", smgEv, &maxUsage); err != nil { t.Error(err) } + time.Sleep(time.Duration(10 * time.Millisecond)) if maxUsage != 120 { t.Error("Bad max usage: ", maxUsage) } @@ -747,15 +749,10 @@ func TestSMGVoiceSessionTTLWithRelocate(t *testing.T) { if err := smgRPC.Call("SMGenericV1.InitiateSession", smgEv, &maxUsage); err != nil { t.Error(err) } + time.Sleep(time.Duration(10) * time.Millisecond) if maxUsage != 120 { t.Error("Bad max usage: ", maxUsage) } - eAcntVal = 180.0 - if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { - t.Error(err) - } else if acnt.BalanceMap[utils.VOICE].GetTotalValue() != eAcntVal { - t.Errorf("Expecting: %f, received: %f", eAcntVal, acnt.BalanceMap[utils.VOICE].GetTotalValue()) - } var aSessions []*ActiveSession if err := smgRPC.Call("SMGenericV1.ActiveSessions", map[string]string{utils.MEDI_RUNID: utils.META_DEFAULT, utils.ACCID: smgEv.GetOriginID(utils.META_DEFAULT)}, &aSessions); err != nil { @@ -765,6 +762,12 @@ func TestSMGVoiceSessionTTLWithRelocate(t *testing.T) { } else if aSessions[0].Usage != time.Duration(120)*time.Second { t.Errorf("Expecting 2m, received usage: %v", aSessions[0].Usage) } + eAcntVal = 180.0 + if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { + t.Error(err) + } else if acnt.BalanceMap[utils.VOICE].GetTotalValue() != eAcntVal { + t.Errorf("Expecting: %f, received: %f", eAcntVal, acnt.BalanceMap[utils.VOICE].GetTotalValue()) + } smgEv = SMGenericEvent{ utils.EVENT_NAME: smgEv[utils.EVENT_NAME], utils.TOR: smgEv[utils.TOR], @@ -786,12 +789,7 @@ func TestSMGVoiceSessionTTLWithRelocate(t *testing.T) { if maxUsage != 120 { t.Error("Bad max usage: ", maxUsage) } - eAcntVal = 150.0 - if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { - t.Error(err) - } else if acnt.BalanceMap[utils.VOICE].GetTotalValue() != eAcntVal { - t.Errorf("Expecting: %f, received: %f", eAcntVal, acnt.BalanceMap[utils.VOICE].GetTotalValue()) - } + time.Sleep(time.Duration(20) * time.Millisecond) if err := smgRPC.Call("SMGenericV1.ActiveSessions", map[string]string{utils.MEDI_RUNID: utils.META_DEFAULT, utils.ACCID: smgEv.GetOriginID(utils.META_DEFAULT)}, &aSessions); err != nil { t.Error(err) @@ -800,6 +798,13 @@ func TestSMGVoiceSessionTTLWithRelocate(t *testing.T) { } else if aSessions[0].Usage != time.Duration(150)*time.Second { t.Errorf("Expecting 2m30s, received usage: %v", aSessions[0].Usage) } + eAcntVal = 150.0 + if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { + t.Error(err) + } else if acnt.BalanceMap[utils.VOICE].GetTotalValue() != eAcntVal { + t.Errorf("Expecting: %f, received: %f", eAcntVal, acnt.BalanceMap[utils.VOICE].GetTotalValue()) + } + time.Sleep(100 * time.Millisecond) eAcntVal = 149.95 if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { @@ -825,6 +830,149 @@ func TestSMGVoiceSessionTTLWithRelocate(t *testing.T) { } +func TestSMGVoiceRelocateWithOriginIDPrefix(t *testing.T) { + if !*testIntegration { + return + } + attrSetBalance := utils.AttrSetBalance{Tenant: "cgrates.org", Account: "TestRelocateWithOriginIDPrefix", + BalanceType: utils.VOICE, BalanceID: utils.StringPointer("TestRelocateWithOriginIDPrefix"), + Value: utils.Float64Pointer(300), RatingSubject: utils.StringPointer("*zero1s")} + var reply string + if err := smgRPC.Call("ApierV2.SetBalance", attrSetBalance, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Received: %s", reply) + } + var acnt *engine.Account + attrs := &utils.AttrGetAccount{Tenant: attrSetBalance.Tenant, Account: attrSetBalance.Account} + eAcntVal := 300.0 + if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { + t.Error(err) + } else if acnt.BalanceMap[utils.VOICE].GetTotalValue() != eAcntVal { + t.Errorf("Expecting: %f, received: %f", eAcntVal, acnt.BalanceMap[utils.VOICE].GetTotalValue()) + } + smgEv := SMGenericEvent{ + utils.EVENT_NAME: "TEST_EVENT_RELOCATE_ORIGPREFIX", + utils.TOR: utils.VOICE, + utils.ACCID: "12371", + utils.DIRECTION: utils.OUT, + utils.ACCOUNT: attrSetBalance.Account, + utils.SUBJECT: attrSetBalance.Account, + utils.DESTINATION: "12371", + utils.CATEGORY: "call", + utils.TENANT: "cgrates.org", + utils.REQTYPE: utils.META_PREPAID, + utils.SETUP_TIME: "2016-01-05 18:30:49", + utils.ANSWER_TIME: "2016-01-05 18:31:05", + utils.USAGE: "2m", + } + var maxUsage float64 + if err := smgRPC.Call("SMGenericV1.InitiateSession", smgEv, &maxUsage); err != nil { + t.Error(err) + } + time.Sleep(time.Duration(20) * time.Millisecond) + if maxUsage != 120 { + t.Error("Bad max usage: ", maxUsage) + } + var aSessions []*ActiveSession + if err := smgRPC.Call("SMGenericV1.ActiveSessions", map[string]string{utils.MEDI_RUNID: utils.META_DEFAULT, + utils.ACCID: smgEv.GetOriginID(utils.META_DEFAULT)}, &aSessions); err != nil { + t.Error(err) + } else if len(aSessions) != 1 { + t.Errorf("Unexpected number of sessions received: %+v", aSessions) + } else if aSessions[0].Usage != time.Duration(120)*time.Second { + t.Errorf("Expecting 2m, received usage: %v", aSessions[0].Usage) + } + eAcntVal = 180.0 + if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { + t.Error(err) + } else if acnt.BalanceMap[utils.VOICE].GetTotalValue() != eAcntVal { + t.Errorf("Expecting: %f, received: %f", eAcntVal, acnt.BalanceMap[utils.VOICE].GetTotalValue()) + } + smgEv = SMGenericEvent{ + utils.EVENT_NAME: smgEv[utils.EVENT_NAME], + utils.TOR: smgEv[utils.TOR], + utils.InitialOriginID: smgEv[utils.ACCID], + utils.ACCID: "12372-1", + utils.DIRECTION: smgEv[utils.DIRECTION], + utils.ACCOUNT: smgEv[utils.ACCOUNT], + utils.SUBJECT: smgEv[utils.SUBJECT], + utils.DESTINATION: smgEv[utils.DESTINATION], + utils.CATEGORY: smgEv[utils.CATEGORY], + utils.TENANT: smgEv[utils.TENANT], + utils.REQTYPE: smgEv[utils.REQTYPE], + utils.USAGE: "2m", + utils.LastUsed: "30s", + } + if err := smgRPC.Call("SMGenericV1.UpdateSession", smgEv, &maxUsage); err != nil { + t.Error(err) + } + if maxUsage != 120 { + t.Error("Bad max usage: ", maxUsage) + } + time.Sleep(time.Duration(20) * time.Millisecond) + if err := smgRPC.Call("SMGenericV1.ActiveSessions", map[string]string{utils.MEDI_RUNID: utils.META_DEFAULT, + utils.ACCID: "12372-1"}, &aSessions); err != nil { + t.Error(err) + } else if len(aSessions) != 1 { + t.Errorf("Unexpected number of sessions received: %+v", aSessions) + } else if aSessions[0].Usage != time.Duration(150)*time.Second { + t.Errorf("Expecting 2m30s, received usage: %v", aSessions[0].Usage) + } + eAcntVal = 150.0 + if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { + t.Error(err) + } else if acnt.BalanceMap[utils.VOICE].GetTotalValue() != eAcntVal { + t.Errorf("Expecting: %f, received: %f", eAcntVal, acnt.BalanceMap[utils.VOICE].GetTotalValue()) + } + smgEv = SMGenericEvent{ + utils.EVENT_NAME: smgEv[utils.EVENT_NAME], + utils.TOR: smgEv[utils.TOR], + utils.OriginIDPrefix: "12372", + utils.DIRECTION: smgEv[utils.DIRECTION], + utils.ACCOUNT: smgEv[utils.ACCOUNT], + utils.SUBJECT: smgEv[utils.SUBJECT], + utils.DESTINATION: smgEv[utils.DESTINATION], + utils.CATEGORY: smgEv[utils.CATEGORY], + utils.TENANT: smgEv[utils.TENANT], + utils.REQTYPE: smgEv[utils.REQTYPE], + utils.USAGE: "1m", // Total session usage + } + var rpl string + if err = smgRPC.Call("SMGenericV1.TerminateSession", smgEv, &rpl); err != nil || rpl != utils.OK { + t.Error(err) + } + time.Sleep(time.Duration(10) * time.Millisecond) + if err := smgRPC.Call("SMGenericV1.ActiveSessions", map[string]string{utils.MEDI_RUNID: utils.META_DEFAULT, + utils.ACCID: "12372-1"}, &aSessions); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Error(err, aSessions) + } + eAcntVal = 240 + if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { + t.Error(err) + } else if acnt.BalanceMap[utils.VOICE].GetTotalValue() != eAcntVal { + t.Errorf("Expecting: %f, received: %f", eAcntVal, acnt.BalanceMap[utils.VOICE].GetTotalValue()) + } + if err := smgRPC.Call("SMGenericV1.ProcessCDR", smgEv, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Received reply: %s", reply) + } + time.Sleep(time.Duration(20) * time.Millisecond) + var cdrs []*engine.ExternalCDR + req := utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}, DestinationPrefixes: []string{smgEv.GetDestination(utils.META_DEFAULT)}} + if err := smgRPC.Call("ApierV2.GetCdrs", req, &cdrs); err != nil { + t.Error("Unexpected error: ", err.Error()) + } else if len(cdrs) != 1 { + t.Error("Unexpected number of CDRs returned: ", len(cdrs)) + } else { + if cdrs[0].Usage != "60" { + t.Errorf("Unexpected CDR Usage received, cdr: %v %+v ", cdrs[0].Usage, cdrs[0]) + } + } + +} + func TestSMGVoiceSessionStopCgrEngine(t *testing.T) { if !*testIntegration { return diff --git a/sessionmanager/smg_session.go b/sessionmanager/smg_session.go index c15618d70..0b1619f2c 100644 --- a/sessionmanager/smg_session.go +++ b/sessionmanager/smg_session.go @@ -81,7 +81,6 @@ func (self *SMGSession) debitLoop(debitInterval time.Duration) { // Attempts to debit a duration, returns maximum duration which can be debitted or error func (self *SMGSession) debit(dur time.Duration, lastUsed *time.Duration) (time.Duration, error) { - //utils.Logger.Debug(fmt.Sprintf("### SMGSession.debit, dur: %+v, lastUsed: %+v, session: %+v", dur, lastUsed, self)) requestedDuration := dur if lastUsed != nil { self.ExtraDuration = self.LastDebit - *lastUsed diff --git a/sessionmanager/smgeneric.go b/sessionmanager/smgeneric.go index b07adc02d..5f3d59606 100644 --- a/sessionmanager/smgeneric.go +++ b/sessionmanager/smgeneric.go @@ -332,9 +332,12 @@ func (smg *SMGeneric) getSessionIDsForPrefix(prefix string, passiveSessions bool ssIndx = smg.pSessionsIndex } idxMux.RLock() - for originID := range ssIndx[utils.ACCID][utils.META_DEFAULT] { + // map[OriginID:map[12372-1:map[*default:511654dc4da7ce4706276cb458437cdd81d0e2b3]]] + for originID := range ssIndx[utils.ACCID] { if strings.HasPrefix(originID, prefix) { - cgrIDs = append(cgrIDs, ssIndx[utils.ACCID][originID][utils.META_DEFAULT].Slice()...) + if _, hasDefaultRun := ssIndx[utils.ACCID][originID][utils.META_DEFAULT]; hasDefaultRun { + cgrIDs = append(cgrIDs, ssIndx[utils.ACCID][originID][utils.META_DEFAULT].Slice()...) + } } } idxMux.RUnlock() @@ -864,7 +867,6 @@ func (smg *SMGeneric) ChargeEvent(gev SMGenericEvent) (maxUsage time.Duration, e cd.CgrID = cgrID cd.RunID = sR.CallDescriptor.RunID cd.Increments.Compress() - //utils.Logger.Info(fmt.Sprintf("Refunding session run callcost: %s", utils.ToJSON(cd))) var response float64 err = smg.rals.Call("Responder.RefundIncrements", cd, &response) if err != nil {