From c40faf5ab3a06a9a33958a90b2170c86b3a1f3e4 Mon Sep 17 00:00:00 2001 From: Tripon Alexandru-Ionut Date: Mon, 13 May 2019 12:26:44 +0300 Subject: [PATCH] Added integration tests for AsActiveSessionsCount --- general_tests/session2_it_test.go | 175 ++++++++++++++++++++++++++++++ sessions/sessions_bench_test.go | 6 +- 2 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 general_tests/session2_it_test.go diff --git a/general_tests/session2_it_test.go b/general_tests/session2_it_test.go new file mode 100644 index 000000000..91b4c3b91 --- /dev/null +++ b/general_tests/session2_it_test.go @@ -0,0 +1,175 @@ +// +build integration + +/* +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 +*/ + +package general_tests + +import ( + "net/rpc" + "net/rpc/jsonrpc" + "path" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/sessions" + "github.com/cgrates/cgrates/utils" +) + +var ( + ses2CfgDir string + ses2CfgPath string + ses2Cfg *config.CGRConfig + ses2RPC *rpc.Client + + ses2Tests = []func(t *testing.T){ + testSesItLoadConfig, + testSesItResetDataDB, + testSesItResetStorDb, + testSesItStartEngine, + testSesItRPCConn, + testSesItLoadFromFolder, + testSesItInitSession, + testSesItAsActiveSessions, + testSesItStopCgrEngine, + } +) + +func TestSes2ItTutMongo(t *testing.T) { + ses2CfgDir = "tutmongo" + for _, stest := range ses2Tests { + t.Run("TestSesItTutMongo", stest) + } +} + +func TestSes2ItTutMysql(t *testing.T) { + ses2CfgDir = "tutmysql" + for _, stest := range ses2Tests { + t.Run("TestSesItTutMysql", stest) + } +} + +func testSesItLoadConfig(t *testing.T) { + ses2CfgPath = path.Join(*dataDir, "conf", "samples", ses2CfgDir) + if ses2Cfg, err = config.NewCGRConfigFromPath(ses2CfgPath); err != nil { + t.Error(err) + } +} + +func testSesItResetDataDB(t *testing.T) { + if err := engine.InitDataDb(ses2Cfg); err != nil { + t.Fatal(err) + } +} + +func testSesItResetStorDb(t *testing.T) { + if err := engine.InitStorDb(ses2Cfg); err != nil { + t.Fatal(err) + } +} + +func testSesItStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(ses2CfgPath, *waitRater); err != nil { + t.Fatal(err) + } +} + +func testSesItRPCConn(t *testing.T) { + var err error + ses2RPC, err = jsonrpc.Dial("tcp", ses2Cfg.ListenCfg().RPCJSONListen) + if err != nil { + t.Fatal(err) + } +} + +func testSesItLoadFromFolder(t *testing.T) { + var reply string + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")} + if err := ses2RPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { + t.Error(err) + } + time.Sleep(500 * time.Millisecond) +} + +func testSesItInitSession(t *testing.T) { + // Set balance + attrSetBalance := utils.AttrSetBalance{ + Tenant: "cgrates.org", + Account: "1001", + BalanceType: utils.VOICE, + BalanceID: utils.StringPointer("TestDynamicDebitBalance"), + Value: utils.Float64Pointer(float64(time.Hour)), + } + var reply string + if err := ses2RPC.Call("ApierV2.SetBalance", + attrSetBalance, &reply); err != nil { + t.Fatal(err) + } + + // Init session + initArgs := &sessions.V1InitSessionArgs{ + InitSession: true, + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: utils.UUIDSha1Prefix(), + Event: map[string]interface{}{ + utils.EVENT_NAME: "TEST_EVENT", + utils.OriginID: utils.UUIDSha1Prefix(), + utils.ToR: utils.VOICE, + utils.Category: "call", + utils.Tenant: "cgrates.org", + utils.Account: "1001", + utils.Subject: "1001", + utils.Destination: "1002", + utils.RequestType: utils.META_PREPAID, + utils.AnswerTime: time.Date(2016, time.January, 5, 18, 31, 05, 0, time.UTC), + }, + }, + } + var initRpl *sessions.V1InitSessionReply + if err := ses2RPC.Call(utils.SessionSv1InitiateSession, + initArgs, &initRpl); err != nil { + t.Fatal(err) + } + +} + +func testSesItAsActiveSessions(t *testing.T) { + var count int + if err := ses2RPC.Call(utils.SessionSv1GetActiveSessionsCount, utils.SessionFilter{ + Filters: []string{"*string:~Account:1001"}, + }, &count); err != nil { + t.Fatal(err) + } else if count != 1 { + t.Errorf("Expeced 1 session received %v session(s)", count) + } + if err := ses2RPC.Call(utils.SessionSv1GetActiveSessionsCount, utils.SessionFilter{ + Filters: []string{"*string:~Account:1002"}, + }, &count); err != nil { + t.Fatal(err) + } else if count != 0 { + t.Errorf("Expeced 0 session received %v session(s)", count) + } +} +func testSesItStopCgrEngine(t *testing.T) { + if err := engine.KillEngine(100); err != nil { + t.Error(err) + } +} diff --git a/sessions/sessions_bench_test.go b/sessions/sessions_bench_test.go index ec41fdcbe..7e5b37c2d 100644 --- a/sessions/sessions_bench_test.go +++ b/sessions/sessions_bench_test.go @@ -161,8 +161,8 @@ func sendInit() { func getCount() int { var count int - if err := sBenchRPC.Call(utils.SessionSv1GetActiveSessionsCount, map[string]interface{}{ - "Filters": map[string]string{utils.ToR: utils.VOICE}, + if err := sBenchRPC.Call(utils.SessionSv1GetActiveSessionsCount, utils.SessionFilter{ + Filters: []string{"*string:~ToR:*voice"}, }, &count); err != nil { log.Fatal(err) } @@ -183,7 +183,7 @@ func BenchmarkSendInitSession(b *testing.B) { } } -func BenchmarkSendInitSessionx10(b *testing.B) { +func benchmarkSendInitSessionx10(b *testing.B) { connOnce.Do(func() { startRPC() loadTP()