oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package team
2
3import (
4 "context"
5 "database/sql"
6 "errors"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "sync"
11 "testing"
12 "time"
13)
14
15// The helper exits without Close, defers, or test cleanup: SQLite and the
16// kernel must recover the abandoned transaction and lock themselves.
17func TestCommitCrashHelper(t *testing.T) {
18 path := os.Getenv("TEAM_COMMIT_CRASH_PATH")
19 if path == "" {
20 return
21 }
22 clock := NewFakeClock(time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC))
23 opts := Options{Clock: clock, Authority: NewFakeAuthority(clock, 5*time.Second)}
24 stage := os.Getenv("TEAM_COMMIT_CRASH_STAGE")
25 hook := func(at string) error {
26 if at == stage {
27 os.Exit(73)
28 }
29 return nil
30 }
31 if os.Getenv("TEAM_COMMIT_CRASH_OPEN") == "1" {
32 opts.commitHook = hook
33 }
34 s, err := Open(context.Background(), path, opts)
35 if err != nil {
36 t.Fatal(err)
37 }
38 if os.Getenv("TEAM_COMMIT_CRASH_OPEN") != "1" {
39 _, err = s.Resume(context.Background())
40 if err == nil {
41 s.commitHook = hook
42 err = s.tx(context.Background(), func(tx *sql.Tx) error { return nil })
43 }
44 }
45 if err != nil {
46 t.Fatal(err)
47 }
48 os.Exit(74)
49}
50
51func TestProcessCrashAtCommitBoundary(t *testing.T) {
52 for _, mode := range []string{"transaction", "reopen", "first_open"} {
53 opening := mode != "transaction"
54 for _, stage := range []string{"before_commit", "after_commit"} {
55 name := mode + "/" + stage
56 t.Run(name, func(t *testing.T) {
57 e := newEnv(t)
58 e.crash()
59 if mode == "first_open" {
60 e.path = filepath.Join(e.dir, "fresh.db")
61 }
62 for round := 0; round < 2; round++ {
63 exe, err := os.Executable()
64 must(t, err)
65 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
66 cmd := exec.CommandContext(ctx, exe, "-test.run=^TestCommitCrashHelper$")
67 cmd.Dir = e.dir
68 cmd.Env = append(os.Environ(), "TEAM_COMMIT_CRASH_PATH="+e.path, "TEAM_COMMIT_CRASH_STAGE="+stage)
69 if opening {
70 cmd.Env = append(cmd.Env, "TEAM_COMMIT_CRASH_OPEN=1")
71 }
72 out, err := cmd.CombinedOutput()
73 cancel()
74 var exit *exec.ExitError
75 if !errors.As(err, &exit) || exit.ExitCode() != 73 {
76 t.Fatalf("helper did not reach crash boundary: %v\n%s", err, out)
77 }
78 }
79 e.openNoResume()
80 rr, err := e.store.Resume(e.ctx)
81 must(t, err)
82 if !rr.Resumed || rr.Quarantine {
83 t.Fatalf("ordinary process crash treated as restore: %+v", rr)
84 }
85 })
86 }
87 }
88}
89
90func TestWitnessFailureStopsHandleUntilReopen(t *testing.T) {
91 e := newEnv(t)
92 inc, revision, _, err := readMarker(e.store.marker)
93 must(t, err)
94 // A directory at the temporary-file path forces a real write failure.
95 must(t, os.Mkdir(e.store.marker+".tmp", 0o700))
96 _, err = e.store.Status(e.ctx)
97 wantCode(t, err, CodeInvalid)
98 var dbRevision int64
99 must(t, e.store.db.QueryRow(`SELECT revision FROM service WHERE id=1`).Scan(&dbRevision))
100 if dbRevision != revision+1 {
101 t.Fatalf("database advanced from %d to %d", revision, dbRevision)
102 }
103 mInc, mRevision, _, err := readMarker(e.store.marker)
104 must(t, err)
105 if mInc != inc || mRevision != revision {
106 t.Fatal("failed witness changed the published position")
107 }
108 _, err = e.store.CreateJob(e.ctx, softwareSpec(SoftwareSteps(false)))
109 wantCode(t, err, CodeStaleStore)
110 _, err = e.store.Status(e.ctx)
111 wantCode(t, err, CodeStaleStore)
112 must(t, e.store.db.QueryRow(`SELECT revision FROM service WHERE id=1`).Scan(&dbRevision))
113 if dbRevision != revision+1 {
114 t.Fatal("poisoned handle widened the unpublished gap")
115 }
116 must(t, os.Remove(e.store.marker+".tmp"))
117 // Even once storage is writable again, this handle cannot continue.
118 wantCode(t, e.store.Close(), CodeStaleStore)
119 e.store = nil
120 e.openNoResume()
121 rr, err := e.store.Resume(e.ctx)
122 must(t, err)
123 if !rr.Resumed || rr.Quarantine {
124 t.Fatalf("single unpublished commit did not recover: %+v", rr)
125 }
126 e.createJob(softwareSpec(SoftwareSteps(false)))
127}
128
129func TestCommitGuardExcludesReplacementOpen(t *testing.T) {
130 e := newEnv(t)
131 committed, release, done := make(chan struct{}), make(chan struct{}), make(chan error, 1)
132 var once sync.Once
133 finish := func() { once.Do(func() { close(release) }) }
134 e.store.commitHook = func(stage string) error {
135 if stage == "after_commit" {
136 close(committed)
137 <-release
138 }
139 return nil
140 }
141 go func() {
142 done <- e.store.tx(e.ctx, func(*sql.Tx) error { return nil })
143 }()
144 t.Cleanup(finish)
145 <-committed
146 ctx, cancel := context.WithTimeout(e.ctx, 50*time.Millisecond)
147 replacement, err := Open(ctx, e.path, e.options())
148 cancel()
149 if replacement != nil {
150 _ = replacement.Close()
151 t.Fatal("replacement opened during an unpublished commit")
152 }
153 if !errors.Is(err, context.DeadlineExceeded) {
154 t.Fatalf("replacement did not wait on the shared guard: %v", err)
155 }
156 finish()
157 must(t, <-done)
158 e.store.commitHook = nil
159 old := e.store
160 e.openNoResume()
161 _, err = old.Status(e.ctx)
162 wantCode(t, err, CodeStaleStore)
163 _ = old.Close()
164 rr, err := e.store.Resume(e.ctx)
165 must(t, err)
166 if !rr.Resumed {
167 t.Fatalf("replacement failed recovery: %+v", rr)
168 }
169}
170
171func TestRestoreDoesNotReuseWitnessIncarnation(t *testing.T) {
172 e := newEnv(t)
173 backup := e.snapshot()
174 e.crash()
175 e.open()
176 st, err := e.store.Status(e.ctx)
177 must(t, err)
178 must(t, e.store.Close())
179 e.store = nil
180 raw, err := os.ReadFile(backup)
181 must(t, err)
182 must(t, os.WriteFile(e.path, raw, 0o600))
183 e.openNoResume()
184 restored, err := e.store.Status(e.ctx)
185 must(t, err)
186 if restored.Incarnation <= st.Incarnation || !restored.Quarantine {
187 t.Fatalf("restored store reused an owner: old %+v, restored %+v", st, restored)
188 }
189}
190