package calendartest import ( "net/http" "net/url" "testing" "time" calendar "amadan.net/oficina/calendar/internal/calendar" ) func TestSuccessfulClaimRaceWithoutProgressBacksOff(t *testing.T) { control := controlForTest(t) app := calendar.NewForTest(t, t.TempDir()) due := time.Now().Add(-time.Minute).Unix() 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 { t.Fatal(err) } if _, err := app.DBForTest().Exec(`CREATE TRIGGER decline_claim BEFORE UPDATE OF claimed_at ON reminder_queues BEGIN SELECT RAISE(IGNORE); END`); err != nil { t.Fatal(err) } before := time.Now() if rec := tick(t, app.Handler()); rec.Code != http.StatusNoContent { t.Fatalf("claim race tick = %d", rec.Code) } if at, _ := control.snapshot(); at.Before(before.Add(time.Minute - time.Second)) { t.Fatalf("no-progress successful drain rearmed immediately: %v", at) } } func TestRepeatedOccurrenceGuardFailsEvenAfterASuccessfulSend(t *testing.T) { control := controlForTest(t) app, session := signedIn(t, "someone@example.com") now := time.Now().UTC() for _, offset := range []time.Duration{time.Hour, 2 * time.Hour} { start := now.Add(offset) 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"}}) } if _, err := app.DBForTest().Exec(`UPDATE reminder_queues SET occurrence_key='same', due_at=?`, now.Add(-time.Minute).Unix()); err != nil { t.Fatal(err) } before := time.Now() if rec := tick(t, app.Handler()); rec.Code != http.StatusInternalServerError { t.Fatalf("repeated occurrence was acknowledged despite a prior send: %d", rec.Code) } if at, _ := control.snapshot(); at.Before(before.Add(time.Minute - time.Second)) { t.Fatalf("stalled occurrence did not back off: %v", at) } }