diff --git a/database/redis/notification_event_test.go b/database/redis/notification_event_test.go index f852aa6fd..cb9fc42de 100644 --- a/database/redis/notification_event_test.go +++ b/database/redis/notification_event_test.go @@ -19,11 +19,14 @@ const ( triggerID3 = "F0F4A5B9-637C-4933-AA0D-88B9798A2630" //nolint ) -var ( +const ( allTimeFrom = "-inf" allTimeTo = "+inf" - now = time.Now().Unix() - value = float64(0) +) + +var ( + now = time.Now().Unix() + value = float64(0) ) // nolint @@ -33,11 +36,6 @@ func TestNotificationEvents(t *testing.T) { dataBase.Flush() defer dataBase.Flush() - const ( - defaultFrom = "-inf" - defaultTo = "+inf" - ) - Convey("Notification events manipulation", t, func() { Convey("Test push-get-get count-fetch", func() { Convey("Should no events", func() { @@ -45,7 +43,7 @@ func TestNotificationEvents(t *testing.T) { So(err, ShouldBeNil) So(actual, ShouldResemble, make([]*moira.NotificationEvent, 0)) - total := dataBase.GetNotificationEventCount(triggerID, defaultFrom, defaultTo) + total := dataBase.GetNotificationEventCount(triggerID, allTimeFrom, allTimeTo) So(total, ShouldEqual, 0) actual1, err := dataBase.FetchNotificationEvent() @@ -78,7 +76,7 @@ func TestNotificationEvents(t *testing.T) { }, }) - total := dataBase.GetNotificationEventCount(triggerID, defaultFrom, defaultTo) + total := dataBase.GetNotificationEventCount(triggerID, allTimeFrom, allTimeTo) So(total, ShouldEqual, 1) actual1, err := dataBase.FetchNotificationEvent() @@ -107,7 +105,7 @@ func TestNotificationEvents(t *testing.T) { }, }) - total := dataBase.GetNotificationEventCount(triggerID, defaultFrom, defaultTo) + total := dataBase.GetNotificationEventCount(triggerID, allTimeFrom, allTimeTo) So(total, ShouldEqual, 1) }) @@ -153,7 +151,7 @@ func TestNotificationEvents(t *testing.T) { }, }) - total := dataBase.GetNotificationEventCount(triggerID1, defaultFrom, defaultTo) + total := dataBase.GetNotificationEventCount(triggerID1, allTimeFrom, allTimeTo) So(total, ShouldEqual, 1) actual, err = dataBase.GetNotificationEvents(triggerID2, 0, 1, allTimeFrom, allTimeTo) @@ -169,7 +167,7 @@ func TestNotificationEvents(t *testing.T) { }, }) - total = dataBase.GetNotificationEventCount(triggerID2, defaultFrom, defaultTo) + total = dataBase.GetNotificationEventCount(triggerID2, allTimeFrom, allTimeTo) So(total, ShouldEqual, 1) }) @@ -198,7 +196,7 @@ func TestNotificationEvents(t *testing.T) { }, }) - total := dataBase.GetNotificationEventCount(triggerID1, defaultFrom, defaultTo) + total := dataBase.GetNotificationEventCount(triggerID1, allTimeFrom, allTimeTo) So(total, ShouldEqual, 1) }) @@ -244,16 +242,16 @@ func TestNotificationEvents(t *testing.T) { }, }) - total := dataBase.GetNotificationEventCount(triggerID3, defaultFrom, defaultTo) + total := dataBase.GetNotificationEventCount(triggerID3, allTimeFrom, allTimeTo) So(total, ShouldEqual, 1) - total = dataBase.GetNotificationEventCount(triggerID3, strconv.FormatInt(now-1, 10), defaultTo) + total = dataBase.GetNotificationEventCount(triggerID3, strconv.FormatInt(now-1, 10), allTimeTo) So(total, ShouldEqual, 1) - total = dataBase.GetNotificationEventCount(triggerID3, strconv.FormatInt(now, 10), defaultTo) + total = dataBase.GetNotificationEventCount(triggerID3, strconv.FormatInt(now, 10), allTimeTo) So(total, ShouldEqual, 1) - total = dataBase.GetNotificationEventCount(triggerID3, strconv.FormatInt(now+1, 10), defaultTo) + total = dataBase.GetNotificationEventCount(triggerID3, strconv.FormatInt(now+1, 10), allTimeTo) So(total, ShouldEqual, 0) actual, err = dataBase.GetNotificationEvents(triggerID3, 1, 1, allTimeFrom, allTimeTo) @@ -380,7 +378,7 @@ func TestNotificationEventErrorConnection(t *testing.T) { err = dataBase.PushNotificationEvent(&newNotificationEvent, true) So(err, ShouldNotBeNil) - total := dataBase.GetNotificationEventCount("123", "-inf", "+inf") + total := dataBase.GetNotificationEventCount("123", allTimeFrom, allTimeFrom) So(total, ShouldEqual, 0) actual2, err := dataBase.FetchNotificationEvent() diff --git a/notifier/scheduler.go b/notifier/scheduler.go index d36be235b..b36ff3062 100644 --- a/notifier/scheduler.go +++ b/notifier/scheduler.go @@ -85,6 +85,8 @@ func (scheduler *StandardScheduler) ScheduleNotification(params moira.SchedulerP return notification } +const allTimeTo = "+inf" + func (scheduler *StandardScheduler) calculateNextDelivery(now time.Time, event *moira.NotificationEvent, logger moira.Logger, ) (time.Time, bool) { @@ -125,7 +127,7 @@ func (scheduler *StandardScheduler) calculateNextDelivery(now time.Time, event * if from.Before(beginning) { from = beginning } - count := scheduler.database.GetNotificationEventCount(event.TriggerID, strconv.FormatInt(from.Unix(), 10), "+inf") + count := scheduler.database.GetNotificationEventCount(event.TriggerID, strconv.FormatInt(from.Unix(), 10), allTimeTo) if count >= level.count { next = now.Add(level.delay) logger.Debug(). diff --git a/notifier/scheduler_test.go b/notifier/scheduler_test.go index 7bf3aaf60..bd090835d 100644 --- a/notifier/scheduler_test.go +++ b/notifier/scheduler_test.go @@ -98,8 +98,8 @@ func TestThrottling(t *testing.T) { systemClock.EXPECT().NowUTC().Return(now).Times(1) dataBase.EXPECT().GetTriggerThrottling(params2.Event.TriggerID).Return(now, now) dataBase.EXPECT().GetSubscription(*params2.Event.SubscriptionID).Return(subscription, nil) - dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Unix(), 10), "+inf").Return(int64(0)) - dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Unix(), 10), "+inf").Return(int64(0)) + dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Unix(), 10), allTimeTo).Return(int64(0)) + dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Unix(), 10), allTimeTo).Return(int64(0)) notification := scheduler.ScheduleNotification(params2, logger) So(notification, ShouldResemble, &expected2) @@ -274,8 +274,8 @@ func TestSubscriptionSchedule(t *testing.T) { Convey("Has trigger events count slightly less than low throttling level, should next timestamp now minutes, but throttling", func() { dataBase.EXPECT().GetTriggerThrottling(event.TriggerID).Return(time.Unix(0, 0), time.Unix(0, 0)) dataBase.EXPECT().GetSubscription(*event.SubscriptionID).Return(subscription, nil) - dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Add(-time.Hour*3).Unix(), 10), "+inf").Return(int64(13)) - dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Add(-time.Hour).Unix(), 10), "+inf").Return(int64(9)) + dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Add(-time.Hour*3).Unix(), 10), allTimeTo).Return(int64(13)) + dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Add(-time.Hour).Unix(), 10), allTimeTo).Return(int64(9)) next, throttled := scheduler.calculateNextDelivery(now, &event, logger) So(next, ShouldResemble, now) @@ -285,8 +285,8 @@ func TestSubscriptionSchedule(t *testing.T) { Convey("Has trigger events count event more than low throttling level, should next timestamp in 30 minutes", func() { dataBase.EXPECT().GetTriggerThrottling(event.TriggerID).Return(time.Unix(0, 0), time.Unix(0, 0)) dataBase.EXPECT().GetSubscription(*event.SubscriptionID).Return(subscription, nil) - dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Add(-time.Hour*3).Unix(), 10), "+inf").Return(int64(10)) - dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Add(-time.Hour).Unix(), 10), "+inf").Return(int64(10)) + dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Add(-time.Hour*3).Unix(), 10), allTimeTo).Return(int64(10)) + dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Add(-time.Hour).Unix(), 10), allTimeTo).Return(int64(10)) dataBase.EXPECT().SetTriggerThrottling(event.TriggerID, now.Add(time.Hour/2)).Return(nil) next, throttled := scheduler.calculateNextDelivery(now, &event, logger) @@ -297,7 +297,7 @@ func TestSubscriptionSchedule(t *testing.T) { Convey("Has trigger event more than high throttling level, should next timestamp in 1 hour", func() { dataBase.EXPECT().GetTriggerThrottling(event.TriggerID).Return(time.Unix(0, 0), time.Unix(0, 0)) dataBase.EXPECT().GetSubscription(*event.SubscriptionID).Return(subscription, nil) - dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Add(-time.Hour*3).Unix(), 10), "+inf").Return(int64(20)) + dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, strconv.FormatInt(now.Add(-time.Hour*3).Unix(), 10), allTimeTo).Return(int64(20)) dataBase.EXPECT().SetTriggerThrottling(event.TriggerID, now.Add(time.Hour)).Return(nil) next, throttled := scheduler.calculateNextDelivery(now, &event, logger)