mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
302 lines
7.5 KiB
Go
302 lines
7.5 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 (
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cgrates/birpc/context"
|
|
"github.com/cgrates/cgrates/config"
|
|
"github.com/cgrates/cgrates/utils"
|
|
"github.com/cgrates/cron"
|
|
)
|
|
|
|
func TestStartTrendS(t *testing.T) {
|
|
cfg := config.NewDefaultCGRConfig()
|
|
cfg.TrendSCfg().Enabled = true
|
|
dataDB := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
|
|
dm := NewDataManager(dataDB, cfg.CacheCfg(), nil)
|
|
tS := NewTrendS(dm, nil, nil, cfg)
|
|
dm.SetTrendProfile(&TrendProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "ID1",
|
|
Schedule: "@every 1s",
|
|
StatID: "stat1",
|
|
Metrics: []string{"metric1", "metric2"},
|
|
TTL: time.Minute,
|
|
QueueLength: 10,
|
|
MinItems: 5,
|
|
CorrelationType: "*last",
|
|
Tolerance: 0.1,
|
|
Stored: true,
|
|
ThresholdIDs: []string{"threshold1"},
|
|
})
|
|
if err := tS.StartTrendS(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestStoreTrend(t *testing.T) {
|
|
cfg := config.NewDefaultCGRConfig()
|
|
dataDB := NewInternalDB(nil, nil, true, nil)
|
|
dm := NewDataManager(dataDB, cfg.CacheCfg(), nil)
|
|
tS := &TrendS{
|
|
cgrcfg: cfg,
|
|
dm: dm,
|
|
storedTrends: make(utils.StringSet),
|
|
}
|
|
|
|
trendProfile := &TrendProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "trendID1",
|
|
Schedule: "@every 1s",
|
|
StatID: "stat1",
|
|
Metrics: []string{"metric1", "metric2"},
|
|
TTL: time.Minute,
|
|
QueueLength: 100,
|
|
MinItems: 10,
|
|
CorrelationType: "average",
|
|
Tolerance: 0.05,
|
|
Stored: true,
|
|
ThresholdIDs: []string{"threshold1"},
|
|
}
|
|
|
|
trend := &Trend{
|
|
Tenant: "cgrates.org",
|
|
ID: "trendID1",
|
|
tPrfl: trendProfile,
|
|
Metrics: make(map[time.Time]map[string]*MetricWithTrend),
|
|
RunTimes: []time.Time{},
|
|
mLast: make(map[string]time.Time),
|
|
mCounts: make(map[string]int),
|
|
mTotals: make(map[string]float64),
|
|
}
|
|
|
|
cfg.TrendSCfg().StoreInterval = 0
|
|
if err := tS.storeTrend(trend); err != nil {
|
|
t.Errorf("Expected no error when StoreInterval is 0, but got: %v", err)
|
|
}
|
|
if len(tS.storedTrends) != 0 {
|
|
t.Error("Expected storedTrends to be empty when StoreInterval is 0")
|
|
}
|
|
cfg.TrendSCfg().StoreInterval = -1
|
|
if err := tS.storeTrend(trend); err != nil {
|
|
t.Errorf("Expected no error when StoreInterval is -1, but got: %v", err)
|
|
}
|
|
cfg.TrendSCfg().StoreInterval = time.Second
|
|
if err := tS.storeTrend(trend); err != nil {
|
|
t.Errorf("Expected no error when StoreInterval is positive, but got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestStoreTrends(t *testing.T) {
|
|
cfg := config.NewDefaultCGRConfig()
|
|
dataDB := NewInternalDB(nil, nil, true, nil)
|
|
dm := NewDataManager(dataDB, cfg.CacheCfg(), nil)
|
|
|
|
tS := &TrendS{
|
|
cgrcfg: cfg,
|
|
dm: dm,
|
|
storedTrends: make(utils.StringSet),
|
|
}
|
|
|
|
trendID := "trendID1"
|
|
tS.storedTrends.Add(trendID)
|
|
|
|
tS.storeTrends()
|
|
|
|
if len(tS.storedTrends) == 0 {
|
|
t.Error("Expected storedTrends to not be empty")
|
|
}
|
|
}
|
|
|
|
func TestV1GetTrendSummary(t *testing.T) {
|
|
cfg := config.NewDefaultCGRConfig()
|
|
dataDB := NewInternalDB(nil, nil, true, nil)
|
|
dm := NewDataManager(dataDB, cfg.CacheCfg(), nil)
|
|
|
|
tS := &TrendS{
|
|
dm: dm,
|
|
}
|
|
|
|
trendID := "trendID1"
|
|
tenantID := "cgrates.org"
|
|
trend := &Trend{
|
|
Tenant: tenantID,
|
|
ID: trendID,
|
|
}
|
|
if err := dm.SetTrend(trend); err != nil {
|
|
t.Fatalf("Failed to set trend in data manager: %v", err)
|
|
}
|
|
|
|
arg := utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{
|
|
Tenant: tenantID,
|
|
ID: trendID,
|
|
},
|
|
APIOpts: make(map[string]any),
|
|
}
|
|
var reply TrendSummary
|
|
|
|
err := tS.V1GetTrendSummary(nil, arg, &reply)
|
|
|
|
if err != nil {
|
|
t.Errorf("Expected no error, but got: %v", err)
|
|
}
|
|
|
|
if reply.Tenant != tenantID || reply.ID != trendID {
|
|
t.Errorf("Expected reply to have Tenant: %s and ID: %s, but got Tenant: %s and ID: %s",
|
|
tenantID, trendID, reply.Tenant, reply.ID)
|
|
}
|
|
}
|
|
|
|
func TestTrendProfileTenantID(t *testing.T) {
|
|
profile := &TrendProfile{
|
|
Tenant: "cgrates.org",
|
|
ID: "1",
|
|
}
|
|
result := profile.TenantID()
|
|
expected := "cgrates.org:1"
|
|
if result != expected {
|
|
t.Errorf("TenantID() = %v; want %v", result, expected)
|
|
}
|
|
}
|
|
|
|
func TestTrendTenantID(t *testing.T) {
|
|
trend := &Trend{
|
|
Tenant: "cgrates.org",
|
|
ID: "1",
|
|
}
|
|
result := trend.TenantID()
|
|
expected := "cgrates.org:1"
|
|
if result != expected {
|
|
t.Errorf("TenantID() = %v; want %v", result, expected)
|
|
}
|
|
}
|
|
|
|
func TestNewTrendS(t *testing.T) {
|
|
dm := &DataManager{}
|
|
connMgr := &ConnManager{}
|
|
filterS := &FilterS{}
|
|
cgrcfg := &config.CGRConfig{}
|
|
|
|
trendS := NewTrendS(dm, connMgr, filterS, cgrcfg)
|
|
|
|
if trendS == nil {
|
|
t.Errorf("Expected NewTrendS to return a non-nil instance")
|
|
}
|
|
if trendS.dm != dm {
|
|
t.Errorf("Expected DataManager to be set correctly, got %v, want %v", trendS.dm, dm)
|
|
}
|
|
if trendS.connMgr != connMgr {
|
|
t.Errorf("Expected ConnManager to be set correctly, got %v, want %v", trendS.connMgr, connMgr)
|
|
}
|
|
if trendS.filterS != filterS {
|
|
t.Errorf("Expected FilterS to be set correctly, got %v, want %v", trendS.filterS, filterS)
|
|
}
|
|
if trendS.cgrcfg != cgrcfg {
|
|
t.Errorf("Expected CGRConfig to be set correctly, got %v, want %v", trendS.cgrcfg, cgrcfg)
|
|
}
|
|
|
|
if trendS.trendStop == nil {
|
|
t.Errorf("Expected loopStopped to be initialized, but got nil")
|
|
}
|
|
if trendS.crnTQsMux == nil {
|
|
t.Errorf("Expected crnTQsMux to be initialized, but got nil")
|
|
}
|
|
if trendS.crnTQs == nil {
|
|
t.Errorf("Expected crnTQs to be initialized, but got nil")
|
|
} else if len(trendS.crnTQs) != 0 {
|
|
t.Errorf("Expected crnTQs to be empty, but got length %d", len(trendS.crnTQs))
|
|
}
|
|
|
|
}
|
|
|
|
func TestProcessEEsWithError(t *testing.T) {
|
|
|
|
trend := &Trend{
|
|
ID: "ID",
|
|
Tenant: "cgrates.org",
|
|
}
|
|
|
|
mockConnMgr := &ConnManager{}
|
|
trendService := &TrendS{
|
|
cgrcfg: &config.CGRConfig{},
|
|
connMgr: mockConnMgr,
|
|
}
|
|
|
|
err := trendService.processEEs(trend)
|
|
if err != nil || errors.Is(err, utils.ErrPartiallyExecuted) {
|
|
t.Errorf("Expected error %v, got %v", utils.ErrPartiallyExecuted, err)
|
|
}
|
|
|
|
}
|
|
|
|
func TestV1ScheduleQueriesInvalidTrendID(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tS := &TrendS{
|
|
crn: cron.New(),
|
|
crnTQs: make(map[string]map[string]cron.EntryID),
|
|
crnTQsMux: &sync.RWMutex{},
|
|
}
|
|
|
|
args := &utils.ArgScheduleTrendQueries{
|
|
TenantIDWithAPIOpts: utils.TenantIDWithAPIOpts{
|
|
TenantID: &utils.TenantID{
|
|
Tenant: "cgrates.org",
|
|
ID: "ID",
|
|
},
|
|
APIOpts: make(map[string]any),
|
|
},
|
|
TrendIDs: []string{"invalidID"},
|
|
}
|
|
|
|
var scheduled int
|
|
err := tS.V1ScheduleQueries(ctx, args, &scheduled)
|
|
|
|
if err == nil {
|
|
t.Errorf("expected an error but got none")
|
|
}
|
|
|
|
if scheduled != 0 {
|
|
t.Errorf("expected scheduled to be 0 but got %d", scheduled)
|
|
}
|
|
}
|
|
|
|
func TestProcessThresholds_OptsInitialization(t *testing.T) {
|
|
tS := &TrendS{}
|
|
|
|
trnd := &Trend{
|
|
Tenant: "cgrates.org",
|
|
ID: "ID",
|
|
}
|
|
|
|
err := tS.processThresholds(trnd)
|
|
|
|
if err != nil {
|
|
t.Errorf("expected no error but got: %v", err)
|
|
}
|
|
|
|
}
|