Skip to content

Commit

Permalink
Make variable NotificationsLimitUnlimited private in redis
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrergeru committed Oct 3, 2024
1 parent 8f9e172 commit db544e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
14 changes: 7 additions & 7 deletions database/redis/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// Separate const to prevent cyclic dependencies.
const NotificationsLimitUnlimited = int64(-1)
const notificationsLimitUnlimited = int64(-1)

type notificationTypes struct {
Valid, ToRemove, ToResaveNew, ToResaveOld []*moira.ScheduledNotification
Expand Down Expand Up @@ -295,8 +295,8 @@ func (connector *DbConnector) FetchNotifications(to int64, limit int64) ([]*moir
}

// No limit
if limit == NotificationsLimitUnlimited {
return connector.fetchNotifications(to, NotificationsLimitUnlimited)
if limit == notificationsLimitUnlimited {
return connector.fetchNotifications(to, notificationsLimitUnlimited)
}

count, err := connector.notificationsCount(to)
Expand All @@ -306,7 +306,7 @@ func (connector *DbConnector) FetchNotifications(to int64, limit int64) ([]*moir

// Hope count will be not greater then limit when we call fetchNotificationsNoLimit
if limit > connector.notification.TransactionHeuristicLimit && count < limit/2 {
return connector.fetchNotifications(to, NotificationsLimitUnlimited)
return connector.fetchNotifications(to, notificationsLimitUnlimited)
}

return connector.fetchNotifications(to, limit)
Expand Down Expand Up @@ -355,7 +355,7 @@ func (connector *DbConnector) fetchNotifications(to int64, limit int64) ([]*moir
// sorted by timestamp in one transaction with or without limit, depending on whether limit is nil.
func getNotificationsInTxWithLimit(ctx context.Context, tx *redis.Tx, to int64, limit int64) ([]*moira.ScheduledNotification, error) {
var rng *redis.ZRangeBy
if limit != NotificationsLimitUnlimited {
if limit != notificationsLimitUnlimited {
rng = &redis.ZRangeBy{Min: "-inf", Max: strconv.FormatInt(to, 10), Offset: 0, Count: limit}
} else {
rng = &redis.ZRangeBy{Min: "-inf", Max: strconv.FormatInt(to, 10)}
Expand Down Expand Up @@ -394,15 +394,15 @@ func getLimitedNotifications(

limitedNotifications := notifications

if limit != NotificationsLimitUnlimited {
if limit != notificationsLimitUnlimited {
limitedNotifications = limitNotifications(notifications)
lastTs := limitedNotifications[len(limitedNotifications)-1].Timestamp

if len(notifications) == len(limitedNotifications) {
// this means that all notifications have same timestamp,
// we hope that all notifications with same timestamp should fit our memory
var err error
limitedNotifications, err = getNotificationsInTxWithLimit(ctx, tx, lastTs, NotificationsLimitUnlimited)
limitedNotifications, err = getNotificationsInTxWithLimit(ctx, tx, lastTs, notificationsLimitUnlimited)
if err != nil {
return nil, fmt.Errorf("failed to get notification without limit in transaction: %w", err)
}
Expand Down
30 changes: 15 additions & 15 deletions database/redis/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestScheduledNotification(t *testing.T) {
})

Convey("Test fetch notifications", func() {
actual, err := database.FetchNotifications(now-database.getDelayedTimeInSeconds(), NotificationsLimitUnlimited) //nolint
actual, err := database.FetchNotifications(now-database.getDelayedTimeInSeconds(), notificationsLimitUnlimited) //nolint
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld})

Expand All @@ -67,7 +67,7 @@ func TestScheduledNotification(t *testing.T) {
So(total, ShouldEqual, 2)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notification, &notificationNew})

actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), NotificationsLimitUnlimited) //nolint
actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited) //nolint
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notification, &notificationNew})

Expand Down Expand Up @@ -127,7 +127,7 @@ func TestScheduledNotification(t *testing.T) {
So(total, ShouldEqual, 0)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})

actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), NotificationsLimitUnlimited) //nolint
actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited) //nolint
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})
})
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestScheduledNotification(t *testing.T) {
So(total, ShouldEqual, 0)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})

actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), NotificationsLimitUnlimited) //nolint
actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited) //nolint
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})
})
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestScheduledNotificationErrorConnection(t *testing.T) {
So(err, ShouldNotBeNil)
So(total, ShouldEqual, 0)

actual2, err := database.FetchNotifications(0, NotificationsLimitUnlimited)
actual2, err := database.FetchNotifications(0, notificationsLimitUnlimited)
So(err, ShouldNotBeNil)
So(actual2, ShouldBeNil)

Expand Down Expand Up @@ -283,7 +283,7 @@ func TestFetchNotifications(t *testing.T) {

Convey("Test fetch notifications without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notification, notificationNew, notificationOld})
actual, err := database.FetchNotifications(now+database.getDelayedTimeInSeconds(), NotificationsLimitUnlimited) //nolint
actual, err := database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited) //nolint
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notification, &notificationNew})

Expand Down Expand Up @@ -329,7 +329,7 @@ func TestGetNotificationsInTxWithLimit(t *testing.T) {
Convey("Test with zero notifications without limit", func() {
addNotifications(database, []moira.ScheduledNotification{})
err := client.Watch(ctx, func(tx *redis.Tx) error {
actual, err := getNotificationsInTxWithLimit(ctx, tx, now+database.getDelayedTimeInSeconds()*2, NotificationsLimitUnlimited)
actual, err := getNotificationsInTxWithLimit(ctx, tx, now+database.getDelayedTimeInSeconds()*2, notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})
return nil
Expand All @@ -343,7 +343,7 @@ func TestGetNotificationsInTxWithLimit(t *testing.T) {
Convey("Test all notifications without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notification, notificationNew, notificationOld})
err := client.Watch(ctx, func(tx *redis.Tx) error {
actual, err := getNotificationsInTxWithLimit(ctx, tx, now+database.getDelayedTimeInSeconds()*2, NotificationsLimitUnlimited)
actual, err := getNotificationsInTxWithLimit(ctx, tx, now+database.getDelayedTimeInSeconds()*2, notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notification, &notificationNew})
return nil
Expand Down Expand Up @@ -417,7 +417,7 @@ func TestGetLimitedNotifications(t *testing.T) {
Convey("Test all notifications with different timestamps without limit", func() {
notifications := []*moira.ScheduledNotification{&notificationOld, &notification, &notificationNew}
err := client.Watch(ctx, func(tx *redis.Tx) error {
actual, err := getLimitedNotifications(ctx, tx, NotificationsLimitUnlimited, notifications)
actual, err := getLimitedNotifications(ctx, tx, notificationsLimitUnlimited, notifications)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notification, &notificationNew})
return nil
Expand Down Expand Up @@ -912,7 +912,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("Without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notification, notificationNew, notificationOld})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notification, &notificationNew})

Expand All @@ -935,7 +935,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("Without limit", func() {
addNotifications(database, []moira.ScheduledNotification{})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})

Expand All @@ -946,7 +946,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("Test all notification with ts and without limit in db", func() {
addNotifications(database, []moira.ScheduledNotification{notification, notificationNew, notificationOld, notification4})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notification4, &notification, &notificationNew})

Expand Down Expand Up @@ -1015,7 +1015,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("Without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notificationOld, notificationOld2, notification, notificationNew, notificationNew2, notificationNew3})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notificationOld2, &notification, &notificationNew, &notificationNew3})

Expand Down Expand Up @@ -1051,7 +1051,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("Without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notificationOld, notificationOld2, notification, notificationNew, notificationNew2, notificationNew3})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notificationOld2, &notification, &notificationNew, &notificationNew2})

Expand Down Expand Up @@ -1091,7 +1091,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notificationOld, notificationOld2, notification, notificationNew, notificationNew2, notificationNew3})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notificationOld2, &notification, &notificationNew, &notificationNew3})

Expand Down

0 comments on commit db544e2

Please sign in to comment.