From 171fb5512e3160aee53fb2f7b42e3449a94358db Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Fri, 9 Jul 2021 17:48:25 +0300 Subject: [PATCH] Cover funcs from engine/thresholds.go --- engine/thresholds_test.go | 309 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) diff --git a/engine/thresholds_test.go b/engine/thresholds_test.go index 3d174bb0f..98796a6b4 100644 --- a/engine/thresholds_test.go +++ b/engine/thresholds_test.go @@ -18,7 +18,11 @@ along with this program. If not, see package engine import ( + "bytes" + "log" + "os" "reflect" + "strings" "testing" "time" @@ -1176,3 +1180,308 @@ func TestThresholdsUpdateThreshold(t *testing.T) { t.Fatal(err) } } + +func TestThresholdsProcessEventAccountUpdateErrPartExec(t *testing.T) { + utils.Logger.SetLogLevel(4) + utils.Logger.SetSyslog(nil) + + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { + log.SetOutput(os.Stderr) + }() + + thPrf := &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TH1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + MinHits: 2, + MaxHits: 5, + Weight: 10, + ActionIDs: []string{"actPrf"}, + } + th := &Threshold{ + Tenant: "cgrates.org", + ID: "TH1", + Hits: 2, + tPrfl: thPrf, + } + + args := &ThresholdsArgsProcessEvent{ + ThresholdIDs: []string{"TH1"}, + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "ThresholdProcessEvent", + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + APIOpts: map[string]interface{}{ + utils.MetaEventType: utils.AccountUpdate, + }, + }, + } + expLog := `[WARNING] failed executing actions: actPrf, error: NOT_FOUND` + if err := th.ProcessEvent(args, dm); err == nil || + err != utils.ErrPartiallyExecuted { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", utils.ErrPartiallyExecuted, err) + } + + if rcvLog := buf.String(); !strings.Contains(rcvLog, expLog) { + t.Errorf("expected log <%+v> \nto be included in: <%+v>", expLog, rcvLog) + } + utils.Logger.SetLogLevel(0) +} + +func TestThresholdsProcessEventAsyncExecErr(t *testing.T) { + utils.Logger.SetLogLevel(4) + utils.Logger.SetSyslog(nil) + + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { + log.SetOutput(os.Stderr) + }() + + thPrf := &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TH1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + MinHits: 2, + MaxHits: 5, + Weight: 10, + ActionIDs: []string{"actPrf"}, + Async: true, + } + th := &Threshold{ + Tenant: "cgrates.org", + ID: "TH1", + Hits: 2, + tPrfl: thPrf, + } + + args := &ThresholdsArgsProcessEvent{ + ThresholdIDs: []string{"TH1"}, + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "ThresholdProcessEvent", + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + }, + } + expLog := `[WARNING] failed executing actions: actPrf, error: NOT_FOUND` + if err := th.ProcessEvent(args, dm); err != nil { + t.Error(err) + } + time.Sleep(10 * time.Millisecond) + if rcvLog := buf.String(); !strings.Contains(rcvLog, expLog) { + t.Errorf("expected log <%+v> \nto be included in: <%+v>", expLog, rcvLog) + } + + utils.Logger.SetLogLevel(0) +} + +func TestThresholdsProcessEvent3(t *testing.T) { + thPrf := &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TH1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + MinHits: 3, + MaxHits: 5, + Weight: 10, + ActionIDs: []string{"actPrf"}, + } + th := &Threshold{ + Tenant: "cgrates.org", + ID: "TH1", + Hits: 2, + tPrfl: thPrf, + } + + args := &ThresholdsArgsProcessEvent{ + ThresholdIDs: []string{"TH1"}, + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "ThresholdProcessEvent", + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + APIOpts: map[string]interface{}{ + utils.MetaEventType: utils.AccountUpdate, + }, + }, + } + if err := th.ProcessEvent(args, dm); err != nil { + t.Error(err) + } +} + +func TestThresholdsShutdown(t *testing.T) { + utils.Logger.SetLogLevel(6) + utils.Logger.SetSyslog(nil) + + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { + log.SetOutput(os.Stderr) + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + tS := NewThresholdService(dm, cfg, nil) + + expLog1 := `[INFO] shutdown initialized` + expLog2 := `[INFO] shutdown complete` + tS.Shutdown() + + if rcvLog := buf.String(); !strings.Contains(rcvLog, expLog1) || + !strings.Contains(rcvLog, expLog2) { + t.Errorf("expected logs <%+v> and <%+v> \n to be included in <%+v>", + expLog1, expLog2, rcvLog) + } + utils.Logger.SetLogLevel(0) +} + +func TestThresholdsStoreThresholdsOK(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + tS := NewThresholdService(dm, cfg, nil) + + value := &Threshold{ + dirty: utils.BoolPointer(true), + Tenant: "cgrates.org", + ID: "TH1", + Hits: 2, + } + + Cache.SetWithoutReplicate(utils.CacheThresholds, "TH1", value, nil, true, + utils.NonTransactional) + tS.storedTdIDs.Add("TH1") + exp := &Threshold{ + dirty: utils.BoolPointer(false), + Tenant: "cgrates.org", + ID: "TH1", + Hits: 2, + } + tS.storeThresholds() + + if rcv, err := tS.dm.GetThreshold("cgrates.org", "TH1", true, false, + utils.NonTransactional); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", + utils.ToJSON(exp), utils.ToJSON(rcv)) + } + + Cache.Remove(utils.CacheThresholds, "TH1", true, utils.NonTransactional) +} + +func TestThresholdsStoreThresholdsStoreThErr(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + utils.Logger.SetLogLevel(4) + utils.Logger.SetSyslog(nil) + + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { + log.SetOutput(os.Stderr) + }() + + cfg := config.NewDefaultCGRConfig() + tS := NewThresholdService(nil, cfg, nil) + + value := &Threshold{ + dirty: utils.BoolPointer(true), + Tenant: "cgrates.org", + ID: "TH1", + Hits: 2, + } + + Cache.SetWithoutReplicate(utils.CacheThresholds, "TH1", value, nil, true, + utils.NonTransactional) + tS.storedTdIDs.Add("TH1") + exp := utils.StringSet{ + "TH1": struct{}{}, + } + expLog := `[WARNING] failed saving Threshold with tenant: cgrates.org and ID: TH1, error: NO_DATABASE_CONNECTION` + tS.storeThresholds() + + if !reflect.DeepEqual(tS.storedTdIDs, exp) { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", exp, tS.storedTdIDs) + } + if rcvLog := buf.String(); !strings.Contains(rcvLog, expLog) { + t.Errorf("expected log <%+v>\n to be in included in: <%+v>", expLog, rcvLog) + } + + utils.Logger.SetLogLevel(0) + Cache.Remove(utils.CacheThresholds, "TH1", true, utils.NonTransactional) +} + +func TestThresholdsStoreThresholdsCacheGetErr(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + utils.Logger.SetLogLevel(4) + utils.Logger.SetSyslog(nil) + + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { + log.SetOutput(os.Stderr) + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + tS := NewThresholdService(dm, cfg, nil) + + value := &Threshold{ + dirty: utils.BoolPointer(true), + Tenant: "cgrates.org", + ID: "TH1", + Hits: 2, + } + + Cache.SetWithoutReplicate(utils.CacheThresholds, "TH2", value, nil, true, + utils.NonTransactional) + tS.storedTdIDs.Add("TH1") + expLog := `[WARNING] failed retrieving from cache treshold with ID: TH1` + tS.storeThresholds() + + if rcvLog := buf.String(); !strings.Contains(rcvLog, expLog) { + t.Errorf("expected <%+v> \nto be included in: <%+v>", expLog, rcvLog) + } + + utils.Logger.SetLogLevel(0) + Cache.Remove(utils.CacheThresholds, "TH2", true, utils.NonTransactional) +} + +func TestThresholdsStoreThresholdNilDirtyField(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + tS := NewThresholdService(dm, cfg, nil) + + th := &Threshold{ + Tenant: "cgrates.org", + ID: "TH1", + Hits: 2, + } + + if err := tS.StoreThreshold(th); err != nil { + t.Error(err) + } +}