Add new unit tests on agents

This commit is contained in:
armirveliaj
2024-07-24 10:16:40 -04:00
committed by Dan Christian Bogos
parent a263e25858
commit 06ff5ef377
2 changed files with 126 additions and 0 deletions

View File

@@ -20,9 +20,117 @@ package agents
import (
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/sessions"
"github.com/cgrates/fsock"
)
func TestFAsSessionSClientIface(t *testing.T) {
_ = sessions.BiRPClient(new(FSsessions))
}
func TestFSsessionsReload(t *testing.T) {
cfg := &config.FsAgentCfg{}
sm := &FSsessions{
cfg: cfg,
conns: []*fsock.FSock{},
senderPools: []*fsock.FSockPool{},
}
sm.Reload()
if len(sm.conns) != len(cfg.EventSocketConns) {
t.Errorf("Expected conns length %d, but got %d", len(cfg.EventSocketConns), len(sm.conns))
}
if len(sm.senderPools) != len(cfg.EventSocketConns) {
t.Errorf("Expected senderPools length %d, but got %d", len(cfg.EventSocketConns), len(sm.senderPools))
}
for i, conn := range sm.conns {
if conn != nil {
t.Errorf("Expected conns[%d] to be nil, but got %v", i, conn)
}
}
for i, pool := range sm.senderPools {
if pool != nil {
t.Errorf("Expected senderPools[%d] to be nil, but got %v", i, pool)
}
}
}
func TestNewFSsessions(t *testing.T) {
cfg := &config.FsAgentCfg{}
timezone := "UTC"
connMgr := &engine.ConnManager{}
fsa := NewFSsessions(cfg, timezone, connMgr)
if fsa.cfg != cfg {
t.Errorf("Expected cfg to be %v, but got %v", cfg, fsa.cfg)
}
if fsa.timezone != timezone {
t.Errorf("Expected timezone to be %s, but got %s", timezone, fsa.timezone)
}
if fsa.connMgr != connMgr {
t.Errorf("Expected connMgr to be %v, but got %v", connMgr, fsa.connMgr)
}
if len(fsa.conns) != len(cfg.EventSocketConns) {
t.Errorf("Expected conns length %d, but got %d", len(cfg.EventSocketConns), len(fsa.conns))
}
for i, conn := range fsa.conns {
if conn != nil {
t.Errorf("Expected conns[%d] to be nil, but got %v", i, conn)
}
}
if len(fsa.senderPools) != len(cfg.EventSocketConns) {
t.Errorf("Expected senderPools length %d, but got %d", len(cfg.EventSocketConns), len(fsa.senderPools))
}
for i, pool := range fsa.senderPools {
if pool != nil {
t.Errorf("Expected senderPools[%d] to be nil, but got %v", i, pool)
}
}
}
func TestFSsessionsCreateHandlers(t *testing.T) {
cfgNoPark := &config.FsAgentCfg{
SubscribePark: false,
}
smNoPark := &FSsessions{
cfg: cfgNoPark,
}
handlersNoPark := smNoPark.createHandlers()
if len(handlersNoPark["CHANNEL_ANSWER"]) != 1 {
t.Errorf("Expected 1 handler for CHANNEL_ANSWER, but got %d", len(handlersNoPark["CHANNEL_ANSWER"]))
}
if len(handlersNoPark["CHANNEL_HANGUP_COMPLETE"]) != 1 {
t.Errorf("Expected 1 handler for CHANNEL_HANGUP_COMPLETE, but got %d", len(handlersNoPark["CHANNEL_HANGUP_COMPLETE"]))
}
if _, ok := handlersNoPark["CHANNEL_PARK"]; ok {
t.Errorf("CHANNEL_PARK handler should not be present when SubscribePark is false")
}
cfgWithPark := &config.FsAgentCfg{
SubscribePark: true,
}
smWithPark := &FSsessions{
cfg: cfgWithPark,
}
handlersWithPark := smWithPark.createHandlers()
if len(handlersWithPark["CHANNEL_ANSWER"]) != 1 {
t.Errorf("Expected 1 handler for CHANNEL_ANSWER, but got %d", len(handlersWithPark["CHANNEL_ANSWER"]))
}
if len(handlersWithPark["CHANNEL_HANGUP_COMPLETE"]) != 1 {
t.Errorf("Expected 1 handler for CHANNEL_HANGUP_COMPLETE, but got %d", len(handlersWithPark["CHANNEL_HANGUP_COMPLETE"]))
}
if len(handlersWithPark["CHANNEL_PARK"]) != 1 {
t.Errorf("Expected 1 handler for CHANNEL_PARK when SubscribePark is true, but got %d", len(handlersWithPark["CHANNEL_PARK"]))
}
}
func TestFSsessionsV1GetActiveSessionIDsErrorHandling(t *testing.T) {
sm := &FSsessions{}
var sessionIDs []*sessions.SessionID
err := sm.V1GetActiveSessionIDs("", &sessionIDs)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if len(sessionIDs) != 0 {
t.Errorf("Expected no session IDs, but got %d", len(sessionIDs))
}
}

View File

@@ -1235,3 +1235,21 @@ func TestFseventMissingParameter(t *testing.T) {
})
}
}
func TestV1TerminateSessionArgsErrorHandling(t *testing.T) {
tFSEvent := FSEvent{}
args := tFSEvent.V1TerminateSessionArgs()
if args == nil {
t.Errorf("Expected nil args due to error, got: %v", args)
}
}
func TestV1InitSessionArgsMissingVarCGRFlags(t *testing.T) {
tFSEvent := FSEvent{}
args := tFSEvent.V1InitSessionArgs()
if args == nil {
t.Errorf("Expected non-nil args, got nil")
} else if !args.InitSession {
t.Errorf("Expected InitSession to be true, got false")
}
}