Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: add tests for jobs package

goenning682ee2ce9c49
diff --git a/app/cmd/server.go b/app/cmd/server.go
index b5140bdd9..9d99f3ee3 100644
--- a/app/cmd/server.go
+++ b/app/cmd/server.go
@@ -60,11 +60,11 @@ func startJobs(ctx context.Context) {
if env.IsBillingEnabled() {
_ = c.AddJob(jobs.NewJob(ctx, "LockExpiredTenantsJob", jobs.LockExpiredTenantsJobHandler{}))
- _ = c.AddJob(jobs.NewJob(ctx, "TrialNotification7DaysJob", jobs.TrialNotificationJobHandler{
+ _ = c.AddJob(jobs.NewJob(ctx, "TrialReminder7DaysJob", jobs.TrialReminderJobHandler{
Days: 7,
TemplateName: "trial_7days",
}))
- _ = c.AddJob(jobs.NewJob(ctx, "TrialNotification1DayJob", jobs.TrialNotificationJobHandler{
+ _ = c.AddJob(jobs.NewJob(ctx, "TrialReminder1DayJob", jobs.TrialReminderJobHandler{
Days: 1,
TemplateName: "trial_1day",
}))
diff --git a/app/jobs/email_supression_job_test.go b/app/jobs/email_supression_job_test.go
new file mode 100644
index 000000000..25af3597b
--- /dev/null
+++ b/app/jobs/email_supression_job_test.go
@@ -0,0 +1,41 @@
+package jobs_test
+
+import (
+ "context"
+ "testing"
+
+ "github.com/getfider/fider/app/jobs"
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/query"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+)
+
+func TestEmailSupressionJob_Schedule_IsCorrect(t *testing.T) {
+ RegisterT(t)
+
+ job := &jobs.EmailSupressionJobHandler{}
+ Expect(job.Schedule()).Equals("0 5 * * * *")
+}
+
+func TestEmailSupressionJob_ShouldSupressRecentFailures(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.FetchRecentSupressions) error {
+ q.EmailAddresses = []string{
+ }
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.SupressEmail) error {
+ Expect(c.EmailAddresses).Equals([]string{"[email protected]", "[email protected]"})
+ return nil
+ })
+
+ job := &jobs.EmailSupressionJobHandler{}
+ err := job.Run(jobs.Context{
+ Context: context.Background(),
+ })
+ Expect(err).IsNil()
+}
diff --git a/app/jobs/job_test.go b/app/jobs/job_test.go
new file mode 100644
index 000000000..507268a8a
--- /dev/null
+++ b/app/jobs/job_test.go
@@ -0,0 +1,116 @@
+package jobs_test
+
+import (
+ "context"
+ "errors"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/getfider/fider/app/jobs"
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/query"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+)
+
+type MockJobHandler struct {
+ WaitTime time.Duration
+ ShouldFail bool
+}
+
+func (e MockJobHandler) Schedule() string {
+ return "0 * * * * *"
+}
+
+func (e MockJobHandler) Run(ctx jobs.Context) error {
+ if e.WaitTime > 0 {
+ time.Sleep(e.WaitTime)
+ }
+
+ if e.ShouldFail {
+ return errors.New("Failed")
+ }
+ return nil
+}
+
+func TestJob_WhenSuccessful_ShouldUpdateLastSuccessfulRun(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetSystemSettings) error {
+ Expect(q.Key).Equals("jobs.Test.last_successful_run")
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.SetSystemSettings) error {
+ Expect(c.Key).Equals("jobs.Test.last_successful_run")
+ Expect(c.Value).ContainsSubstring(time.Now().Format("2006-01-02"))
+ return nil
+ })
+
+ schedule, job := jobs.NewJob(context.Background(), "Test", MockJobHandler{
+ ShouldFail: false,
+ })
+ Expect(schedule).Equals("0 * * * * *")
+
+ job.Run()
+}
+
+func TestJob_WhenError_ShouldUpdateLastFailedRun(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetSystemSettings) error {
+ Expect(q.Key).Equals("jobs.Test.last_successful_run")
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.SetSystemSettings) error {
+ Expect(c.Key).Equals("jobs.Test.last_failed_run")
+ Expect(c.Value).ContainsSubstring(time.Now().Format("2006-01-02"))
+ return nil
+ })
+
+ schedule, job := jobs.NewJob(context.Background(), "Test", MockJobHandler{
+ ShouldFail: true,
+ })
+ Expect(schedule).Equals("0 * * * * *")
+
+ job.Run()
+}
+
+func TestJob_WhenTwoConcurrentRuns_ShouldExecuteOnce(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetSystemSettings) error {
+ return nil
+ })
+
+ counter := 0
+ bus.AddHandler(func(ctx context.Context, c *cmd.SetSystemSettings) error {
+ counter++
+ return nil
+ })
+
+ _, job1 := jobs.NewJob(context.Background(), "Test", MockJobHandler{
+ WaitTime: 1 * time.Second,
+ })
+ _, job2 := jobs.NewJob(context.Background(), "Test", MockJobHandler{
+ WaitTime: 1 * time.Second,
+ })
+
+ wg := sync.WaitGroup{}
+ wg.Add(2)
+
+ go func() {
+ defer wg.Done()
+ job1.Run()
+ }()
+ go func() {
+ defer wg.Done()
+ job2.Run()
+ }()
+
+ wg.Wait()
+
+ Expect(counter).Equals(1)
+}
diff --git a/app/jobs/lock_expired_tenants_job_test.go b/app/jobs/lock_expired_tenants_job_test.go
new file mode 100644
index 000000000..35c86cc14
--- /dev/null
+++ b/app/jobs/lock_expired_tenants_job_test.go
@@ -0,0 +1,32 @@
+package jobs_test
+
+import (
+ "context"
+ "testing"
+
+ "github.com/getfider/fider/app/jobs"
+ "github.com/getfider/fider/app/models/cmd"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+)
+
+func TestLockExpiredTenantsJob_Schedule_IsCorrect(t *testing.T) {
+ RegisterT(t)
+
+ job := &jobs.LockExpiredTenantsJobHandler{}
+ Expect(job.Schedule()).Equals("0 0 0 * * *")
+}
+
+func TestLockExpiredTenantsJob_ShouldJustDispatchCommand(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.LockExpiredTenants) error {
+ return nil
+ })
+
+ job := &jobs.LockExpiredTenantsJobHandler{}
+ err := job.Run(jobs.Context{
+ Context: context.Background(),
+ })
+ Expect(err).IsNil()
+}
diff --git a/app/jobs/purge_notifications_job.go b/app/jobs/purge_notifications_job.go
index 098d52669..d588957bc 100644
--- a/app/jobs/purge_notifications_job.go
+++ b/app/jobs/purge_notifications_job.go
@@ -2,7 +2,9 @@ package jobs
import (
"github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/log"
)
type PurgeExpiredNotificationsJobHandler struct {
@@ -13,5 +15,17 @@ func (e PurgeExpiredNotificationsJobHandler) Schedule() string {
}
func (e PurgeExpiredNotificationsJobHandler) Run(ctx Context) error {
- return bus.Dispatch(ctx, &cmd.PurgeExpiredNotifications{})
+ log.Debug(ctx, "deleting notifications older than 1 year")
+
+ c := &cmd.PurgeExpiredNotifications{}
+ err := bus.Dispatch(ctx, c)
+ if err != nil {
+ return err
+ }
+
+ log.Debugf(ctx, "@{RowsDeleted} notifications were deleted", dto.Props{
+ "RowsDeleted": c.NumOfDeletedNotifications,
+ })
+
+ return nil
}
diff --git a/app/jobs/purge_notifications_job_test.go b/app/jobs/purge_notifications_job_test.go
new file mode 100644
index 000000000..d48e5a86d
--- /dev/null
+++ b/app/jobs/purge_notifications_job_test.go
@@ -0,0 +1,32 @@
+package jobs_test
+
+import (
+ "context"
+ "testing"
+
+ "github.com/getfider/fider/app/jobs"
+ "github.com/getfider/fider/app/models/cmd"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+)
+
+func TestPurgeExpiredNotificationsJob_Schedule_IsCorrect(t *testing.T) {
+ RegisterT(t)
+
+ job := &jobs.PurgeExpiredNotificationsJobHandler{}
+ Expect(job.Schedule()).Equals("0 0 * * * *")
+}
+
+func TestPurgeExpiredNotificationsJob_ShouldJustDispatchCommand(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.PurgeExpiredNotifications) error {
+ return nil
+ })
+
+ job := &jobs.PurgeExpiredNotificationsJobHandler{}
+ err := job.Run(jobs.Context{
+ Context: context.Background(),
+ })
+ Expect(err).IsNil()
+}
diff --git a/app/jobs/trial_notification_job.go b/app/jobs/trial_reminder_job.go
similarity index 88%
rename from app/jobs/trial_notification_job.go
rename to app/jobs/trial_reminder_job.go
index 4106023b1..af24659a4 100644
--- a/app/jobs/trial_notification_job.go
+++ b/app/jobs/trial_reminder_job.go
@@ -12,17 +12,16 @@ import (
"github.com/getfider/fider/app/pkg/web"
)
-type TrialNotificationJobHandler struct {
+type TrialReminderJobHandler struct {
Days int
TemplateName string
}
-func (e TrialNotificationJobHandler) Schedule() string {
+func (e TrialReminderJobHandler) Schedule() string {
return "0 0 7 * * *" // every day at 7:00 AM
}
-func (e TrialNotificationJobHandler) Run(ctx Context) error {
-
+func (e TrialReminderJobHandler) Run(ctx Context) error {
if ctx.LastSuccessfulRun == nil {
// this is the first run, we can skip it
return nil
diff --git a/app/jobs/trial_reminder_job_test.go b/app/jobs/trial_reminder_job_test.go
new file mode 100644
index 000000000..0046ba1d2
--- /dev/null
+++ b/app/jobs/trial_reminder_job_test.go
@@ -0,0 +1,64 @@
+package jobs_test
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ "github.com/getfider/fider/app/jobs"
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/query"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/bus"
+)
+
+func TestTrialReminderJob_Schedule_IsCorrect(t *testing.T) {
+ RegisterT(t)
+
+ job := &jobs.TrialReminderJobHandler{}
+ Expect(job.Schedule()).Equals("0 0 7 * * *")
+}
+
+func TestTrialReminderJob_DoNothingOnFirstRun(t *testing.T) {
+ RegisterT(t)
+
+ job := &jobs.TrialReminderJobHandler{}
+ err := job.Run(jobs.Context{
+ Context: context.Background(),
+ })
+ Expect(err).IsNil()
+}
+
+func TestTrialReminderJob_ShouldSendEmailsToTrialingContacts(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetTrialingTenantContacts) error {
+ q.Contacts = []*entity.User{
+ {Name: "user1", Email: "[email protected]", Tenant: &entity.Tenant{Subdomain: "demo1"}},
+ {Name: "user2", Email: "[email protected]", Tenant: &entity.Tenant{Subdomain: "demo2"}},
+ }
+ return nil
+ })
+
+ bus.AddListener(func(ctx context.Context, c *cmd.SendMail) error {
+ Expect(c.TemplateName).Equals("trial_1day")
+ Expect(c.To).Equals([]dto.Recipient{
+ {Name: "user1", Address: "[email protected]", Props: dto.Props{"name": "user1", "url": "https://demo1.test.fider.io"}},
+ {Name: "user2", Address: "[email protected]", Props: dto.Props{"name": "user2", "url": "https://demo2.test.fider.io"}},
+ })
+ return nil
+ })
+
+ lastRun := time.Now()
+ job := &jobs.TrialReminderJobHandler{
+ Days: 1,
+ TemplateName: "trial_1day",
+ }
+ err := job.Run(jobs.Context{
+ Context: context.Background(),
+ LastSuccessfulRun: &lastRun,
+ })
+ Expect(err).IsNil()
+}
diff --git a/app/services/sqlstore/postgres/notification.go b/app/services/sqlstore/postgres/notification.go
index cac4414f8..2e7d0fc50 100644
--- a/app/services/sqlstore/postgres/notification.go
+++ b/app/services/sqlstore/postgres/notification.go
@@ -7,19 +7,15 @@ import (
"time"
"github.com/getfider/fider/app/models/cmd"
- "github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/models/entity"
"github.com/getfider/fider/app/models/enum"
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/dbx"
"github.com/getfider/fider/app/pkg/errors"
- "github.com/getfider/fider/app/pkg/log"
"github.com/lib/pq"
)
func purgeExpiredNotifications(ctx context.Context, c *cmd.PurgeExpiredNotifications) error {
- log.Debug(ctx, "deleting notifications more than 1 year old")
-
trx, err := dbx.BeginTx(ctx)
if err != nil {
return errors.Wrap(err, "failed to open transaction")
@@ -30,10 +26,6 @@ func purgeExpiredNotifications(ctx context.Context, c *cmd.PurgeExpiredNotificat
return errors.Wrap(err, "failed to delete expired notifications")
}
- log.Debugf(ctx, "@{RowsDeleted} notifications were deleted", dto.Props{
- "RowsDeleted": count,
- })
-
if err = trx.Commit(); err != nil {
return errors.Wrap(err, "failed commit transaction")
}