package hostedctl import ( "context" "database/sql" "errors" "path/filepath" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/isolatedjob/isolatedjobtest" ) func TestRollbackThenElapsedTime(t *testing.T) { ctx := context.Background() clk := isolatedjobtest.NewFakeClock(testWall) s := openStore(t, filepath.Join(t.TempDir(), "hosted.db"), clk) if got := s.Clock().Now(); got != 0 { t.Fatalf("Now on a fresh database = %v, want 0", time.Duration(got)) } clk.AdvanceMono(3 * time.Minute) start := s.Clock().Now() clk.SetWall(testWall.Add(-time.Hour)) clk.AdvanceMono(10 * time.Minute) if err := s.Checkpoint(ctx); err != nil { t.Fatalf("Checkpoint = %v", err) } if got := time.Duration(s.Clock().Now() - start); got != 10*time.Minute { t.Fatalf("Now advanced %v after the wall clock went back 1h and Mono advanced 10m, want exactly 10m", got) } clk.SetWall(testWall.Add(5 * time.Hour)) if got := time.Duration(s.Clock().Now() - start); got != 10*time.Minute { t.Fatalf("Now advanced %v after the wall clock jumped forward with no Mono time, want 10m still", got) } } func TestRestartMarksElapsedUntrusted(t *testing.T) { ctx := context.Background() path := filepath.Join(t.TempDir(), "hosted.db") clk := isolatedjobtest.NewFakeClock(testWall) s, err := Open(ctx, path, clk) if err != nil { t.Fatal(err) } clk.AdvanceMono(5 * time.Minute) if err := s.Checkpoint(ctx); err != nil { t.Fatal(err) } _ = s.Close() // A new process: Mono starts again from zero, the wall clock is 30 min on. s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(30*time.Minute))) if got := time.Duration(s.Clock().Now()); got != 35*time.Minute { t.Fatalf("Now after a restart with the wall clock 30m ahead = %v, want 35m", got) } if !s.Restarted() { t.Fatal("Restarted() after a restart with the wall clock ahead = false, want true") } if alerts := s.StartupAlerts(); len(alerts) != 0 { t.Fatalf("StartupAlerts with the wall clock ahead = %v, want none", alerts) } _ = s.Close() // Open again with no write in between and the wall clock 20 min back // (still 10 min ahead of the first checkpoint). T handed out at 35m must // not drop to 15m, so Open's own checkpoint is the one that counts. s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(10*time.Minute))) if got := time.Duration(s.Clock().Now()); got != 35*time.Minute { t.Fatalf("Now after a second restart with the wall clock 20m back = %v, want 35m", got) } if !s.Restarted() { t.Fatal("Restarted() after the second restart = false, want true") } } // crash stops s the way a killed process would: no checkpoint, no cleanup // beyond what the operating system does. func crash(s *Store) { s.mu.Lock() s.closed = true s.mu.Unlock() _ = s.release() } func TestRestartKeepsCommittedTime(t *testing.T) { ctx := context.Background() path := filepath.Join(t.TempDir(), "hosted.db") clk := isolatedjobtest.NewFakeClock(testWall) s, err := Open(ctx, path, clk) if err != nil { t.Fatal(err) } var stored T if err := s.Tx(ctx, func(tx *sql.Tx) error { clk.AdvanceMono(7 * time.Minute) // time passes inside the transaction stored = s.Clock().Now() return appendAudit(tx, Audit{Action: "launch", At: stored}) }); err != nil { t.Fatal(err) } clk.SetWall(testWall.Add(-time.Hour)) crash(s) // no reaper checkpoint and no Close since the write s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-time.Hour))) if got := s.Clock().Now(); got < stored { t.Fatalf("Now after a crash and a wall clock 1h back = %v, below the %v a committed row already holds", time.Duration(got), time.Duration(stored)) } } func TestRestartAfterClose(t *testing.T) { ctx := context.Background() path := filepath.Join(t.TempDir(), "hosted.db") clk := isolatedjobtest.NewFakeClock(testWall) s, err := Open(ctx, path, clk) if err != nil { t.Fatal(err) } clk.AdvanceMono(10 * time.Minute) seen := s.Clock().Now() // read, never written by a transaction clk.SetWall(testWall.Add(-time.Hour)) if err := s.Close(); err != nil { t.Fatal(err) } s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-time.Hour))) if got := s.Clock().Now(); got < seen { t.Fatalf("Now after Close and a wall clock 1h back = %v, below the %v seen before Close", time.Duration(got), time.Duration(seen)) } } func TestRestartWithRollback(t *testing.T) { ctx := context.Background() path := filepath.Join(t.TempDir(), "hosted.db") clk := isolatedjobtest.NewFakeClock(testWall) s, err := Open(ctx, path, clk) if err != nil { t.Fatal(err) } clk.AdvanceMono(20 * time.Minute) if err := s.Checkpoint(ctx); err != nil { t.Fatal(err) } _ = s.Close() s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-time.Hour))) if got := time.Duration(s.Clock().Now()); got != 20*time.Minute { t.Fatalf("Now after a restart with the wall clock 1h behind = %v, want 20m (unchanged)", got) } if !s.Restarted() { t.Fatal("Restarted() after a restart with the wall clock behind = false, want true") } alerts := s.StartupAlerts() if len(alerts) != 1 || alerts[0].Kind != AlertClockRollback || !strings.Contains(alerts[0].Detail, "1h0m0s") || alerts[0].At != T(20*time.Minute) { t.Fatalf("StartupAlerts = %+v, want one %s alert naming 1h0m0s at 20m", alerts, AlertClockRollback) } var rolled []Audit for _, a := range auditRows(t, s) { if a.Action == AlertClockRollback { rolled = append(rolled, a) } } if len(rolled) != 1 || rolled[0].Actor != "" || !strings.Contains(rolled[0].Reason, "1h0m0s") { t.Fatalf("clock_rollback audit rows = %+v, want one from the controller naming 1h0m0s", rolled) } rec := &recordingAlerter{} deps := Deps{Store: s, Runner: nopRunner{}, Auditor: nopAuditor{}, Alerter: rec} if _, err := New(testConfig(), deps); err != nil { t.Fatalf("New = %v", err) } if _, err := New(testConfig(), deps); err != nil { t.Fatalf("second New = %v", err) } if len(rec.got) != 1 || rec.got[0] != alerts[0] { t.Fatalf("alerts delivered after Open and two News = %+v, want exactly [%+v]", rec.got, alerts[0]) } _ = s.Close() // The wall clock is still behind the first checkpoint, but Open wrote a // new one, so this restart sees no rollback. s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-time.Hour))) if alerts := s.StartupAlerts(); len(alerts) != 0 { t.Fatalf("StartupAlerts on the next restart = %+v, want none", alerts) } _ = s.Close() // An alert that cannot be delivered stops the controller starting, and // survives a crash until it is delivered. s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-2*time.Hour))) pending := s.StartupAlerts() if len(pending) != 1 { t.Fatalf("StartupAlerts after a second rollback = %+v, want one", pending) } down := errors.New("alert channel down") c, err := New(testConfig(), Deps{Store: s, Runner: nopRunner{}, Auditor: nopAuditor{}, Alerter: &recordingAlerter{fail: down}}) if c != nil || !errors.Is(err, down) { t.Fatalf("New with an undeliverable rollback alert = (%v, %v), want (nil, the alert error)", c, err) } crash(s) s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-2*time.Hour))) rec = &recordingAlerter{} if _, err := New(testConfig(), Deps{Store: s, Runner: nopRunner{}, Auditor: nopAuditor{}, Alerter: rec}); err != nil || len(rec.got) != 1 || rec.got[0] != pending[0] { t.Fatalf("New after a crash = %v with alerts %+v, want nil and exactly [%+v]", err, rec.got, pending[0]) } if !strings.HasPrefix(pending[0].Detail, "alert ") || pending[0].Detail == alerts[0].Detail { t.Fatalf("alert details %q and %q, want each to start with its own stable event ID", alerts[0].Detail, pending[0].Detail) } _ = s.Close() s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-2*time.Hour))) rec = &recordingAlerter{} if _, err := New(testConfig(), Deps{Store: s, Runner: nopRunner{}, Auditor: nopAuditor{}, Alerter: rec}); err != nil || len(rec.got) != 0 { t.Fatalf("New after the alert was delivered and the store reopened = %v with alerts %+v, want nil and none", err, rec.got) } }