| 1 | package calendartest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | calendar "amadan.net/oficina/calendar/internal/calendar" |
| 10 | ) |
| 11 | |
| 12 | func TestSuccessfulClaimRaceWithoutProgressBacksOff(t *testing.T) { |
| 13 | control := controlForTest(t) |
| 14 | app := calendar.NewForTest(t, t.TempDir()) |
| 15 | due := time.Now().Add(-time.Minute).Unix() |
| 16 | if _, err := app.DBForTest().Exec(`INSERT INTO reminder_queues (reminder_id,occurrence_key,due_at,attempts,late_by_sec) VALUES (1,'unclaimable',?,0,0)`, due); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | if _, err := app.DBForTest().Exec(`CREATE TRIGGER decline_claim BEFORE UPDATE OF claimed_at ON reminder_queues BEGIN SELECT RAISE(IGNORE); END`); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | before := time.Now() |
| 23 | if rec := tick(t, app.Handler()); rec.Code != http.StatusNoContent { |
| 24 | t.Fatalf("claim race tick = %d", rec.Code) |
| 25 | } |
| 26 | if at, _ := control.snapshot(); at.Before(before.Add(time.Minute - time.Second)) { |
| 27 | t.Fatalf("no-progress successful drain rearmed immediately: %v", at) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestRepeatedOccurrenceGuardFailsEvenAfterASuccessfulSend(t *testing.T) { |
| 32 | control := controlForTest(t) |
| 33 | app, session := signedIn(t, "someone@example.com") |
| 34 | now := time.Now().UTC() |
| 35 | for _, offset := range []time.Duration{time.Hour, 2 * time.Hour} { |
| 36 | start := now.Add(offset) |
| 37 | createEvent(t, app.Handler(), session, url.Values{"title": {"Guarded reminder"}, "start": {start.Format("2006-01-02T15:04")}, "end": {start.Add(15 * time.Minute).Format("2006-01-02T15:04")}, "kind": {"meeting"}, "status": {"confirmed"}, "repeat": {"none"}}) |
| 38 | } |
| 39 | if _, err := app.DBForTest().Exec(`UPDATE reminder_queues SET occurrence_key='same', due_at=?`, now.Add(-time.Minute).Unix()); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | before := time.Now() |
| 43 | if rec := tick(t, app.Handler()); rec.Code != http.StatusInternalServerError { |
| 44 | t.Fatalf("repeated occurrence was acknowledged despite a prior send: %d", rec.Code) |
| 45 | } |
| 46 | if at, _ := control.snapshot(); at.Before(before.Add(time.Minute - time.Second)) { |
| 47 | t.Fatalf("stalled occurrence did not back off: %v", at) |
| 48 | } |
| 49 | } |
| 50 | |