Skip to content

Commit

Permalink
refactor: constants and values
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko committed Jan 9, 2025
1 parent bc77115 commit 638af61
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
36 changes: 17 additions & 19 deletions database/redis/notification_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,19 +36,14 @@ 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() {
actual, err := dataBase.GetNotificationEvents(triggerID, 0, 1, allTimeFrom, allTimeTo)
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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
})

Expand Down Expand Up @@ -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)
Expand All @@ -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)
})

Expand Down Expand Up @@ -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)
})

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion notifier/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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().
Expand Down
14 changes: 7 additions & 7 deletions notifier/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 638af61

Please sign in to comment.