Files
cgrates/engine/libstats_test.go
2019-04-05 16:14:53 +02:00

193 lines
6.2 KiB
Go

/*
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 engine
import (
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/utils"
)
var sq *StatQueue
func TestStatQueuesSort(t *testing.T) {
sInsts := StatQueues{
&StatQueue{sqPrfl: &StatQueueProfile{ID: "FIRST", Weight: 30.0}},
&StatQueue{sqPrfl: &StatQueueProfile{ID: "SECOND", Weight: 40.0}},
&StatQueue{sqPrfl: &StatQueueProfile{ID: "THIRD", Weight: 30.0}},
&StatQueue{sqPrfl: &StatQueueProfile{ID: "FOURTH", Weight: 35.0}},
}
sInsts.Sort()
eSInst := StatQueues{
&StatQueue{sqPrfl: &StatQueueProfile{ID: "SECOND", Weight: 40.0}},
&StatQueue{sqPrfl: &StatQueueProfile{ID: "FOURTH", Weight: 35.0}},
&StatQueue{sqPrfl: &StatQueueProfile{ID: "FIRST", Weight: 30.0}},
&StatQueue{sqPrfl: &StatQueueProfile{ID: "THIRD", Weight: 30.0}},
}
if !reflect.DeepEqual(eSInst, sInsts) {
t.Errorf("expecting: %+v, received: %+v", eSInst, sInsts)
}
}
func TestStatRemEventWithID(t *testing.T) {
sq = &StatQueue{
SQMetrics: map[string]StatMetric{
utils.MetaASR: &StatASR{
Answered: 1,
Count: 2,
Events: map[string]*StatWithCompress{
"cgrates.org:TestRemEventWithID_1": &StatWithCompress{Stat: 1},
"cgrates.org:TestRemEventWithID_2": &StatWithCompress{Stat: 0},
},
},
},
}
asrMetric := sq.SQMetrics[utils.MetaASR].(*StatASR)
if asr := asrMetric.GetFloat64Value(); asr != 50 {
t.Errorf("received asrMetric: %v", asrMetric)
}
sq.remEventWithID("cgrates.org:TestRemEventWithID_1")
if asr := asrMetric.GetFloat64Value(); asr != 0 {
t.Errorf("received asrMetric: %v", asrMetric)
} else if len(asrMetric.Events) != 1 {
t.Errorf("unexpected Events in asrMetric: %+v", asrMetric.Events)
}
sq.remEventWithID("cgrates.org:TestRemEventWithID_5") // non existent
if asr := asrMetric.GetFloat64Value(); asr != 0 {
t.Errorf("received asrMetric: %v", asrMetric)
} else if len(asrMetric.Events) != 1 {
t.Errorf("unexpected Events in asrMetric: %+v", asrMetric.Events)
}
sq.remEventWithID("cgrates.org:TestRemEventWithID_2")
if asr := asrMetric.GetFloat64Value(); asr != -1 {
t.Errorf("received asrMetric: %v", asrMetric)
} else if len(asrMetric.Events) != 0 {
t.Errorf("unexpected Events in asrMetric: %+v", asrMetric.Events)
}
sq.remEventWithID("cgrates.org:TestRemEventWithID_2")
if asr := asrMetric.GetFloat64Value(); asr != -1 {
t.Errorf("received asrMetric: %v", asrMetric)
} else if len(asrMetric.Events) != 0 {
t.Errorf("unexpected Events in asrMetric: %+v", asrMetric.Events)
}
}
func TestStatRemExpired(t *testing.T) {
sq = &StatQueue{
SQMetrics: map[string]StatMetric{
utils.MetaASR: &StatASR{
Answered: 2,
Count: 3,
Events: map[string]*StatWithCompress{
"cgrates.org:TestStatRemExpired_1": &StatWithCompress{Stat: 1},
"cgrates.org:TestStatRemExpired_2": &StatWithCompress{Stat: 0},
"cgrates.org:TestStatRemExpired_3": &StatWithCompress{Stat: 1},
},
},
},
SQItems: []SQItem{
SQItem{"cgrates.org:TestStatRemExpired_1", utils.TimePointer(time.Now())},
SQItem{"cgrates.org:TestStatRemExpired_2", utils.TimePointer(time.Now())},
SQItem{"cgrates.org:TestStatRemExpired_3", utils.TimePointer(time.Now().Add(time.Duration(time.Minute)))},
},
}
asrMetric := sq.SQMetrics[utils.MetaASR].(*StatASR)
if asr := asrMetric.GetFloat64Value(); asr != 66.66667 {
t.Errorf("received asrMetric: %v", asrMetric)
}
sq.remExpired()
if asr := asrMetric.GetFloat64Value(); asr != 100 {
t.Errorf("received asrMetric: %v", asrMetric)
} else if len(asrMetric.Events) != 1 {
t.Errorf("unexpected Events in asrMetric: %+v", asrMetric.Events)
}
if len(sq.SQItems) != 1 {
t.Errorf("Unexpected items: %+v", sq.SQItems)
}
}
func TestStatRemOnQueueLength(t *testing.T) {
sq = &StatQueue{
sqPrfl: &StatQueueProfile{
QueueLength: 2,
},
SQItems: []SQItem{
{"cgrates.org:TestStatRemExpired_1", nil},
},
}
sq.remOnQueueLength()
if len(sq.SQItems) != 1 {
t.Errorf("wrong items: %+v", sq.SQItems)
}
sq.SQItems = []SQItem{
{"cgrates.org:TestStatRemExpired_1", nil},
{"cgrates.org:TestStatRemExpired_2", nil},
}
sq.remOnQueueLength()
if len(sq.SQItems) != 1 {
t.Errorf("wrong items: %+v", sq.SQItems)
} else if sq.SQItems[0].EventID != "cgrates.org:TestStatRemExpired_2" {
t.Errorf("wrong item in SQItems: %+v", sq.SQItems[0])
}
sq.sqPrfl.QueueLength = -1
sq.SQItems = []SQItem{
{"cgrates.org:TestStatRemExpired_1", nil},
{"cgrates.org:TestStatRemExpired_2", nil},
{"cgrates.org:TestStatRemExpired_3", nil},
}
sq.remOnQueueLength()
if len(sq.SQItems) != 3 {
t.Errorf("wrong items: %+v", sq.SQItems)
}
}
func TestStatAddStatEvent(t *testing.T) {
sq = &StatQueue{
SQMetrics: map[string]StatMetric{
utils.MetaASR: &StatASR{
Answered: 1,
Count: 1,
Events: map[string]*StatWithCompress{
"cgrates.org:TestStatRemExpired_1": &StatWithCompress{Stat: 1},
},
},
},
}
asrMetric := sq.SQMetrics[utils.MetaASR].(*StatASR)
if asr := asrMetric.GetFloat64Value(); asr != 100 {
t.Errorf("received ASR: %v", asr)
}
ev1 := &utils.CGREvent{Tenant: "cgrates.org", ID: "TestStatAddStatEvent_1"}
sq.addStatEvent(ev1, nil)
if asr := asrMetric.GetFloat64Value(); asr != 50 {
t.Errorf("received ASR: %v", asr)
} else if asrMetric.Answered != 1 || asrMetric.Count != 2 {
t.Errorf("ASR: %v", asrMetric)
}
ev1.Event = map[string]interface{}{
utils.AnswerTime: time.Now()}
sq.addStatEvent(ev1, nil)
if asr := asrMetric.GetFloat64Value(); asr != 66.66667 {
t.Errorf("received ASR: %v", asr)
} else if asrMetric.Answered != 2 || asrMetric.Count != 3 {
t.Errorf("ASR: %v", asrMetric)
}
}