oficina / calendar Public

Clone
git clone https://amadan.net/oficina/calendar

Plain git — no account needed to clone.

Download

Download this file

1package calendartest
2
3import (
4 "fmt"
5 "net/http"
6 "net/url"
7 "testing"
8 "time"
9
10 calendar "amadan.net/oficina/calendar/internal/calendar"
11)
12
13func TestSuccessfulClaimRaceWithoutProgressBacksOff(t *testing.T) {
14 control := controlForTest(t)
15 app := calendar.NewForTest(t, t.TempDir())
16 due := time.Now().Add(-time.Minute).Unix()
17 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 {
18 t.Fatal(err)
19 }
20 if _, err := app.DBForTest().Exec(`CREATE TRIGGER decline_claim BEFORE UPDATE OF claimed_at ON reminder_queues BEGIN SELECT RAISE(IGNORE); END`); err != nil {
21 t.Fatal(err)
22 }
23 before := time.Now()
24 if rec := tick(t, app.Handler()); rec.Code != http.StatusNoContent {
25 t.Fatalf("claim race tick = %d", rec.Code)
26 }
27 if at, _ := control.snapshot(); at.Before(before.Add(time.Minute - time.Second)) {
28 t.Fatalf("no-progress successful drain rearmed immediately: %v", at)
29 }
30}
31
32func TestRepeatedOccurrenceGuardFailsEvenAfterASuccessfulSend(t *testing.T) {
33 control := controlForTest(t)
34 app, session := signedIn(t, "someone@example.com")
35 now := time.Now().UTC()
36 start := now.Add(time.Hour)
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 past := now.Add(-time.Minute).Unix()
39 if _, err := app.DBForTest().Exec(`UPDATE reminder_queues SET due_at=?`, past); err != nil {
40 t.Fatal(err)
41 }
42 // A broken reschedule: whatever the send's completion writes back, the
43 // SAME rule's SAME occurrence is claimable again in the same pass. (Two
44 // different rules sharing an occurrence — two people's reminders on
45 // one meeting — are each owed once, and are not this.)
46 if _, err := app.DBForTest().Exec(fmt.Sprintf(`CREATE TRIGGER rearm AFTER UPDATE OF due_at ON reminder_queues WHEN NEW.due_at > %d BEGIN UPDATE reminder_queues SET due_at = %d WHERE id = NEW.id; END`, past, past)); err != nil {
47 t.Fatal(err)
48 }
49 before := time.Now()
50 if rec := tick(t, app.Handler()); rec.Code != http.StatusInternalServerError {
51 t.Fatalf("repeated occurrence was acknowledged despite a prior send: %d", rec.Code)
52 }
53 if at, _ := control.snapshot(); at.Before(before.Add(time.Minute - time.Second)) {
54 t.Fatalf("stalled occurrence did not back off: %v", at)
55 }
56}
57