Updated SessionSv1.ProcessEvent for *rals flag

This commit is contained in:
Trial97
2020-07-21 12:38:31 +03:00
committed by Dan Christian Bogos
parent c97314202f
commit a961d6225d
4 changed files with 84 additions and 13 deletions

View File

@@ -161,6 +161,7 @@ func testSSv1ItProcessEventAuth(t *testing.T) {
Flags: []string{utils.ConcatenatedKey(utils.MetaResources, utils.MetaAuthorize),
utils.ConcatenatedKey(utils.MetaResources, utils.MetaDerivedReply),
utils.ConcatenatedKey(utils.MetaRALs, utils.MetaAuthorize),
utils.ConcatenatedKey(utils.MetaRALs, utils.MetaDerivedReply),
utils.MetaRoutes, utils.MetaAttributes, utils.MetaChargers},
CGREvent: &utils.CGREvent{
Tenant: "cgrates.org",
@@ -182,8 +183,14 @@ func testSSv1ItProcessEventAuth(t *testing.T) {
if err := sSv1BiRpc.Call(utils.SessionSv1ProcessEvent, args, &rply); err != nil {
t.Fatal(err)
}
if rply.MaxUsage == nil || rply.MaxUsage["CustomerCharges"] != authUsage {
t.Errorf("Unexpected MaxUsage: %v", rply.MaxUsage)
expMaxUsage := map[string]time.Duration{
"CustomerCharges": authUsage,
"SupplierCharges": authUsage,
"raw": authUsage,
utils.MetaRaw: authUsage,
}
if !reflect.DeepEqual(expMaxUsage, rply.MaxUsage) {
t.Errorf("Expected %s received %s", expMaxUsage, rply.MaxUsage)
}
if rply.ResourceAllocation == nil || rply.ResourceAllocation["CustomerCharges"] == utils.EmptyString {
t.Errorf("Unexpected ResourceAllocation: %s", rply.ResourceAllocation)
@@ -240,6 +247,7 @@ func testSSv1ItProcessEventInitiateSession(t *testing.T) {
initUsage := 5 * time.Minute
args := &sessions.V1ProcessEventArgs{
Flags: []string{utils.ConcatenatedKey(utils.MetaRALs, utils.MetaInitiate),
utils.ConcatenatedKey(utils.MetaRALs, utils.MetaDerivedReply),
utils.ConcatenatedKey(utils.MetaResources, utils.MetaAllocate),
utils.ConcatenatedKey(utils.MetaResources, utils.MetaDerivedReply),
utils.MetaAttributes, utils.MetaChargers},
@@ -268,8 +276,14 @@ func testSSv1ItProcessEventInitiateSession(t *testing.T) {
// in case of prepaid and pseudoprepade we expect a MaxUsage of 5min
// and in case of postpaid and rated we expect the value of Usage field
// if this was missing the MaxUsage should be equal to MaxCallDuration from config
if rply.MaxUsage == nil || rply.MaxUsage["CustomerCharges"] != initUsage {
t.Errorf("Unexpected MaxUsage: %v", rply.MaxUsage)
expMaxUsage := map[string]time.Duration{
"CustomerCharges": initUsage,
"SupplierCharges": initUsage,
"raw": initUsage,
utils.MetaRaw: initUsage,
}
if !reflect.DeepEqual(expMaxUsage, rply.MaxUsage) {
t.Errorf("Expected %s received %s", expMaxUsage, rply.MaxUsage)
}
if rply.ResourceAllocation == nil || rply.ResourceAllocation["CustomerCharges"] != "RES_ACNT_1001" {
t.Errorf("Unexpected ResourceAllocation: %s", rply.ResourceAllocation)
@@ -310,7 +324,9 @@ func testSSv1ItProcessEventInitiateSession(t *testing.T) {
func testSSv1ItProcessEventUpdateSession(t *testing.T) {
reqUsage := 5 * time.Minute
args := &sessions.V1ProcessEventArgs{
Flags: []string{utils.ConcatenatedKey(utils.MetaRALs, utils.MetaUpdate), utils.MetaAttributes},
Flags: []string{utils.ConcatenatedKey(utils.MetaRALs, utils.MetaUpdate),
utils.ConcatenatedKey(utils.MetaRALs, utils.MetaDerivedReply),
utils.MetaAttributes},
CGREvent: &utils.CGREvent{
Tenant: "cgrates.org",
ID: "testSSv1ItProcessEventUpdateSession",
@@ -361,8 +377,14 @@ func testSSv1ItProcessEventUpdateSession(t *testing.T) {
// in case of prepaid and pseudoprepade we expect a MaxUsage of 5min
// and in case of postpaid and rated we expect the value of Usage field
// if this was missing the MaxUsage should be equal to MaxCallDuration from config
if rply.MaxUsage == nil || rply.MaxUsage["CustomerCharges"] != reqUsage {
t.Errorf("Unexpected MaxUsage: %v", rply.MaxUsage)
expMaxUsage := map[string]time.Duration{
"CustomerCharges": reqUsage,
"SupplierCharges": reqUsage,
"raw": reqUsage,
utils.MetaRaw: reqUsage,
}
if !reflect.DeepEqual(expMaxUsage, rply.MaxUsage) {
t.Errorf("Expected %s received %s", expMaxUsage, rply.MaxUsage)
}
aSessions := make([]*sessions.ExternalSession, 0)
if err := sSv1BiRpc.Call(utils.SessionSv1GetActiveSessions, &utils.SessionFilter{}, &aSessions); err != nil {

View File

@@ -273,3 +273,22 @@ func getDerivedEvents(events map[string]*utils.CGREventWithOpts, derivedReply bo
utils.MetaRaw: events[utils.MetaRaw],
}
}
// getDerivedMaxUsage returns only the *raw MaxUsage if derivedReply flag is not specified
func getDerivedMaxUsage(maxUsages map[string]time.Duration, derivedReply bool) (out map[string]time.Duration) {
if derivedReply {
out = maxUsages
} else {
out = make(map[string]time.Duration)
}
var maxUsage time.Duration
var maxUsageSet bool // so we know if we have set the 0 on purpose
for _, rplyMaxUsage := range maxUsages {
if !maxUsageSet || rplyMaxUsage < maxUsage {
maxUsage = rplyMaxUsage
maxUsageSet = true
}
}
out[utils.MetaRaw] = maxUsage
return
}

View File

@@ -311,3 +311,27 @@ func TestGetDerivedEvents(t *testing.T) {
t.Errorf("Expected %s received %s", utils.ToJSON(exp), utils.ToJSON(rply))
}
}
func TestGetDerivedMaxUsage(t *testing.T) {
max := map[string]time.Duration{
utils.MetaDefault: time.Second,
"CustomRoute": time.Hour,
}
exp := map[string]time.Duration{
utils.MetaRaw: time.Second,
utils.MetaDefault: time.Second,
"CustomRoute": time.Hour,
}
if rply := getDerivedMaxUsage(max, true); !reflect.DeepEqual(exp, rply) {
t.Errorf("Expected %s received %s", utils.ToJSON(exp), utils.ToJSON(rply))
}
exp = map[string]time.Duration{
utils.MetaRaw: time.Second,
}
if rply := getDerivedMaxUsage(max, false); !reflect.DeepEqual(exp, rply) {
t.Errorf("Expected %s received %s", utils.ToJSON(exp), utils.ToJSON(rply))
}
}

View File

@@ -3416,13 +3416,15 @@ func (sS *SessionS) BiRPCv1ProcessEvent(clnt rpcclient.ClientConnector,
switch {
//check for auth session
case ralsOpts.Has(utils.MetaAuthorize):
if rply.MaxUsage, err = sS.authEvent(&utils.CGREventWithOpts{
var sRunsMaxUsage map[string]time.Duration
if sRunsMaxUsage, err = sS.authEvent(&utils.CGREventWithOpts{
CGREvent: args.CGREvent,
Opts: args.Opts,
ArgDispatcher: args.ArgDispatcher,
}, ralsOpts.Has(utils.MetaFD)); err != nil {
return err
}
rply.MaxUsage = getDerivedMaxUsage(sRunsMaxUsage, ralsOpts.Has(utils.MetaDerivedReply))
// check for init session
case ralsOpts.Has(utils.MetaInitiate):
if opts.HasField(utils.OptsDebitInterval) { // dynamic DebitInterval via CGRDebitInterval
@@ -3439,14 +3441,16 @@ func (sS *SessionS) BiRPCv1ProcessEvent(clnt rpcclient.ClientConnector,
if err != nil {
return err
}
sRunsMaxUsage := make(map[string]time.Duration)
if s.debitStop != nil { //active debit
for _, sr := range s.SRuns {
rply.MaxUsage[sr.CD.RunID] = sS.cgrCfg.SessionSCfg().MaxCallDuration
sRunsMaxUsage[sr.CD.RunID] = sS.cgrCfg.SessionSCfg().MaxCallDuration
}
} else if rply.MaxUsage, err = sS.updateSession(s, nil, args.Opts, false); err != nil {
} else if sRunsMaxUsage, err = sS.updateSession(s, nil, args.Opts, false); err != nil {
return utils.NewErrRALs(err)
}
//check for update session
rply.MaxUsage = getDerivedMaxUsage(sRunsMaxUsage, ralsOpts.Has(utils.MetaDerivedReply))
//check for update session
case ralsOpts.Has(utils.MetaUpdate):
if opts.HasField(utils.OptsDebitInterval) { // dynamic DebitInterval via CGRDebitInterval
if dbtItvl, err = opts.GetDuration(utils.OptsDebitInterval); err != nil {
@@ -3467,10 +3471,12 @@ func (sS *SessionS) BiRPCv1ProcessEvent(clnt rpcclient.ClientConnector,
return err
}
}
if rply.MaxUsage, err = sS.updateSession(s, ev, args.Opts, false); err != nil {
var sRunsMaxUsage map[string]time.Duration
if sRunsMaxUsage, err = sS.updateSession(s, ev, args.Opts, false); err != nil {
return utils.NewErrRALs(err)
}
// check for terminate session
rply.MaxUsage = getDerivedMaxUsage(sRunsMaxUsage, ralsOpts.Has(utils.MetaDerivedReply))
// check for terminate session
case ralsOpts.Has(utils.MetaTerminate):
if opts.HasField(utils.OptsDebitInterval) { // dynamic DebitInterval via CGRDebitInterval
if dbtItvl, err = opts.GetDuration(utils.OptsDebitInterval); err != nil {