|
| 1 | +package mailretry_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/SyafaHadyan/kibooz-backend/internal/app/auth/mailretry" |
| 14 | + "github.com/SyafaHadyan/kibooz-backend/internal/mailer" |
| 15 | +) |
| 16 | + |
| 17 | +type source struct { |
| 18 | + ids []uuid.UUID |
| 19 | + err error |
| 20 | + calls int |
| 21 | + limit int |
| 22 | + lease time.Duration |
| 23 | +} |
| 24 | + |
| 25 | +func (s *source) ClaimMailRetries(_ context.Context, _ time.Time, lease time.Duration, limit int) ([]uuid.UUID, error) { |
| 26 | + s.calls++ |
| 27 | + s.limit, s.lease = limit, lease |
| 28 | + |
| 29 | + return s.ids, s.err |
| 30 | +} |
| 31 | + |
| 32 | +func ids(n int) []uuid.UUID { |
| 33 | + list := make([]uuid.UUID, n) |
| 34 | + for i := range list { |
| 35 | + list[i] = uuid.New() |
| 36 | + } |
| 37 | + |
| 38 | + return list |
| 39 | +} |
| 40 | + |
| 41 | +func TestRunMailsEveryAccountThatIsDue(t *testing.T) { |
| 42 | + src := &source{ids: ids(3)} |
| 43 | + |
| 44 | + var tried []uuid.UUID |
| 45 | + |
| 46 | + job := mailretry.NewJob(src, func(_ context.Context, id uuid.UUID) error { |
| 47 | + tried = append(tried, id) |
| 48 | + |
| 49 | + return nil |
| 50 | + }) |
| 51 | + |
| 52 | + count, err := job.Run(context.Background()) |
| 53 | + require.NoError(t, err) |
| 54 | + require.Equal(t, 3, count) |
| 55 | + require.Equal(t, src.ids, tried) |
| 56 | + require.Positive(t, src.limit) |
| 57 | + require.Greater(t, src.lease, time.Minute, "the lease is longer than the run") |
| 58 | +} |
| 59 | + |
| 60 | +func TestAFailureOfOneAccountDoesNotStopTheOthers(t *testing.T) { |
| 61 | + src := &source{ids: ids(3)} |
| 62 | + calls := 0 |
| 63 | + |
| 64 | + job := mailretry.NewJob(src, func(context.Context, uuid.UUID) error { |
| 65 | + calls++ |
| 66 | + if calls == 1 { |
| 67 | + return errors.New("database is down") |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | + }) |
| 72 | + |
| 73 | + count, err := job.Run(context.Background()) |
| 74 | + require.ErrorContains(t, err, "database is down") |
| 75 | + require.Equal(t, 3, count) |
| 76 | +} |
| 77 | + |
| 78 | +func TestAQuotaAnswerStopsTheRunAndThenTheJobForAWhile(t *testing.T) { |
| 79 | + src := &source{ids: ids(3)} |
| 80 | + calls := 0 |
| 81 | + |
| 82 | + job := mailretry.NewJob(src, func(context.Context, uuid.UUID) error { |
| 83 | + calls++ |
| 84 | + |
| 85 | + return fmt.Errorf("wrapped %w", mailer.ErrQuota) |
| 86 | + }) |
| 87 | + |
| 88 | + count, err := job.Run(context.Background()) |
| 89 | + require.NoError(t, err) |
| 90 | + require.Equal(t, 1, count) |
| 91 | + require.Equal(t, 1, calls, "the accounts after the refused one are left for later") |
| 92 | + |
| 93 | + // the next runs do not even look for accounts |
| 94 | + count, err = job.Run(context.Background()) |
| 95 | + require.NoError(t, err) |
| 96 | + require.Zero(t, count) |
| 97 | + require.Equal(t, 1, src.calls) |
| 98 | +} |
| 99 | + |
| 100 | +func TestARunReportsAFailedClaimAndStopsWithItsContext(t *testing.T) { |
| 101 | + _, err := mailretry.NewJob(&source{err: errors.New("database is down")}, nil).Run(context.Background()) |
| 102 | + require.Error(t, err) |
| 103 | + |
| 104 | + ctx, cancel := context.WithCancel(context.Background()) |
| 105 | + cancel() |
| 106 | + |
| 107 | + count, err := mailretry.NewJob(&source{ids: ids(2)}, func(context.Context, uuid.UUID) error { return nil }).Run(ctx) |
| 108 | + require.ErrorIs(t, err, context.Canceled) |
| 109 | + require.Zero(t, count) |
| 110 | +} |
| 111 | + |
| 112 | +func TestStartRunsTheJobAndStopWaitsForIt(t *testing.T) { |
| 113 | + ran := make(chan struct{}, 1) |
| 114 | + observed := make(chan error, 1) |
| 115 | + |
| 116 | + job := mailretry.NewJob(&source{ids: ids(1)}, func(context.Context, uuid.UUID) error { |
| 117 | + select { |
| 118 | + case ran <- struct{}{}: |
| 119 | + default: |
| 120 | + } |
| 121 | + |
| 122 | + return nil |
| 123 | + }).Observe(func(err error) { |
| 124 | + select { |
| 125 | + case observed <- err: |
| 126 | + default: |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + stop := job.Start(10 * time.Millisecond) |
| 131 | + |
| 132 | + select { |
| 133 | + case <-ran: |
| 134 | + case <-time.After(5 * time.Second): |
| 135 | + t.Fatal("the job did not run") |
| 136 | + } |
| 137 | + |
| 138 | + require.NoError(t, <-observed) |
| 139 | + stop() |
| 140 | + |
| 141 | + mailretry.NewJob(&source{}, nil).Start(0)() |
| 142 | +} |
| 143 | + |
| 144 | +func TestAnAnswerAboutTheRateEndsTheRunAndNotTheJob(t *testing.T) { |
| 145 | + src := &source{ids: ids(3)} |
| 146 | + calls := 0 |
| 147 | + |
| 148 | + job := mailretry.NewJob(src, func(context.Context, uuid.UUID) error { |
| 149 | + calls++ |
| 150 | + |
| 151 | + return fmt.Errorf("wrapped %w", mailer.ErrRateLimit) |
| 152 | + }) |
| 153 | + |
| 154 | + for range 2 { |
| 155 | + count, err := job.Run(context.Background()) |
| 156 | + require.NoError(t, err) |
| 157 | + require.Equal(t, 1, count) |
| 158 | + } |
| 159 | + |
| 160 | + require.Equal(t, 2, calls) |
| 161 | + require.Equal(t, 2, src.calls, "the next run looks again") |
| 162 | +} |
0 commit comments