oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package hostedctl
2
3import (
4 "context"
5 "database/sql"
6 "errors"
7 "path/filepath"
8 "strings"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/isolatedjob/isolatedjobtest"
13)
14
15func TestRollbackThenElapsedTime(t *testing.T) {
16 ctx := context.Background()
17 clk := isolatedjobtest.NewFakeClock(testWall)
18 s := openStore(t, filepath.Join(t.TempDir(), "hosted.db"), clk)
19 if got := s.Clock().Now(); got != 0 {
20 t.Fatalf("Now on a fresh database = %v, want 0", time.Duration(got))
21 }
22 clk.AdvanceMono(3 * time.Minute)
23 start := s.Clock().Now()
24
25 clk.SetWall(testWall.Add(-time.Hour))
26 clk.AdvanceMono(10 * time.Minute)
27 if err := s.Checkpoint(ctx); err != nil {
28 t.Fatalf("Checkpoint = %v", err)
29 }
30 if got := time.Duration(s.Clock().Now() - start); got != 10*time.Minute {
31 t.Fatalf("Now advanced %v after the wall clock went back 1h and Mono advanced 10m, want exactly 10m", got)
32 }
33
34 clk.SetWall(testWall.Add(5 * time.Hour))
35 if got := time.Duration(s.Clock().Now() - start); got != 10*time.Minute {
36 t.Fatalf("Now advanced %v after the wall clock jumped forward with no Mono time, want 10m still", got)
37 }
38}
39
40func TestRestartMarksElapsedUntrusted(t *testing.T) {
41 ctx := context.Background()
42 path := filepath.Join(t.TempDir(), "hosted.db")
43 clk := isolatedjobtest.NewFakeClock(testWall)
44 s, err := Open(ctx, path, clk)
45 if err != nil {
46 t.Fatal(err)
47 }
48 clk.AdvanceMono(5 * time.Minute)
49 if err := s.Checkpoint(ctx); err != nil {
50 t.Fatal(err)
51 }
52 _ = s.Close()
53
54 // A new process: Mono starts again from zero, the wall clock is 30 min on.
55 s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(30*time.Minute)))
56 if got := time.Duration(s.Clock().Now()); got != 35*time.Minute {
57 t.Fatalf("Now after a restart with the wall clock 30m ahead = %v, want 35m", got)
58 }
59 if !s.Restarted() {
60 t.Fatal("Restarted() after a restart with the wall clock ahead = false, want true")
61 }
62 if alerts := s.StartupAlerts(); len(alerts) != 0 {
63 t.Fatalf("StartupAlerts with the wall clock ahead = %v, want none", alerts)
64 }
65 _ = s.Close()
66
67 // Open again with no write in between and the wall clock 20 min back
68 // (still 10 min ahead of the first checkpoint). T handed out at 35m must
69 // not drop to 15m, so Open's own checkpoint is the one that counts.
70 s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(10*time.Minute)))
71 if got := time.Duration(s.Clock().Now()); got != 35*time.Minute {
72 t.Fatalf("Now after a second restart with the wall clock 20m back = %v, want 35m", got)
73 }
74 if !s.Restarted() {
75 t.Fatal("Restarted() after the second restart = false, want true")
76 }
77}
78
79// crash stops s the way a killed process would: no checkpoint, no cleanup
80// beyond what the operating system does.
81func crash(s *Store) {
82 s.mu.Lock()
83 s.closed = true
84 s.mu.Unlock()
85 _ = s.release()
86}
87
88func TestRestartKeepsCommittedTime(t *testing.T) {
89 ctx := context.Background()
90 path := filepath.Join(t.TempDir(), "hosted.db")
91 clk := isolatedjobtest.NewFakeClock(testWall)
92 s, err := Open(ctx, path, clk)
93 if err != nil {
94 t.Fatal(err)
95 }
96 var stored T
97 if err := s.Tx(ctx, func(tx *sql.Tx) error {
98 clk.AdvanceMono(7 * time.Minute) // time passes inside the transaction
99 stored = s.Clock().Now()
100 return appendAudit(tx, Audit{Action: "launch", At: stored})
101 }); err != nil {
102 t.Fatal(err)
103 }
104 clk.SetWall(testWall.Add(-time.Hour))
105 crash(s) // no reaper checkpoint and no Close since the write
106
107 s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-time.Hour)))
108 if got := s.Clock().Now(); got < stored {
109 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))
110 }
111}
112
113func TestRestartAfterClose(t *testing.T) {
114 ctx := context.Background()
115 path := filepath.Join(t.TempDir(), "hosted.db")
116 clk := isolatedjobtest.NewFakeClock(testWall)
117 s, err := Open(ctx, path, clk)
118 if err != nil {
119 t.Fatal(err)
120 }
121 clk.AdvanceMono(10 * time.Minute)
122 seen := s.Clock().Now() // read, never written by a transaction
123 clk.SetWall(testWall.Add(-time.Hour))
124 if err := s.Close(); err != nil {
125 t.Fatal(err)
126 }
127
128 s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-time.Hour)))
129 if got := s.Clock().Now(); got < seen {
130 t.Fatalf("Now after Close and a wall clock 1h back = %v, below the %v seen before Close", time.Duration(got), time.Duration(seen))
131 }
132}
133
134func TestRestartWithRollback(t *testing.T) {
135 ctx := context.Background()
136 path := filepath.Join(t.TempDir(), "hosted.db")
137 clk := isolatedjobtest.NewFakeClock(testWall)
138 s, err := Open(ctx, path, clk)
139 if err != nil {
140 t.Fatal(err)
141 }
142 clk.AdvanceMono(20 * time.Minute)
143 if err := s.Checkpoint(ctx); err != nil {
144 t.Fatal(err)
145 }
146 _ = s.Close()
147
148 s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-time.Hour)))
149 if got := time.Duration(s.Clock().Now()); got != 20*time.Minute {
150 t.Fatalf("Now after a restart with the wall clock 1h behind = %v, want 20m (unchanged)", got)
151 }
152 if !s.Restarted() {
153 t.Fatal("Restarted() after a restart with the wall clock behind = false, want true")
154 }
155 alerts := s.StartupAlerts()
156 if len(alerts) != 1 || alerts[0].Kind != AlertClockRollback || !strings.Contains(alerts[0].Detail, "1h0m0s") || alerts[0].At != T(20*time.Minute) {
157 t.Fatalf("StartupAlerts = %+v, want one %s alert naming 1h0m0s at 20m", alerts, AlertClockRollback)
158 }
159 var rolled []Audit
160 for _, a := range auditRows(t, s) {
161 if a.Action == AlertClockRollback {
162 rolled = append(rolled, a)
163 }
164 }
165 if len(rolled) != 1 || rolled[0].Actor != "" || !strings.Contains(rolled[0].Reason, "1h0m0s") {
166 t.Fatalf("clock_rollback audit rows = %+v, want one from the controller naming 1h0m0s", rolled)
167 }
168
169 rec := &recordingAlerter{}
170 deps := Deps{Store: s, Runner: nopRunner{}, Auditor: nopAuditor{}, Alerter: rec}
171 if _, err := New(testConfig(), deps); err != nil {
172 t.Fatalf("New = %v", err)
173 }
174 if _, err := New(testConfig(), deps); err != nil {
175 t.Fatalf("second New = %v", err)
176 }
177 if len(rec.got) != 1 || rec.got[0] != alerts[0] {
178 t.Fatalf("alerts delivered after Open and two News = %+v, want exactly [%+v]", rec.got, alerts[0])
179 }
180 _ = s.Close()
181
182 // The wall clock is still behind the first checkpoint, but Open wrote a
183 // new one, so this restart sees no rollback.
184 s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-time.Hour)))
185 if alerts := s.StartupAlerts(); len(alerts) != 0 {
186 t.Fatalf("StartupAlerts on the next restart = %+v, want none", alerts)
187 }
188 _ = s.Close()
189
190 // An alert that cannot be delivered stops the controller starting, and
191 // survives a crash until it is delivered.
192 s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-2*time.Hour)))
193 pending := s.StartupAlerts()
194 if len(pending) != 1 {
195 t.Fatalf("StartupAlerts after a second rollback = %+v, want one", pending)
196 }
197 down := errors.New("alert channel down")
198 c, err := New(testConfig(), Deps{Store: s, Runner: nopRunner{}, Auditor: nopAuditor{}, Alerter: &recordingAlerter{fail: down}})
199 if c != nil || !errors.Is(err, down) {
200 t.Fatalf("New with an undeliverable rollback alert = (%v, %v), want (nil, the alert error)", c, err)
201 }
202 crash(s)
203
204 s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-2*time.Hour)))
205 rec = &recordingAlerter{}
206 if _, err := New(testConfig(), Deps{Store: s, Runner: nopRunner{}, Auditor: nopAuditor{}, Alerter: rec}); err != nil || len(rec.got) != 1 || rec.got[0] != pending[0] {
207 t.Fatalf("New after a crash = %v with alerts %+v, want nil and exactly [%+v]", err, rec.got, pending[0])
208 }
209 if !strings.HasPrefix(pending[0].Detail, "alert ") || pending[0].Detail == alerts[0].Detail {
210 t.Fatalf("alert details %q and %q, want each to start with its own stable event ID", alerts[0].Detail, pending[0].Detail)
211 }
212 _ = s.Close()
213
214 s = openStore(t, path, isolatedjobtest.NewFakeClock(testWall.Add(-2*time.Hour)))
215 rec = &recordingAlerter{}
216 if _, err := New(testConfig(), Deps{Store: s, Runner: nopRunner{}, Auditor: nopAuditor{}, Alerter: rec}); err != nil || len(rec.got) != 0 {
217 t.Fatalf("New after the alert was delivered and the store reopened = %v with alerts %+v, want nil and none", err, rec.got)
218 }
219}
220