| 1 | package calendartest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | calendar "amadan.net/oficina/calendar/internal/calendar" |
| 10 | ) |
| 11 | |
| 12 | // The work is in the database before the failing boot, as it is when an |
| 13 | // instance restarts. It used to be inserted straight into the database |
| 14 | // after boot, which raced the retry loop: when the first retry ran before |
| 15 | // the INSERT it found nothing due, succeeded, and nothing woke it again |
| 16 | // (a raw INSERT bypasses the request middleware that wakes it), so about |
| 17 | // one run in ten failed "timer was not armed". |
| 18 | func TestTransientControlFailureAtBootRecovers(t *testing.T) { |
| 19 | control := controlForTest(t) |
| 20 | dir := t.TempDir() |
| 21 | seed := calendar.NewForTest(t, dir) |
| 22 | due := time.Now().Add(time.Hour).Unix() |
| 23 | if _, err := seed.DBForTest().Exec(`INSERT INTO reminder_queues (reminder_id,occurrence_key,due_at,attempts,late_by_sec) VALUES (1,'boot-retry',?,0,0)`, due); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | seed.Close() |
| 27 | control.mu.Lock() |
| 28 | control.at = time.Time{} |
| 29 | control.fail = true |
| 30 | control.mu.Unlock() |
| 31 | calendar.NewForTest(t, dir) |
| 32 | control.mu.Lock() |
| 33 | control.fail = false |
| 34 | control.mu.Unlock() |
| 35 | control.waitArmed(t) |
| 36 | if at, _ := control.snapshot(); at.Unix() != due { |
| 37 | t.Fatalf("recovered timer = %v", at) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestOldPlatformRefusesStartupBeforeWritingATimer(t *testing.T) { |
| 42 | control := controlForTest(t) |
| 43 | control.mu.Lock() |
| 44 | control.unsupported = true |
| 45 | control.mu.Unlock() |
| 46 | app, err := calendar.New(calendar.Config{Env: "development", DBPath: t.TempDir() + "/calendar.db", Origin: calendar.TestOrigin}) |
| 47 | if err == nil { |
| 48 | app.Close() |
| 49 | t.Fatal("old platform accepted") |
| 50 | } |
| 51 | if _, writes := control.snapshot(); writes != 0 { |
| 52 | t.Fatalf("unsupported platform received %d writes", writes) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestReadOnlyDrainFailureBacksOffWithoutOrdinaryRequestsBypassingIt(t *testing.T) { |
| 57 | control := controlForTest(t) |
| 58 | app := calendar.NewForTest(t, t.TempDir()) |
| 59 | _, err := app.DBForTest().Exec(`INSERT INTO bookings (page_id,event_id,token_hash,state,booker_address_fp,booker_zone,hold_expires_at,cancelled_by,ip_fp,ip_forget_at,reschedules,created_at,updated_at) |
| 60 | VALUES (0,0,'failure','expired','','UTC',0,'','test',?,0,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)`, time.Now().Add(-time.Minute).Unix()) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | _, err = app.DBForTest().Exec(`CREATE TRIGGER prevent_forget BEFORE UPDATE OF ip_fp ON bookings BEGIN SELECT RAISE(FAIL,'test write failure'); END`) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | for _, delay := range []time.Duration{time.Minute, 2 * time.Minute} { |
| 69 | before := time.Now() |
| 70 | rec := tick(t, app.Handler()) |
| 71 | if rec.Code != http.StatusInternalServerError { |
| 72 | t.Fatalf("write failure status = %d", rec.Code) |
| 73 | } |
| 74 | due, _ := control.snapshot() |
| 75 | if due.Before(before.Add(delay-time.Second)) || due.After(time.Now().Add(delay)) { |
| 76 | t.Fatalf("drain failure did not back off %v: %v", delay, due) |
| 77 | } |
| 78 | if got := app.NextDue(); got.Before(due) { |
| 79 | t.Fatalf("NextDue bypassed failure floor: %v < %v", got, due) |
| 80 | } |
| 81 | if raw := app.StoreForTest().NextDueAt(context.Background()); raw.IsZero() || raw.After(time.Now()) { |
| 82 | t.Fatalf("fixture is not read-success/write-failure: %v", raw) |
| 83 | } |
| 84 | } |
| 85 | if _, err := app.DBForTest().Exec(`DROP TRIGGER prevent_forget`); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | if rec := tick(t, app.Handler()); rec.Code != http.StatusNoContent { |
| 89 | t.Fatalf("recovery = %d", rec.Code) |
| 90 | } |
| 91 | if at, _ := control.snapshot(); !at.IsZero() { |
| 92 | t.Fatalf("successful drain kept failure floor: %v", at) |
| 93 | } |
| 94 | } |
| 95 | |