From 565e3851b6e11c725ddda2d220d155dac06843f2 Mon Sep 17 00:00:00 2001 From: ice-myles <96409608+ice-myles@users.noreply.github.com> Date: Wed, 12 Jun 2024 19:29:37 +0300 Subject: [PATCH] Work on comments: unit tests for next announcements time match. Next time format. Fix for multierror. --- go.mod | 3 + notifications/.testdata/application.yaml | 5 +- notifications/announcement_weekly_stats.go | 6 +- .../announcement_weekly_stats_test.go | 119 ++++++++++++++++++ notifications/notification_type_mining.go | 2 +- 5 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 notifications/announcement_weekly_stats_test.go diff --git a/go.mod b/go.mod index 92bba1c..db35a0e 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 diff --git a/notifications/.testdata/application.yaml b/notifications/.testdata/application.yaml index b79f882..7f712fe 100644 --- a/notifications/.testdata/application.yaml +++ b/notifications/.testdata/application.yaml @@ -84,7 +84,4 @@ notifications_test: messageBroker: <<: *notificationsMessageBroker consumingTopics: *notificationsMessageBrokerTopics - consumerGroup: husky-notifications-testing-runner - db: - <<: *notificationsDatabase - schemaPath: notifications/DDL.lua \ No newline at end of file + consumerGroup: husky-notifications-testing-runner \ No newline at end of file diff --git a/notifications/announcement_weekly_stats.go b/notifications/announcement_weekly_stats.go index b31588a..ed4e43f 100644 --- a/notifications/announcement_weekly_stats.go +++ b/notifications/announcement_weekly_stats.go @@ -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{ @@ -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 // . diff --git a/notifications/announcement_weekly_stats_test.go b/notifications/announcement_weekly_stats_test.go new file mode 100644 index 0000000..754f6f6 --- /dev/null +++ b/notifications/announcement_weekly_stats_test.go @@ -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)) +} diff --git a/notifications/notification_type_mining.go b/notifications/notification_type_mining.go index dbcadb3..d917e7a 100644 --- a/notifications/notification_type_mining.go +++ b/notifications/notification_type_mining.go @@ -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(