From 13bdbabee8b338034320ca3cc138a635fe5d028f Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 22 Nov 2019 11:21:47 +0200 Subject: [PATCH] Active scheduler on internal and add test for replicaton the account --- apier/v1/dm_remote_it_test.go | 155 ++++++++++++++++++ .../remote_replication/internal/cgrates.json | 13 +- 2 files changed, 164 insertions(+), 4 deletions(-) diff --git a/apier/v1/dm_remote_it_test.go b/apier/v1/dm_remote_it_test.go index e6ceeec72..70763f01e 100644 --- a/apier/v1/dm_remote_it_test.go +++ b/apier/v1/dm_remote_it_test.go @@ -74,6 +74,8 @@ var sTestsInternalRemoteIT = []func(t *testing.T){ testInternalRemoteITGetReverseDestination, testInternalReplicationSetThreshold, testInternalMatchThreshold, + testInternalAccountBalanceOperations, + testInternalSetAccount, testInternalRemoteITKillEngine, } @@ -655,6 +657,7 @@ func testInternalReplicationSetThreshold(t *testing.T) { expectedIDX = []string{ "*string:~Account:1001:THD_ACNT_1001", "*string:~Account:1001:THD_Replication", + "*string:~Account:1002:THD_ACNT_1002", "*string:~CustomField:CustomValue:THD_Replication", } // verify index on internal @@ -760,6 +763,158 @@ func testInternalMatchThreshold(t *testing.T) { } +func testInternalAccountBalanceOperations(t *testing.T) { + var reply string + attrs := &utils.AttrSetBalance{ + Tenant: "cgrates.org", + Account: "testAccount1", + BalanceID: utils.StringPointer("testAccSetBalance"), + BalanceType: utils.MONETARY, + Value: utils.Float64Pointer(17.4), + } + if err := internalRPC.Call(utils.ApierV1SetBalance, attrs, &reply); err != nil { + t.Error(err) + } + + var acnt *engine.Account + attrAcc := &utils.AttrGetAccount{ + Tenant: "cgrates.org", + Account: "testAccount1", + } + // verify account on engineOne + if err := engineOneRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Error(err) + } else if len(acnt.BalanceMap[utils.MONETARY]) != 1 { + t.Errorf("Expecting: %+v, received: %+v", + 1, len(acnt.BalanceMap[utils.MONETARY])) + } else if val := acnt.BalanceMap[utils.MONETARY].GetTotalValue(); val != 17.4 { + t.Errorf("Expecting: %+v, received: %+v", + 17.4, val) + } + + // verify account on engineTwo + if err := engineTwoRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Error(err) + } else if len(acnt.BalanceMap[utils.MONETARY]) != 1 { + t.Errorf("Expecting: %+v, received: %+v", + 1, len(acnt.BalanceMap[utils.MONETARY])) + } else if val := acnt.BalanceMap[utils.MONETARY].GetTotalValue(); val != 17.4 { + t.Errorf("Expecting: %+v, received: %+v", + 17.4, val) + } + + // debit balance on internal and the account should be replicated to other engines + if err := internalRPC.Call(utils.ApierV1DebitBalance, &AttrAddBalance{ + Tenant: "cgrates.org", + Account: "testAccount1", + BalanceType: utils.MONETARY, + Value: 3.62, + }, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Received: %s", reply) + } + + // verify debited account on engineOne + if err := engineOneRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Error(err) + } else if len(acnt.BalanceMap[utils.MONETARY]) != 1 { + t.Errorf("Expecting: %+v, received: %+v", + 1, len(acnt.BalanceMap[utils.MONETARY])) + } else if val := acnt.BalanceMap[utils.MONETARY].GetTotalValue(); val != 13.78 { + t.Errorf("Expecting: %+v, received: %+v", + 13.78, val) + } + + // verify debited account on engineTwo + if err := engineTwoRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Error(err) + } else if len(acnt.BalanceMap[utils.MONETARY]) != 1 { + t.Errorf("Expecting: %+v, received: %+v", + 1, len(acnt.BalanceMap[utils.MONETARY])) + } else if val := acnt.BalanceMap[utils.MONETARY].GetTotalValue(); val != 13.78 { + t.Errorf("Expecting: %+v, received: %+v", + 13.78, val) + } + + addBal := &AttrAddBalance{ + Tenant: "cgrates.org", + Account: "testAccount1", + BalanceType: utils.MONETARY, + Value: 12.765, + } + // add balance for the account on internal and this should be replicated to other engines + if err := internalRPC.Call(utils.ApierV1AddBalance, addBal, &reply); err != nil { + t.Error(err) + } + + // verify account on engineOne + if err := engineOneRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Error(err) + } else if len(acnt.BalanceMap[utils.MONETARY]) != 1 { + t.Errorf("Expecting: %+v, received: %+v", + 1, len(acnt.BalanceMap[utils.MONETARY])) + } else if val := acnt.BalanceMap[utils.MONETARY].GetTotalValue(); val != 26.545 { + t.Errorf("Expecting: %+v, received: %+v", + 26.545, val) + } + + // verify account on engineTwo + if err := engineTwoRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Error(err) + } else if len(acnt.BalanceMap[utils.MONETARY]) != 1 { + t.Errorf("Expecting: %+v, received: %+v", + 1, len(acnt.BalanceMap[utils.MONETARY])) + } else if val := acnt.BalanceMap[utils.MONETARY].GetTotalValue(); val != 26.545 { + t.Errorf("Expecting: %+v, received: %+v", + 26.545, val) + } + +} + +func testInternalSetAccount(t *testing.T) { + var reply string + + if err := internalRPC.Call(utils.ApierV1SetAccount, + utils.AttrSetAccount{ + Tenant: "cgrates.org", + Account: "testSetAccount", + ActionPlanId: "AP_PACKAGE_10", + ReloadScheduler: true, + }, &reply); err != nil { + t.Error(err) + } + // give some time to scheduler to execute the action + time.Sleep(50 * time.Millisecond) + + var acnt *engine.Account + attrAcc := &utils.AttrGetAccount{ + Tenant: "cgrates.org", + Account: "testSetAccount", + } + // verify account on engineOne + if err := engineOneRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Error(err) + } else if len(acnt.BalanceMap[utils.MONETARY]) != 1 { + t.Errorf("Expecting: %+v, received: %+v", + 1, len(acnt.BalanceMap[utils.MONETARY])) + } else if val := acnt.BalanceMap[utils.MONETARY].GetTotalValue(); val != 10 { + t.Errorf("Expecting: %+v, received: %+v", + 10, val) + } + + // verify account on engineTwo + if err := engineTwoRPC.Call(utils.ApierV2GetAccount, attrAcc, &acnt); err != nil { + t.Error(err) + } else if len(acnt.BalanceMap[utils.MONETARY]) != 1 { + t.Errorf("Expecting: %+v, received: %+v", + 1, len(acnt.BalanceMap[utils.MONETARY])) + } else if val := acnt.BalanceMap[utils.MONETARY].GetTotalValue(); val != 10 { + t.Errorf("Expecting: %+v, received: %+v", + 10, val) + } +} + func testInternalRemoteITKillEngine(t *testing.T) { if err := engine.KillEngine(100); err != nil { t.Error(err) diff --git a/data/conf/samples/remote_replication/internal/cgrates.json b/data/conf/samples/remote_replication/internal/cgrates.json index 54e8a32ab..07778b64b 100644 --- a/data/conf/samples/remote_replication/internal/cgrates.json +++ b/data/conf/samples/remote_replication/internal/cgrates.json @@ -75,8 +75,13 @@ ], }, - "stats": { - "enabled": false, - "store_interval": "-1", - } +"stats": { + "enabled": false, + "store_interval": "-1", +}, + +"scheduler": { + "enabled": true, +}, + }