Skip to content

Commit

Permalink
Revise tests
Browse files Browse the repository at this point in the history
-ensure failure messages are useful
-optimize tests with needlessly large sleep times
-enforce a stricter margin for error
  • Loading branch information
ionutboangiu authored and danbogos committed Apr 12, 2024
1 parent 56360e4 commit e025a0d
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 117 deletions.
39 changes: 19 additions & 20 deletions apier/v1/apier2_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,40 +404,39 @@ func testAPIerSetActionPlanDfltTime(t *testing.T) {
} else if reply1 != utils.OK {
t.Errorf("Calling APIerSv1.SetActionPlan received: %s", reply1)
}
var rply []*scheduler.ScheduledAction
refTime := time.Now()
var rply []*scheduler.ScheduledAction
if err := apierRPC.Call(context.Background(), utils.APIerSv1GetScheduledActions,
scheduler.ArgsGetScheduledActions{}, &rply); err != nil {
t.Fatal(err)
}

for _, schedAct := range rply {
got := schedAct.NextRunTime
switch schedAct.ActionPlanID {
case "AP_WEEKLY":
t1 := refTime.AddDate(0, 0, 7)
if schedAct.NextRunTime.Before(t1.Add(-time.Second)) ||
schedAct.NextRunTime.After(t1.Add(2*time.Second)) {
t.Errorf("Expected the nextRuntime to be after 1 week,but received: <%+v>", utils.ToJSON(schedAct))
want := refTime.AddDate(0, 0, 7) // +1 week
if diff := want.Sub(got); diff < 0 || diff > 2*time.Second {
t.Errorf("%s scheduled date = %v, want %v (diff %v, margin 2s)",
schedAct.ActionPlanID, got.Format(time.StampMilli), want.Format(time.StampMilli), diff)
}
case "AP_DAILY":
t1 := refTime.AddDate(0, 0, 1)
if schedAct.NextRunTime.Before(t1.Add(-time.Second)) ||
schedAct.NextRunTime.After(t1.Add(2*time.Second)) {
t.Errorf("Expected the nextRuntime to be after 1 day,but received: <%+v>", utils.ToJSON(schedAct))
want := refTime.AddDate(0, 0, 1) // +1 day
if diff := want.Sub(got); diff < 0 || diff > 2*time.Second {
t.Errorf("%s scheduled date = %v, want %v (diff %v, margin 2s)",
schedAct.ActionPlanID, got.Format(time.StampMilli), want.Format(time.StampMilli), diff)
}
case "AP_HOURLY":
if schedAct.NextRunTime.Before(refTime.Add(59*time.Minute+59*time.Second)) ||
schedAct.NextRunTime.After(refTime.Add(time.Hour+2*time.Second)) {
t.Errorf("Expected the nextRuntime to be after 1 hour,but received: <%+v>", utils.ToJSON(schedAct))
want := refTime.Add(time.Hour) // +1h
if diff := want.Sub(got); diff < 0 || diff > 2*time.Second {
t.Errorf("%s scheduled date = %v, want %v (diff %v, margin 2s)",
schedAct.ActionPlanID, got.Format(time.StampMilli), want.Format(time.StampMilli), diff)
}
case "AP_MONTHLY":
// *monthly needs to mach exactly the day
expected := refTime.AddDate(0, 1, 0)
expected = time.Date(expected.Year(), expected.Month(), refTime.Day(), refTime.Hour(),
refTime.Minute(), refTime.Second(), 0, schedAct.NextRunTime.Location())
if schedAct.NextRunTime.Before(expected.Add(-time.Second)) ||
schedAct.NextRunTime.After(expected.Add(2*time.Second)) {
t.Errorf("Expected the nextRuntime to be after 1 month,but received: <%+v>", utils.ToJSON(schedAct))
// *monthly needs to match exactly the day
want := refTime.AddDate(0, 1, 0) // +1 month
if diff := want.Sub(got); diff < 0 || diff > 2*time.Second {
t.Errorf("%s scheduled date = %v, want %v (diff %v, margin 2s)",
schedAct.ActionPlanID, got.Format(time.StampMilli), want.Format(time.StampMilli), diff)
}
}
}
Expand Down
56 changes: 21 additions & 35 deletions engine/caches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,49 +283,35 @@ func TestCacheSV1GetItem(t *testing.T) {
}

func TestCacheSV1GetItemExpiryTime(t *testing.T) {
tmp := Cache

defer func() {
Cache = tmp
config.SetCgrConfig(config.NewDefaultCGRConfig())
}()
Cache.Clear(nil)
args := &utils.ArgsGetCacheItemWithAPIOpts{
ArgsGetCacheItem: utils.ArgsGetCacheItem{
CacheID: "cacheID",
ItemID: "itemID",
},
}
cacheID := "testCache"
itemID := "testItem"
tscache := ltcache.NewTransCache(
map[string]*ltcache.CacheConfig{
"cacheID": {
MaxItems: 3,
TTL: time.Minute * 30,
StaticTTL: false,
OnEvicted: func(itmID string, value any) {

},
cacheID: {
MaxItems: -1,
TTL: 30 * time.Minute,
},
})
tscache.Set("cacheID", "itemID", "value", []string{}, true, "tId")

cfg := config.NewDefaultCGRConfig()
db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
dm := NewDataManager(db, cfg.CacheCfg(), nil)
chS := &CacheS{
cfg: cfg,
dm: dm,
tCache: tscache,
}
reply := now
loc, _ := time.LoadLocation("EST")
exp := now.Add(30 * time.Minute).In(loc).Minute()
if err := chS.V1GetItemExpiryTime(context.Background(), args, &reply); err != nil {
t.Error(err)
} else if reply.Minute() != exp {
t.Errorf("expected %+v,received %+v", exp, reply)
}
chS.tCache.Set(cacheID, itemID, "value", []string{}, true, "")
want := time.Now().Add(30 * time.Minute)

var got time.Time
if err := chS.V1GetItemExpiryTime(context.Background(),
&utils.ArgsGetCacheItemWithAPIOpts{
ArgsGetCacheItem: utils.ArgsGetCacheItem{
CacheID: cacheID,
ItemID: itemID,
},
}, &got); err != nil {
t.Fatalf("V1GetItemExpiryTime(%q,%q): got unexpected err=%v", cacheID, itemID, err)
}
if diff := want.Sub(got); diff < 0 || diff > time.Millisecond {
t.Errorf("V1GetItemExpiryTime(%q,%q) = %v, want %v (diff %v, margin 1ms)",
cacheID, itemID, got.Format(time.StampMicro), want.Format(time.StampMicro), diff)
}
}

func TestCacheSV1RemoveItem(t *testing.T) {
Expand Down
47 changes: 47 additions & 0 deletions engine/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,53 @@ func TestStatQueueStartLoop(t *testing.T) {
}
}

func TestStatQueueRunBackupStop(t *testing.T) {
cfg := config.NewDefaultCGRConfig()
cfg.StatSCfg().StoreInterval = 5 * time.Millisecond
data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
dm := NewDataManager(data, cfg.CacheCfg(), nil)
tnt := "cgrates.org"
sqID := "testSQ"
sqS := &StatService{
dm: dm,
storedStatQueues: utils.StringSet{
sqID: struct{}{},
},
cgrcfg: cfg,
loopStopped: make(chan struct{}, 1),
stopBackup: make(chan struct{}),
}
value := &StatQueue{
dirty: utils.BoolPointer(true),
Tenant: tnt,
ID: sqID,
}
Cache.SetWithoutReplicate(utils.CacheStatQueues, sqID, value, nil, true, "")

// Backup loop checks for the state of the stopBackup
// channel after storing the stat queue. Channel can be
// safely closed beforehand.
close(sqS.stopBackup)
sqS.runBackup()

want := &StatQueue{
dirty: utils.BoolPointer(false),
Tenant: tnt,
ID: sqID,
}
if got, err := sqS.dm.GetStatQueue(tnt, sqID, true, false, utils.NonTransactional); err != nil {
t.Errorf("dm.GetStatQueue(%q,%q): got unexpected err=%v", tnt, sqID, err)
} else if !reflect.DeepEqual(got, want) {
t.Errorf("dm.GetStatQueue(%q,%q) = %v, want %v", tnt, sqID, got, want)
}

select {
case <-sqS.loopStopped:
case <-time.After(time.Second):
t.Error("timed out waiting for loop to stop")
}
}

func TestStatQueueShutdown(t *testing.T) {
utils.Logger.SetLogLevel(6)
utils.Logger.SetSyslog(nil)
Expand Down
33 changes: 15 additions & 18 deletions engine/thresholds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1753,42 +1753,39 @@ func TestThresholdsRunBackupStop(t *testing.T) {
cfg.ThresholdSCfg().StoreInterval = 5 * time.Millisecond
data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
dm := NewDataManager(data, cfg.CacheCfg(), nil)
tnt := "cgrates.org"
thID := "Th1"
tS := &ThresholdService{
dm: dm,
storedTdIDs: utils.StringSet{
"Th1": struct{}{},
thID: struct{}{},
},
cgrcfg: cfg,
loopStopped: make(chan struct{}, 1),
stopBackup: make(chan struct{}),
}

value := &Threshold{
dirty: utils.BoolPointer(true),
Tenant: "cgrates.org",
ID: "Th1",
}

Cache.SetWithoutReplicate(utils.CacheThresholds, "Th1", value, nil, true,
utils.NonTransactional)

exp := &Threshold{
dirty: utils.BoolPointer(false),
Tenant: "cgrates.org",
ID: "Th1",
Tenant: tnt,
ID: thID,
}
Cache.SetWithoutReplicate(utils.CacheThresholds, thID, value, nil, true, "")

// Backup loop checks for the state of the stopBackup
// channel after storing the threshold. Channel can be
// safely closed beforehand.
close(tS.stopBackup)
tS.runBackup()

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("threshold: want %+v, got %+v", exp, rcv)
want := &Threshold{
dirty: utils.BoolPointer(false),
Tenant: tnt,
ID: thID,
}
if got, err := tS.dm.GetThreshold(tnt, thID, true, false, utils.NonTransactional); err != nil {
t.Errorf("dm.GetThreshold(%q,%q): got unexpected err=%v", tnt, thID, err)
} else if !reflect.DeepEqual(got, want) {
t.Errorf("dm.GetThreshold(%q,%q) = %v, want %v", tnt, thID, got, want)
}

select {
Expand Down
32 changes: 15 additions & 17 deletions engine/z_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5935,41 +5935,39 @@ func TestResourcesRunBackupStop(t *testing.T) {
cfg.ResourceSCfg().StoreInterval = 5 * time.Millisecond
data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
dm := NewDataManager(data, cfg.CacheCfg(), nil)
tnt := "cgrates.org"
resID := "Res1"
rS := &ResourceService{
dm: dm,
storedResources: utils.StringSet{
"Res1": struct{}{},
resID: struct{}{},
},
cgrcfg: cfg,
loopStopped: make(chan struct{}, 1),
stopBackup: make(chan struct{}),
}

value := &Resource{
dirty: utils.BoolPointer(true),
Tenant: "cgrates.org",
ID: "Res1",
}

Cache.SetWithoutReplicate(utils.CacheResources, "Res1", value, nil, true,
utils.NonTransactional)

exp := &Resource{
dirty: utils.BoolPointer(false),
Tenant: "cgrates.org",
ID: "Res1",
Tenant: tnt,
ID: resID,
}
Cache.SetWithoutReplicate(utils.CacheResources, resID, value, nil, true, "")

// Backup loop checks for the state of the stopBackup
// channel after storing the resource. Channel can be
// safely closed beforehand.
close(rS.stopBackup)
rS.runBackup()

if rcv, err := rS.dm.GetResource("cgrates.org", "Res1", true, false, utils.NonTransactional); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(rcv, exp) {
t.Errorf("resouce: want %+v, got %+v", exp, rcv)
want := &Resource{
dirty: utils.BoolPointer(false),
Tenant: tnt,
ID: resID,
}
if got, err := rS.dm.GetResource(tnt, resID, true, false, ""); err != nil {
t.Errorf("dm.GetResource(%q,%q): got unexpected err=%v", tnt, resID, err)
} else if !reflect.DeepEqual(got, want) {
t.Errorf("dm.GetResource(%q,%q) = %v, want %v", tnt, resID, got, want)
}

select {
Expand Down

0 comments on commit e025a0d

Please sign in to comment.