Skip to content

Commit

Permalink
Work on comments: unit tests for next announcements time match. Next …
Browse files Browse the repository at this point in the history
…time format. Fix for multierror.
  • Loading branch information
ice-myles committed Jun 12, 2024
1 parent b67c79b commit 565e385
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/imroc/req/v3 v3.43.7
github.com/pkg/errors v0.9.1
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
github.com/stretchr/testify v1.9.0
github.com/swaggo/swag v1.16.3
github.com/testcontainers/testcontainers-go v0.31.0
golang.org/x/net v0.26.0
Expand Down Expand Up @@ -55,6 +56,7 @@ require (
github.com/containerd/continuity v0.4.3 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
Expand Down Expand Up @@ -131,6 +133,7 @@ require (
github.com/pascaldekloe/name v1.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/quic-go v0.45.0 // indirect
github.com/redis/go-redis/v9 v9.5.3 // indirect
Expand Down
5 changes: 1 addition & 4 deletions notifications/.testdata/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,4 @@ notifications_test:
messageBroker:
<<: *notificationsMessageBroker
consumingTopics: *notificationsMessageBrokerTopics
consumerGroup: husky-notifications-testing-runner
db:
<<: *notificationsDatabase
schemaPath: notifications/DDL.lua
consumerGroup: husky-notifications-testing-runner
6 changes: 3 additions & 3 deletions notifications/announcement_weekly_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func (s *Scheduler) addScheduledWeeklyStatsAnnouncement(ctx context.Context) err
var nextTimeFormatted string
if s.cfg.Development {
nextTime = time.New(now.Add(stdlibtime.Minute))
nextTimeFormatted = nextTime.Format("2006-01-01 01:00")
nextTimeFormatted = fmt.Sprintf("%v:%02d:%02d %02d:%02d:%02d", nextTime.Year(), int(nextTime.Month()), nextTime.Day(), nextTime.Hour(), nextTime.Minute(), nextTime.Second()) //nolint:lll // .
} else {
nextTime = nextTimeMatch(now, s.cfg.WeeklyStats.Weekday, s.cfg.WeeklyStats.Hour, s.cfg.WeeklyStats.Minutes)
nextTimeFormatted = nextTime.Format("2006-01-01")
nextTimeFormatted = fmt.Sprintf("%v:%02d:%02d", nextTime.Year(), int(nextTime.Month()), nextTime.Day())
}
for language := range languages {
scheduled = append(scheduled, &scheduledAnnouncement{
Expand Down Expand Up @@ -83,7 +83,7 @@ func nextTimeMatch(now *time.Time, weekday stdlibtime.Weekday, hour, minute int)
beginningOfWeek := beginningOfDay.AddDate(0, 0, -currentWeekday)

var nextDate stdlibtime.Time
if currentWeekday == int(weekday) && now.Hour() < hour {
if (currentWeekday == int(weekday) && now.Hour() < hour) || currentWeekday < int(weekday) {
nextDate = beginningOfWeek.AddDate(0, 0, int(weekday))
} else {
nextDate = beginningOfWeek.AddDate(0, 0, int(weekday)+7) //nolint:mnd,gomnd // .
Expand Down
119 changes: 119 additions & 0 deletions notifications/announcement_weekly_stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// SPDX-License-Identifier: ice License 1.0

package notifications

import (
"testing"
stdlibtime "time"

"github.com/stretchr/testify/assert"

"github.com/ice-blockchain/wintr/time"
)

//nolint:funlen // .
func TestNextTimeMatch_AfterSpecifiedWeekday(t *testing.T) {
t.Parallel()

now := time.New(stdlibtime.Date(2024, 6, 11, 0, 0, 0, 0, stdlibtime.UTC))
expectedTime := time.New(stdlibtime.Date(2024, 6, 17, 10, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 12, 15, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 13, 15, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 14, 15, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 15, 15, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 16, 15, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 17, 0, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 17, 7, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 17, 8, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 17, 9, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))

expectedTime = time.New(stdlibtime.Date(2024, 6, 24, 10, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 17, 10, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 17, 10, 1, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 18, 0, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 19, 15, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 20, 16, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 21, 17, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 22, 17, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
now = time.New(stdlibtime.Date(2024, 6, 23, 17, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))

expectedTime = time.New(stdlibtime.Date(2024, 7, 1, 10, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 24, 17, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))
}

//nolint:dupl // .
func TestNextTimeMatch_BeforeSpecifiedWeekday(t *testing.T) {
t.Parallel()

expectedTime := time.New(stdlibtime.Date(2024, 6, 17, 10, 0, 0, 0, stdlibtime.UTC))
now := time.New(stdlibtime.Date(2024, 6, 16, 15, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))

now = time.New(stdlibtime.Date(2024, 6, 9, 0, 0, 0, 0, stdlibtime.UTC))
expectedTime = time.New(stdlibtime.Date(2024, 6, 12, 12, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Wednesday, 12, 0))

expectedTime = time.New(stdlibtime.Date(2024, 6, 12, 17, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 12, 15, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Wednesday, 17, 0))

expectedTime = time.New(stdlibtime.Date(2024, 6, 12, 17, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 11, 14, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Wednesday, 17, 0))

expectedTime = time.New(stdlibtime.Date(2024, 6, 12, 17, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 10, 14, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Wednesday, 17, 0))

expectedTime = time.New(stdlibtime.Date(2024, 6, 16, 13, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 16, 8, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Sunday, 13, 0))
}

//nolint:dupl // .
func TestNextTimeMatch_SpecifiedTimeEqualToNow(t *testing.T) {
t.Parallel()

expectedTime := time.New(stdlibtime.Date(2024, 6, 23, 10, 0, 0, 0, stdlibtime.UTC))
now := time.New(stdlibtime.Date(2024, 6, 16, 10, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Sunday, 10, 0))

expectedTime = time.New(stdlibtime.Date(2024, 6, 24, 10, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 17, 10, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Monday, 10, 0))

expectedTime = time.New(stdlibtime.Date(2024, 6, 26, 11, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 19, 11, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Wednesday, 11, 0))

expectedTime = time.New(stdlibtime.Date(2024, 6, 27, 12, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 20, 12, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Thursday, 12, 0))

expectedTime = time.New(stdlibtime.Date(2024, 6, 28, 13, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 21, 13, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Friday, 13, 0))

expectedTime = time.New(stdlibtime.Date(2024, 6, 29, 14, 0, 0, 0, stdlibtime.UTC))
now = time.New(stdlibtime.Date(2024, 6, 22, 14, 0, 0, 0, stdlibtime.UTC))
assert.Equal(t, expectedTime, nextTimeMatch(now, stdlibtime.Saturday, 14, 0))
}
2 changes: 1 addition & 1 deletion notifications/notification_type_mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (m *miningSessionSource) Process(ctx context.Context, msg *messagebroker.Me
}
var mErr *multierror.Error
if len(old) > 0 {
multierror.Append(mErr, errors.Wrapf(m.insertScheduledNotifications(ctx, old), "can't rollback scheduled notifications for:%v", message)) //nolint:lll,errcheck // .
mErr = multierror.Append(mErr, errors.Wrapf(m.insertScheduledNotifications(ctx, old), "can't rollback scheduled notifications for:%v", message))
}

return errors.Wrapf(multierror.Append(
Expand Down

0 comments on commit 565e385

Please sign in to comment.