From 05cf7db8d53f1518ef97073be2cbd344a3b6fdbe Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 15 Feb 2019 13:00:18 +0200 Subject: [PATCH] Finish initegration test for new session update --- agents/diamagent.go | 1 - agents/radagent_it_test.go | 17 ++++++++++++++++- apier/v1/sessionsv1_it_test.go | 2 +- cmd/cgr-engine/cgr-engine.go | 8 ++++++++ data/conf/samples/radagent/cgrates.json | 9 +++++++++ sessions/sessions.go | 14 ++++++++++---- sessions/sessions_birpc_it_test.go | 13 ++++++------- sessions/sessions_it_test.go | 2 +- 8 files changed, 51 insertions(+), 15 deletions(-) diff --git a/agents/diamagent.go b/agents/diamagent.go index 6a812848e..2560b0972 100644 --- a/agents/diamagent.go +++ b/agents/diamagent.go @@ -61,7 +61,6 @@ func NewDiameterAgent(cgrCfg *config.CGRConfig, filterS *engine.FilterS, procsr.ReplyFields = tpls } } - //da.sS.SetClientConn(da) // pass the connection to sessionS back so we can receive the disconnects return da, nil } diff --git a/agents/radagent_it_test.go b/agents/radagent_it_test.go index 087c07670..ac93c1599 100644 --- a/agents/radagent_it_test.go +++ b/agents/radagent_it_test.go @@ -90,6 +90,21 @@ func TestRAitTPFromFolder(t *testing.T) { t.Error(err) } time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups + + //add a default charger + chargerProfile := &engine.ChargerProfile{ + Tenant: "cgrates.org", + ID: "Default", + RunID: "*default", + AttributeIDs: []string{"*none"}, + Weight: 20, + } + var result string + if err := raRPC.Call("ApierV1.SetChargerProfile", chargerProfile, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } } func TestRAitAuth(t *testing.T) { @@ -260,7 +275,7 @@ func TestRAitAcctStop(t *testing.T) { } // Make sure the sessin was disconnected from SMG var aSessions []*sessions.ActiveSession - if err := raRPC.Call("SMGenericV1.GetActiveSessions", + if err := raRPC.Call(utils.SessionSv1GetActiveSessions, map[string]string{utils.RunID: utils.META_DEFAULT, utils.OriginID: "e4921177ab0e3586c37f6a185864b71a@0:0:0:0:0:0:0:0-51585361-75c2f57b"}, &aSessions); err == nil || err.Error() != utils.ErrNotFound.Error() { t.Error(err) diff --git a/apier/v1/sessionsv1_it_test.go b/apier/v1/sessionsv1_it_test.go index 6e6aefbd4..a79795a51 100644 --- a/apier/v1/sessionsv1_it_test.go +++ b/apier/v1/sessionsv1_it_test.go @@ -835,7 +835,7 @@ func TestSSv1ItDynamicDebit(t *testing.T) { args1, &rply1); err != nil { t.Error(err) return - } else if *rply1.MaxUsage != time.Duration(0*time.Second) { + } else if *rply1.MaxUsage != time.Duration(-1) { t.Errorf("Unexpected MaxUsage: %v", rply1.MaxUsage) } diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 2503e25fa..63f05f9eb 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -378,6 +378,14 @@ func startDiameterAgent(internalSsChan chan rpcclient.RpcClientConnection, } if sSInternal { // bidirectional client backwards connection sS.(*utils.BiRPCInternalClient).SetClientConn(da) + var rply string + if err := sS.Call(utils.SessionSv1RegisterInternalBiJSONConn, + utils.EmptyString, &rply); err != nil { + utils.Logger.Crit(fmt.Sprintf("<%s> Could not connect to %s: %s", + utils.DiameterAgent, utils.SessionS, err.Error())) + exitChan <- true + return + } } if err = da.ListenAndServe(); err != nil { utils.Logger.Err(fmt.Sprintf(" error: %s!", err)) diff --git a/data/conf/samples/radagent/cgrates.json b/data/conf/samples/radagent/cgrates.json index 0863d45b6..f7003aef3 100644 --- a/data/conf/samples/radagent/cgrates.json +++ b/data/conf/samples/radagent/cgrates.json @@ -87,12 +87,15 @@ "flags": ["*auth", "*accounts"], "continue_on_success": false, "request_fields":[ + {"tag": "Category", "field_id": "Category", "type": "*constant", "value": "call"}, {"tag": "RequestType", "field_id": "RequestType", "type": "*constant", "value": "*prepaid", "mandatory": true}, {"tag": "OriginID", "field_id": "OriginID", "type": "*composed", "value": "~*req.Acct-Session-Id;-;~*req.Sip-From-Tag", "mandatory": true}, {"tag": "Account", "field_id": "Account", "type": "*composed", "value": "~*req.User-Name", "mandatory": true}, + {"tag": "Subject", "field_id": "Subject", "type": "*variable", + "value": "~*req.User-Name", "mandatory": true}, {"tag": "Destination", "field_id": "Destination", "type": "*composed", "value": "~*req.Called-Station-Id", "mandatory": true}, {"tag": "SetupTime", "field_id": "SetupTime", "type": "*composed", @@ -111,6 +114,7 @@ "flags": ["*initiate", "*attributes", "*resources", "*accounts"], "continue_on_success": false, "request_fields":[ + {"tag": "Category", "field_id": "Category", "type": "*constant", "value": "call"}, {"tag": "RequestType", "field_id": "RequestType", "type": "*constant", "value": "*prepaid", "mandatory": true}, {"tag": "OriginID", "field_id": "OriginID", "type": "*composed", @@ -119,6 +123,8 @@ "value": "~*req.NAS-IP-Address", "mandatory": true}, {"tag": "Account", "field_id": "Account", "type": "*composed", "value": "~*req.User-Name", "mandatory": true}, + {"tag": "Subject", "field_id": "Subject", "type": "*variable", + "value": "~*req.User-Name", "mandatory": true}, {"tag": "Destination", "field_id": "Destination", "type": "*composed", "value": "~*req.Called-Station-Id", "mandatory": true}, {"tag": "SetupTime", "field_id": "SetupTime", "type": "*composed", @@ -134,6 +140,7 @@ "flags": ["*terminate", "*resources", "*accounts", "*cdrs"], "continue_on_success": false, "request_fields":[ + {"tag": "Category", "field_id": "Category", "type": "*constant", "value": "call"}, {"tag": "RequestType", "field_id": "RequestType", "type": "*constant", "value": "*prepaid", "mandatory": true}, {"tag": "OriginID", "field_id": "OriginID", "type": "*composed", @@ -142,6 +149,8 @@ "value": "~*req.NAS-IP-Address", "mandatory": true}, {"tag": "Account", "field_id": "Account", "type": "*composed", "value": "~*req.User-Name", "mandatory": true}, + {"tag": "Subject", "field_id": "Subject", "type": "*variable", + "value": "~*req.User-Name", "mandatory": true}, {"tag": "Destination", "field_id": "Destination", "type": "*composed", "value": "~*req.Called-Station-Id", "mandatory": true}, {"tag": "SetupTime", "field_id": "SetupTime", "type": "*composed", diff --git a/sessions/sessions.go b/sessions/sessions.go index 39db9d9dd..1abe51f43 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -442,7 +442,9 @@ func (sS *SessionS) debitSession(s *Session, sRunIdx int, dur time.Duration, s.Unlock() return } + sr := s.SRuns[sRunIdx] + rDur := sr.debitReserve(dur, lastUsed) // debit out of reserve, rDur is still to be debited if rDur == time.Duration(0) { s.Unlock() @@ -1164,6 +1166,7 @@ func (sS *SessionS) initSession(tnt string, evStart *engine.SafEvent, clntConnID if dbtItval != 0 && sr.Event.GetStringIgnoreErrors(utils.RequestType) == utils.META_PREPAID { go sS.debitLoopSession(s, i, dbtItval) + time.Sleep(1) } } sS.registerSession(s, false) // make the session available to the rest of the system @@ -1202,7 +1205,6 @@ func (sS *SessionS) updateSession(s *Session, updtEv engine.MapEvent) (maxUsage var maxUsageSet bool // so we know if we have set the 0 on purpose prepaidReqs := []string{utils.META_PREPAID, utils.META_PSEUDOPREPAID} for i, sr := range s.SRuns { - var rplyMaxUsage time.Duration if !utils.IsSliceMember(prepaidReqs, sr.Event.GetStringIgnoreErrors(utils.RequestType)) { @@ -1864,10 +1866,14 @@ func (sS *SessionS) BiRPCv1InitiateSession(clnt rpcclient.RpcClientConnection, if err != nil { return utils.NewErrRALs(err) } - if maxUsage, err := sS.updateSession(s, nil); err != nil { - return utils.NewErrRALs(err) + if dbtItvl > 0 { //active debit + rply.MaxUsage = utils.DurationPointer(time.Duration(-1)) } else { - rply.MaxUsage = &maxUsage + if maxUsage, err := sS.updateSession(s, nil); err != nil { + return utils.NewErrRALs(err) + } else { + rply.MaxUsage = &maxUsage + } } } if args.ProcessThresholds { diff --git a/sessions/sessions_birpc_it_test.go b/sessions/sessions_birpc_it_test.go index 4e6b3af23..3be5ed4ec 100644 --- a/sessions/sessions_birpc_it_test.go +++ b/sessions/sessions_birpc_it_test.go @@ -147,7 +147,6 @@ func TestSessionsBiRPCSessionAutomaticDisconnects(t *testing.T) { acnt.BalanceMap[utils.VOICE].GetTotalValue()) } - usage := time.Duration(10 * time.Millisecond) initArgs := &V1InitSessionArgs{ InitSession: true, CGREvent: utils.CGREvent{ @@ -175,9 +174,9 @@ func TestSessionsBiRPCSessionAutomaticDisconnects(t *testing.T) { initArgs, &initRpl); err != nil { t.Error(err) } - - if *initRpl.MaxUsage != usage { - t.Errorf("Expecting : %+v, received: %+v", usage, *initRpl.MaxUsage) + expMaxUsage := time.Duration(-1) + if *initRpl.MaxUsage != expMaxUsage { + t.Errorf("Expecting : %+v, received: %+v", expMaxUsage, *initRpl.MaxUsage) } // Make sure we are receiving a disconnect event @@ -271,7 +270,6 @@ func TestSessionsBiRPCSessionOriginatorTerminate(t *testing.T) { t.Errorf("Expecting: %f, received: %f", eAcntVal, acnt.BalanceMap[utils.VOICE].GetTotalValue()) } - usage := time.Duration(200 * time.Millisecond) initArgs := &V1InitSessionArgs{ InitSession: true, CGREvent: utils.CGREvent{ @@ -300,8 +298,9 @@ func TestSessionsBiRPCSessionOriginatorTerminate(t *testing.T) { t.Error(err) } - if *initRpl.MaxUsage != usage { - t.Errorf("Expecting : %+v, received: %+v", usage, *initRpl.MaxUsage) + expMaxUsage := time.Duration(-1) + if *initRpl.MaxUsage != expMaxUsage { + t.Errorf("Expecting : %+v, received: %+v", expMaxUsage, *initRpl.MaxUsage) } time.Sleep(time.Duration(10 * time.Millisecond)) // Give time for debits to occur diff --git a/sessions/sessions_it_test.go b/sessions/sessions_it_test.go index 7dacbb604..4bf9e81f6 100644 --- a/sessions/sessions_it_test.go +++ b/sessions/sessions_it_test.go @@ -199,7 +199,7 @@ func TestSessionsItUpdateUnexist(t *testing.T) { time.Sleep(10 * time.Millisecond) - eAcntVal = 8.283100 + eAcntVal = 8.099800 if err := sItRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { t.Error(err) } else if acnt.BalanceMap[utils.MONETARY].GetTotalValue() != eAcntVal {