package calendartest import ( "context" "net/http" "testing" "time" calendar "amadan.net/oficina/calendar/internal/calendar" ) // The work is in the database before the failing boot, as it is when an // instance restarts. It used to be inserted straight into the database // after boot, which raced the retry loop: when the first retry ran before // the INSERT it found nothing due, succeeded, and nothing woke it again // (a raw INSERT bypasses the request middleware that wakes it), so about // one run in ten failed "timer was not armed". func TestTransientControlFailureAtBootRecovers(t *testing.T) { control := controlForTest(t) dir := t.TempDir() seed := calendar.NewForTest(t, dir) due := time.Now().Add(time.Hour).Unix() 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 { t.Fatal(err) } seed.Close() control.mu.Lock() control.at = time.Time{} control.fail = true control.mu.Unlock() calendar.NewForTest(t, dir) control.mu.Lock() control.fail = false control.mu.Unlock() control.waitArmed(t) if at, _ := control.snapshot(); at.Unix() != due { t.Fatalf("recovered timer = %v", at) } } func TestOldPlatformRefusesStartupBeforeWritingATimer(t *testing.T) { control := controlForTest(t) control.mu.Lock() control.unsupported = true control.mu.Unlock() app, err := calendar.New(calendar.Config{Env: "development", DBPath: t.TempDir() + "/calendar.db", Origin: calendar.TestOrigin}) if err == nil { app.Close() t.Fatal("old platform accepted") } if _, writes := control.snapshot(); writes != 0 { t.Fatalf("unsupported platform received %d writes", writes) } } func TestReadOnlyDrainFailureBacksOffWithoutOrdinaryRequestsBypassingIt(t *testing.T) { control := controlForTest(t) app := calendar.NewForTest(t, t.TempDir()) _, 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) VALUES (0,0,'failure','expired','','UTC',0,'','test',?,0,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)`, time.Now().Add(-time.Minute).Unix()) if err != nil { t.Fatal(err) } _, err = app.DBForTest().Exec(`CREATE TRIGGER prevent_forget BEFORE UPDATE OF ip_fp ON bookings BEGIN SELECT RAISE(FAIL,'test write failure'); END`) if err != nil { t.Fatal(err) } for _, delay := range []time.Duration{time.Minute, 2 * time.Minute} { before := time.Now() rec := tick(t, app.Handler()) if rec.Code != http.StatusInternalServerError { t.Fatalf("write failure status = %d", rec.Code) } due, _ := control.snapshot() if due.Before(before.Add(delay-time.Second)) || due.After(time.Now().Add(delay)) { t.Fatalf("drain failure did not back off %v: %v", delay, due) } if got := app.NextDue(); got.Before(due) { t.Fatalf("NextDue bypassed failure floor: %v < %v", got, due) } if raw := app.StoreForTest().NextDueAt(context.Background()); raw.IsZero() || raw.After(time.Now()) { t.Fatalf("fixture is not read-success/write-failure: %v", raw) } } if _, err := app.DBForTest().Exec(`DROP TRIGGER prevent_forget`); err != nil { t.Fatal(err) } if rec := tick(t, app.Handler()); rec.Code != http.StatusNoContent { t.Fatalf("recovery = %d", rec.Code) } if at, _ := control.snapshot(); !at.IsZero() { t.Fatalf("successful drain kept failure floor: %v", at) } }