Skip to content

Commit

Permalink
add tests for resave notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf committed Dec 21, 2023
1 parent c32e76a commit df712fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
18 changes: 0 additions & 18 deletions database/redis/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,24 +526,6 @@ func (connector *DbConnector) AddNotifications(notifications []*moira.ScheduledN
return nil
}

func (connector *DbConnector) saveNotifications(ctx context.Context, pipe redis.Pipeliner, notifications []*moira.ScheduledNotification) error {
for _, notification := range notifications {
bytes, err := reply.GetNotificationBytes(*notification)
if err != nil {
return err
}

z := &redis.Z{Score: float64(notification.Timestamp), Member: bytes}
pipe.ZAdd(ctx, notifierNotificationsKey, z)
}

if _, err := pipe.Exec(ctx); err != nil {
return fmt.Errorf("failed to EXEC: %w", err)
}

return nil
}

func (connector *DbConnector) resaveNotifications(
ctx context.Context,
pipe redis.Pipeliner,
Expand Down
60 changes: 34 additions & 26 deletions database/redis/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ func TestGetNotificationsTriggerChecks(t *testing.T) {
})
}

func TestSaveNotifications(t *testing.T) {
func TestResaveNotifications(t *testing.T) {
logger, _ := logging.GetLogger("database")
database := NewTestDatabase(logger)
database.Flush()
Expand All @@ -1309,22 +1309,31 @@ func TestSaveNotifications(t *testing.T) {
ctx := database.context
pipe := (*client).TxPipeline()

Convey("Test saveNotifications", t, func() {
notification1 := &moira.ScheduledNotification{
Convey("Test resaveNotifications", t, func() {
notificationOld1 := &moira.ScheduledNotification{
Timestamp: 1,
SendFail: 1,
}
notification2 := &moira.ScheduledNotification{
notificationOld2 := &moira.ScheduledNotification{
Timestamp: 2,
SendFail: 2,
}
notification3 := &moira.ScheduledNotification{
notificationOld3 := &moira.ScheduledNotification{
Timestamp: 3,
}

Convey("Test with zero notifications", func() {
err := database.saveNotifications(ctx, pipe, []*moira.ScheduledNotification{})
notificationNew1 := &moira.ScheduledNotification{
Timestamp: 4,
}
notificationNew2 := &moira.ScheduledNotification{
Timestamp: 5,
}
notificationNew3 := &moira.ScheduledNotification{
Timestamp: 6,
}

Convey("Test resave with zero notifications", func() {
affected, err := database.resaveNotifications(ctx, pipe, []*moira.ScheduledNotification{}, []*moira.ScheduledNotification{})
So(err, ShouldBeNil)
So(affected, ShouldResemble, 0)

allNotifications, count, err := database.GetNotifications(0, -1)
So(err, ShouldBeNil)
Expand All @@ -1335,48 +1344,47 @@ func TestSaveNotifications(t *testing.T) {
So(err, ShouldBeNil)
})

Convey("Test save notifications with empty database", func() {
err := database.saveNotifications(ctx, pipe, []*moira.ScheduledNotification{notification1, notification2, notification3})
Convey("Test resave notifications with empty database", func() {
affected, err := database.resaveNotifications(ctx, pipe, []*moira.ScheduledNotification{notificationOld1, notificationOld2}, []*moira.ScheduledNotification{notificationNew1, notificationNew2})
So(err, ShouldBeNil)
So(affected, ShouldResemble, 2)

allNotifications, count, err := database.GetNotifications(0, -1)
So(err, ShouldBeNil)
So(allNotifications, ShouldResemble, []*moira.ScheduledNotification{notification1, notification2, notification3})
So(count, ShouldEqual, 3)
So(allNotifications, ShouldResemble, []*moira.ScheduledNotification{notificationNew1, notificationNew2})
So(count, ShouldEqual, 2)

err = database.RemoveAllNotifications()
So(err, ShouldBeNil)
})

Convey("Test save one notification when other notifications exist in the database", func() {
addNotifications(database, []moira.ScheduledNotification{*notification1, *notification3})
Convey("Test resave one notification when other notifications exist in the database", func() {
addNotifications(database, []moira.ScheduledNotification{*notificationOld1, *notificationOld3})

err := database.saveNotifications(ctx, pipe, []*moira.ScheduledNotification{notification2})
affected, err := database.resaveNotifications(ctx, pipe, []*moira.ScheduledNotification{notificationOld2}, []*moira.ScheduledNotification{notificationNew2})
So(err, ShouldBeNil)
So(affected, ShouldResemble, 1)

allNotifications, count, err := database.GetNotifications(0, -1)
So(err, ShouldBeNil)
So(allNotifications, ShouldResemble, []*moira.ScheduledNotification{notification1, notification2, notification3})
So(allNotifications, ShouldResemble, []*moira.ScheduledNotification{notificationOld1, notificationOld3, notificationNew2})
So(count, ShouldEqual, 3)

err = database.RemoveAllNotifications()
So(err, ShouldBeNil)
})

Convey("Test save one notification when there is already one with the same timestamp", func() {
newNotification := &moira.ScheduledNotification{
Timestamp: 2,
SendFail: 3,
}
addNotifications(database, []moira.ScheduledNotification{*notification1, *notification2, *notification3})
Convey("Test resave all notifications", func() {
addNotifications(database, []moira.ScheduledNotification{*notificationOld1, *notificationOld2, *notificationOld3})

err := database.saveNotifications(ctx, pipe, []*moira.ScheduledNotification{newNotification})
affected, err := database.resaveNotifications(ctx, pipe, []*moira.ScheduledNotification{notificationOld1, notificationOld2, notificationOld3}, []*moira.ScheduledNotification{notificationNew1, notificationNew2, notificationNew3})
So(err, ShouldBeNil)
So(affected, ShouldResemble, 6)

allNotifications, count, err := database.GetNotifications(0, -1)
So(err, ShouldBeNil)
assert.ElementsMatch(t, allNotifications, []*moira.ScheduledNotification{notification1, notification2, newNotification, notification3})
So(count, ShouldEqual, 4)
So(allNotifications, ShouldResemble, []*moira.ScheduledNotification{notificationNew1, notificationNew2, notificationNew3})
So(count, ShouldEqual, 3)

err = database.RemoveAllNotifications()
So(err, ShouldBeNil)
Expand Down

0 comments on commit df712fb

Please sign in to comment.