mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 10:06:24 +05:00
more tests and better checking
This commit is contained in:
@@ -223,7 +223,7 @@ func startMediator(responder *timespans.Responder, loggerDb timespans.DataStorag
|
||||
}
|
||||
|
||||
m, err := mediator.NewMediator(connector, loggerDb, mediator_skipdb, mediator_cdr_out_path, freeswitch_direction, freeswitch_tor, freeswitch_tenant, freeswitch_subject, freeswitch_account, freeswitch_destination, freeswitch_time_start, freeswitch_duration, freeswitch_uuid)
|
||||
if err != nil || m.ValidateIndexses() {
|
||||
if err != nil {
|
||||
timespans.Logger.Crit(fmt.Sprintf("Mediator config parsing error: %v", err))
|
||||
exitChan <- true
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func (mfi *mediatorFieldIdxs) Load(idxs string) error {
|
||||
}
|
||||
for _, cfgStrIdx := range cfgStrIdxs {
|
||||
if cfgIntIdx, errConv := strconv.Atoi(cfgStrIdx); errConv != nil || cfgStrIdx == "" {
|
||||
return fmt.Errorf("All %s members must be ints", idxs)
|
||||
return fmt.Errorf("All mediator index members (%s) must be ints", idxs)
|
||||
} else {
|
||||
*mfi = append(*mfi, cfgIntIdx)
|
||||
}
|
||||
@@ -66,55 +66,37 @@ type Mediator struct {
|
||||
uuidIndexs mediatorFieldIdxs
|
||||
}
|
||||
|
||||
// Creates a new mediator object parsing the indexses
|
||||
func NewMediator(connector timespans.Connector,
|
||||
loggerDb timespans.DataStorage,
|
||||
skipDb bool,
|
||||
outputDir string,
|
||||
directionIndexs, torIndexs, tenantIndexs, subjectIndexs, accountIndexs, destinationIndexs, timeStartIndexs, durationIndexs, uuidIndexs string) (m *Mediator, err error) {
|
||||
directionIndexs, torIndexs, tenantIndexs, subjectIndexs, accountIndexs, destinationIndexs,
|
||||
timeStartIndexs, durationIndexs, uuidIndexs string) (m *Mediator, err error) {
|
||||
m = &Mediator{
|
||||
connector: connector,
|
||||
loggerDb: loggerDb,
|
||||
skipDb: skipDb,
|
||||
outputDir: outputDir,
|
||||
}
|
||||
err = m.directionIndexs.Load(directionIndexs)
|
||||
if err != nil {
|
||||
return
|
||||
idxs := []string{directionIndexs, torIndexs, tenantIndexs, subjectIndexs, accountIndexs,
|
||||
destinationIndexs, timeStartIndexs, durationIndexs, uuidIndexs}
|
||||
objs := []mediatorFieldIdxs{m.directionIndexs, m.torIndexs, m.tenantIndexs, m.subjectIndexs,
|
||||
m.accountIndexs, m.destinationIndexs, m.timeStartIndexs, m.durationIndexs, m.uuidIndexs}
|
||||
for i, o := range objs {
|
||||
err = o.Load(idxs[i])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
err = m.torIndexs.Load(torIndexs)
|
||||
if err != nil {
|
||||
return
|
||||
if !m.validateIndexses() {
|
||||
err = fmt.Errorf("All members must have the same length")
|
||||
}
|
||||
err = m.tenantIndexs.Load(tenantIndexs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = m.subjectIndexs.Load(subjectIndexs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = m.accountIndexs.Load(accountIndexs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = m.destinationIndexs.Load(destinationIndexs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = m.timeStartIndexs.Load(timeStartIndexs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = m.durationIndexs.Load(durationIndexs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = m.uuidIndexs.Load(uuidIndexs)
|
||||
return
|
||||
}
|
||||
|
||||
// Make sure all indexes are having same lenght
|
||||
func (m *Mediator) ValidateIndexses() bool {
|
||||
func (m *Mediator) validateIndexses() bool {
|
||||
refLen := len(m.subjectIndexs)
|
||||
for _, fldIdxs := range []mediatorFieldIdxs{m.directionIndexs, m.torIndexs, m.tenantIndexs,
|
||||
m.accountIndexs, m.destinationIndexs, m.timeStartIndexs, m.durationIndexs, m.uuidIndexs} {
|
||||
@@ -125,6 +107,7 @@ func (m *Mediator) ValidateIndexses() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Watch the specified folder for file moves and parse the files on events
|
||||
func (m *Mediator) TrackCDRFiles(cdrPath string) (err error) {
|
||||
watcher, err := inotify.NewWatcher()
|
||||
if err != nil {
|
||||
@@ -152,6 +135,7 @@ func (m *Mediator) TrackCDRFiles(cdrPath string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Parse the files and get cost for every record
|
||||
func (m *Mediator) parseCSV(cdrfn string) (err error) {
|
||||
flag.Parse()
|
||||
file, err := os.Open(cdrfn)
|
||||
@@ -176,12 +160,12 @@ func (m *Mediator) parseCSV(cdrfn string) (err error) {
|
||||
var cc *timespans.CallCost
|
||||
for runIdx := range m.subjectIndexs { // Query costs for every run index given by subject
|
||||
if runIdx == 0 && !m.skipDb { // The first index is matching the session manager one
|
||||
cc, err = m.GetCostsFromDB(record, runIdx)
|
||||
cc, err = m.getCostsFromDB(record, runIdx)
|
||||
if err != nil || cc == nil { // Fallback on rater if no db record found
|
||||
cc, err = m.GetCostsFromRater(record, runIdx)
|
||||
cc, err = m.getCostsFromRater(record, runIdx)
|
||||
}
|
||||
} else {
|
||||
cc, err = m.GetCostsFromRater(record, runIdx)
|
||||
cc, err = m.getCostsFromRater(record, runIdx)
|
||||
}
|
||||
cost := "-1"
|
||||
if err != nil {
|
||||
@@ -199,13 +183,15 @@ func (m *Mediator) parseCSV(cdrfn string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Mediator) GetCostsFromDB(record []string, runIdx int) (cc *timespans.CallCost, err error) {
|
||||
// Retrive the cost from logging database
|
||||
func (m *Mediator) getCostsFromDB(record []string, runIdx int) (cc *timespans.CallCost, err error) {
|
||||
searchedUUID := record[m.uuidIndexs[runIdx]]
|
||||
cc, err = m.loggerDb.GetCallCostLog(searchedUUID, timespans.SESSION_MANAGER_SOURCE)
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Mediator) GetCostsFromRater(record []string, runIdx int) (cc *timespans.CallCost, err error) {
|
||||
// Retrive the cost from rater
|
||||
func (m *Mediator) getCostsFromRater(record []string, runIdx int) (cc *timespans.CallCost, err error) {
|
||||
d, err := time.ParseDuration(record[m.durationIndexs[runIdx]] + "s")
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
54
mediator/mediator_test.go
Normal file
54
mediator/mediator_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package mediator
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIndexLoad(t *testing.T) {
|
||||
idxs := make(mediatorFieldIdxs, 0)
|
||||
err := idxs.Load("1,2,3")
|
||||
if err != nil && len(idxs) != 3 {
|
||||
t.Error("Error parsing indexses ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexLoadError(t *testing.T) {
|
||||
idxs := make(mediatorFieldIdxs, 0)
|
||||
err := idxs.Load("1,a,2")
|
||||
if err == nil {
|
||||
t.Error("Error parsing indexses ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexLoadEmpty(t *testing.T) {
|
||||
idxs := make(mediatorFieldIdxs, 0)
|
||||
err := idxs.Load("")
|
||||
if err == nil {
|
||||
t.Error("Error parsing indexses ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexLengthSame(t *testing.T) {
|
||||
m := new(Mediator)
|
||||
objs := []mediatorFieldIdxs{m.directionIndexs, m.torIndexs, m.tenantIndexs, m.subjectIndexs,
|
||||
m.accountIndexs, m.destinationIndexs, m.timeStartIndexs, m.durationIndexs, m.uuidIndexs}
|
||||
for _, o := range objs {
|
||||
o.Load("1,2,3")
|
||||
}
|
||||
if !m.validateIndexses() {
|
||||
t.Error("Error checking length")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexLengthDifferent(t *testing.T) {
|
||||
m := new(Mediator)
|
||||
objs := []mediatorFieldIdxs{m.directionIndexs, m.torIndexs, m.tenantIndexs, m.subjectIndexs,
|
||||
m.accountIndexs, m.timeStartIndexs, m.durationIndexs, m.uuidIndexs}
|
||||
for _, o := range objs {
|
||||
o.Load("1,2,3")
|
||||
}
|
||||
m.destinationIndexs.Load("4,5")
|
||||
if m.validateIndexses() {
|
||||
t.Error("Error checking length")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user