oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mac
2
3import (
4 "context"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "syscall"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/runner/mac/proc"
13)
14
15// Whole-branch fix 3, #2: the cleanup fails, a same-boot proof settles
16// the turn's record but fails the cleanup again, and then the Mac
17// reboots. Only the pending cleanup record still carries the attempt's
18// boot: Verify checks its reboot proof before any old process id, so a
19// long-lived process now leading the old group number is never checked
20// or signalled, and Retry is allowed (the completion kept).
21func TestCleanupFailedTwiceClearsAfterAReboot(t *testing.T) {
22 g, dir := gateFixture(t, true)
23 _, temp := userTempFixture(t)
24 g.UserTemp, g.Turns = temp, NewTurns(filepath.Join(t.TempDir(), "lock"))
25 boot := proc.Boot{ID: "boot-1", Time: time.Now().Add(-time.Hour)}
26 g.Turns.boot = func() (proc.Boot, error) { return boot, nil }
27 g.QuietSimulators = func(context.Context) { _ = os.Chmod(temp, 0) }
28 t.Cleanup(func() { _ = os.Chmod(temp, 0o700) })
29 res, id := run(g, dir, "checks-wb3-2", time.Minute, `touch `+quote(filepath.Join(temp, "left")))
30 if res.Terminated || !g.Turns.Pending() {
31 t.Fatalf("%+v pending=%v", res, g.Turns.Pending())
32 }
33 // A same-boot proof: the turn's record is settled, the cleanup fails
34 // again.
35 if g.Verify(id) {
36 t.Fatal("proven with its cleanup failing again")
37 }
38 if !g.Turns.Pending() {
39 t.Fatal("the pending cleanup went")
40 }
41 // The reboot; the old group number now leads a long-lived process.
42 g.QuietSimulators = nil
43 if err := os.Chmod(temp, 0o700); err != nil {
44 t.Fatal(err)
45 }
46 boot = proc.Boot{ID: "boot-2", Time: time.Now()}
47 other := exec.Command("/bin/sleep", "60")
48 other.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
49 if err := other.Start(); err != nil {
50 t.Fatal(err)
51 }
52 t.Cleanup(func() { _ = other.Process.Kill(); _ = other.Wait() })
53 _, attempt, _ := proc.ParseRecord(id)
54 reused := RecordID(other.Process.Pid, attempt)
55 if !g.Verify(reused) || g.Turns.Pending() || exists(filepath.Join(temp, "left")) {
56 t.Fatal("a reboot did not clear the attempt whose cleanup failed twice")
57 }
58 if err := other.Process.Signal(syscall.Signal(0)); err != nil {
59 t.Fatalf("the process now holding the old group number was signalled: %v", err)
60 }
61 if !g.Verify(reused) {
62 t.Fatal("the reboot's completion was not kept for the next consumer")
63 }
64}
65
66// Whole-branch fix 3, #2: only the attempt's own pending cleanup can
67// prove its reboot. Another attempt's, from an earlier boot, says
68// nothing of this one, whose marked process is still there.
69func TestAnotherAttemptsPendingProvesNothing(t *testing.T) {
70 g, dir := gateFixture(t, true)
71 g.Turns, g.Settle = NewTurns(filepath.Join(t.TempDir(), "lock")), 500*time.Millisecond
72 boot := proc.Boot{ID: "boot-2", Time: time.Now()}
73 g.Turns.boot = func() (proc.Boot, error) { return boot, nil }
74 g.procs = withPhantom("checks-wb3-3")
75 res, id := run(g, dir, "checks-wb3-3", time.Minute, `true`)
76 if res.Terminated {
77 t.Fatalf("%+v", res)
78 }
79 if err := g.Turns.writePending(pendingRecord{Attempt: "checks-old", Runner: ProcID{PID: 1 << 30, Start: 1}, Began: time.Now().Add(-2 * time.Hour), BootID: "boot-1"}); err != nil {
80 t.Fatal(err)
81 }
82 if g.Verify(id) {
83 t.Fatal("proven by another attempt's reboot proof, its own marked process still there")
84 }
85}
86