Start updating voice test in sessions

This commit is contained in:
TeoV
2019-02-11 16:29:45 +02:00
committed by Dan Christian Bogos
parent 2bf25a989f
commit 3bc6a7dac2
7 changed files with 716 additions and 368 deletions

View File

@@ -21,7 +21,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package v2
import (
"fmt"
"net/rpc"
"net/rpc/jsonrpc"
"path"
@@ -83,7 +82,6 @@ func TestITMongoTutorial(t *testing.T) {
func testTPitLoadConfig(t *testing.T) {
tpCfgPath = path.Join(*dataDir, "conf", "samples", configDIR)
if tpCfg, err = config.NewCGRConfigFromFolder(tpCfgPath); err != nil {
fmt.Println("err : ", err)
t.Error(err)
}
switch configDIR {

View File

@@ -48,6 +48,13 @@
"enabled": true,
},
"chargers": {
"enabled": true,
"attributes_conns": [
{"address": "*internal"}
],
},
"sessions": {
"enabled": true,
"session_ttl": "50ms",

View File

@@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package sessions
import (
"net/rpc"
"net/rpc/jsonrpc"
"path"
"testing"
@@ -30,6 +31,8 @@ import (
"github.com/cgrates/cgrates/utils"
)
var smgRPC *rpc.Client
func TestSMGDataInitCfg(t *testing.T) {
daCfgPath = path.Join(*dataDir, "conf", "samples", "smg")
// Init config first

View File

@@ -207,8 +207,8 @@ func (sr *SRun) Clone() *SRun {
func (sr *SRun) debitReserve(dur time.Duration, lastUsage *time.Duration) (rDur time.Duration) {
if lastUsage != nil &&
sr.LastUsage != *lastUsage {
sr.ExtraDuration -= sr.LastUsage
sr.ExtraDuration += *lastUsage
diffUsage := sr.LastUsage - *lastUsage
sr.ExtraDuration += diffUsage
sr.TotalUsage -= sr.LastUsage
sr.TotalUsage += *lastUsage
sr.LastUsage = *lastUsage
@@ -218,7 +218,6 @@ func (sr *SRun) debitReserve(dur time.Duration, lastUsage *time.Duration) (rDur
sr.ExtraDuration -= dur
sr.LastUsage = dur
sr.TotalUsage += dur
rDur = time.Duration(0) // complete debit from reserve
} else {
rDur = dur - sr.ExtraDuration
sr.ExtraDuration = 0

144
sessions/session_test.go Normal file
View File

@@ -0,0 +1,144 @@
/*
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 <http://www.gnu.org/licenses/>
*/
package sessions
import (
"testing"
"time"
)
//Test1 ExtraDuration 0 and LastUsage < initial
func TestSRunDebitReserve(t *testing.T) {
lastUsage := time.Duration(1*time.Minute + 30*time.Second)
duration := time.Duration(2 * time.Minute)
sr := &SRun{
ExtraDuration: time.Duration(0),
LastUsage: duration,
TotalUsage: duration,
}
if rDur := sr.debitReserve(duration, &lastUsage); rDur != lastUsage {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, rDur)
}
//start with extraDuration 0 and the difference go in rDur
if sr.ExtraDuration != time.Duration(0) {
t.Errorf("Expecting: %+v, received: %+v", time.Duration(0), sr.ExtraDuration)
}
if sr.LastUsage != lastUsage {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, sr.LastUsage)
}
if sr.TotalUsage != lastUsage {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, sr.TotalUsage)
}
}
//Test2 ExtraDuration 0 and LastUsage > initial
func TestSRunDebitReserve2(t *testing.T) {
lastUsage := time.Duration(2*time.Minute + 30*time.Second)
duration := time.Duration(2 * time.Minute)
sr := &SRun{
ExtraDuration: time.Duration(0),
LastUsage: duration,
TotalUsage: duration,
}
if rDur := sr.debitReserve(duration, &lastUsage); rDur != lastUsage {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, rDur)
}
if sr.ExtraDuration != time.Duration(0) {
t.Errorf("Expecting: %+v, received: %+v", time.Duration(0), sr.ExtraDuration)
}
if sr.LastUsage != lastUsage {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, sr.LastUsage)
}
if sr.TotalUsage != lastUsage {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, sr.TotalUsage)
}
}
//Test3 ExtraDuration ( 1m < duration) and LastUsage < initial
func TestSRunDebitReserve3(t *testing.T) {
lastUsage := time.Duration(1*time.Minute + 30*time.Second)
duration := time.Duration(2 * time.Minute)
sr := &SRun{
ExtraDuration: time.Duration(time.Minute),
LastUsage: duration,
TotalUsage: duration,
}
if rDur := sr.debitReserve(duration, &lastUsage); rDur != (duration - lastUsage) {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, rDur)
}
if sr.ExtraDuration != time.Duration(0) {
t.Errorf("Expecting: %+v, received: %+v", time.Duration(0), sr.ExtraDuration)
}
if sr.LastUsage != lastUsage {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, sr.LastUsage)
}
if sr.TotalUsage != lastUsage {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, sr.TotalUsage)
}
}
//Test4 ExtraDuration 1m and LastUsage > initial
func TestSRunDebitReserve4(t *testing.T) {
lastUsage := time.Duration(2*time.Minute + 30*time.Second)
duration := time.Duration(2 * time.Minute)
sr := &SRun{
ExtraDuration: time.Duration(time.Minute),
LastUsage: duration,
TotalUsage: duration,
}
//We have extraDuration 1 minute and 30s different
if rDur := sr.debitReserve(duration, &lastUsage); rDur != time.Duration(1*time.Minute+30*time.Second) {
t.Errorf("Expecting: %+v, received: %+v", time.Duration(1*time.Minute+30*time.Second), rDur)
}
if sr.ExtraDuration != time.Duration(0) {
t.Errorf("Expecting: %+v, received: %+v", time.Duration(0), sr.ExtraDuration)
}
if sr.LastUsage != lastUsage {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, sr.LastUsage)
}
if sr.TotalUsage != lastUsage {
t.Errorf("Expecting: %+v, received: %+v", lastUsage, sr.TotalUsage)
}
}
//Test5 ExtraDuration 3m ( > initialDuration) and LastUsage < initial
func TestSRunDebitReserve5(t *testing.T) {
lastUsage := time.Duration(1*time.Minute + 30*time.Second)
duration := time.Duration(2 * time.Minute)
sr := &SRun{
ExtraDuration: time.Duration(3 * time.Minute),
LastUsage: duration,
TotalUsage: duration,
}
//in debit reserve we start with an extraDuration 3m
//after we add the different dur-lastUsed (+30s)
if rDur := sr.debitReserve(duration, &lastUsage); rDur != time.Duration(0) {
t.Errorf("Expecting: %+v, received: %+v", time.Duration(0), rDur)
}
//ExtraDuration (3m30s - 2m)
if sr.ExtraDuration != time.Duration(1*time.Minute+30*time.Second) {
t.Errorf("Expecting: %+v, received: %+v", time.Duration(1*time.Minute+30*time.Second), sr.ExtraDuration)
}
if sr.LastUsage != duration {
t.Errorf("Expecting: %+v, received: %+v", duration, sr.LastUsage)
}
if sr.TotalUsage != time.Duration(3*time.Minute+30*time.Second) {
t.Errorf("Expecting: %+v, received: %+v", time.Duration(3*time.Minute+30*time.Second), sr.TotalUsage)
}
}

View File

@@ -435,6 +435,7 @@ func (sS *SessionS) forceSTerminate(s *Session, extraDebit time.Duration, lastUs
// debitSession performs debit for a session run
func (sS *SessionS) debitSession(s *Session, sRunIdx int, dur time.Duration,
lastUsed *time.Duration) (maxDur time.Duration, err error) {
s.Lock()
if sRunIdx >= len(s.SRuns) {
err = errors.New("sRunIdx out of range")
@@ -1202,7 +1203,7 @@ func (sS *SessionS) updateSession(s *Session, updtEv engine.MapEvent) (maxUsage
sr.Event.GetStringIgnoreErrors(utils.RequestType)) {
rplyMaxUsage = time.Duration(-1)
} else if rplyMaxUsage, err = sS.debitSession(s, i, reqMaxUsage,
sr.Event.GetDurationPtrIgnoreErrors(utils.LastUsed)); err != nil {
updtEv.GetDurationPtrIgnoreErrors(utils.LastUsed)); err != nil {
return
}
if !maxUsageSet ||
@@ -2041,6 +2042,8 @@ func (sS *SessionS) BiRPCv1UpdateSession(clnt rpcclient.RpcClientConnection,
} else {
s = ss[0]
}
fmt.Println("=====================")
fmt.Println("Here enter in updateSession from UPDATE API")
if maxUsage, err := sS.updateSession(s, ev.AsMapInterface()); err != nil {
return utils.NewErrRALs(err)
} else {