//go:build testbuild package thin import ( "context" "os" "path/filepath" "sync/atomic" "testing" "time" "amadan.net/oficina/equipo/internal/runner/mac" "amadan.net/oficina/equipo/internal/wire" ) // Whole-branch fix 3, #1: a launch keeps the Mac's turn, and its // recovery evidence, until the executor's Wait (stop, proof, cleanup) // has returned, even when that runs past the launcher's wait. Here the // simulator shutdown ignores its deadline and outlasts every bound: the // turn is never free while it runs, and the attempt ends unterminated // only once it has returned. func TestWorkerLaunchHoldsTheTurnThroughCleanup(t *testing.T) { x := newEngRig(t) x.note("edit:Swipe.swift|false|true") temp := filepath.Join(t.TempDir(), "T") if err := os.Mkdir(temp, 0o700); err != nil { t.Fatal(err) } var stole, ended atomic.Bool x.w.Eng.XcodeExec = &mac.Executor{Turns: x.m.turns, UserTemp: temp, Settle: time.Second, CleanLimit: 300 * time.Millisecond, QuietSimulators: func(context.Context) { for deadline := time.Now().Add(2500 * time.Millisecond); time.Now().Before(deadline); time.Sleep(100 * time.Millisecond) { if u, ok := x.m.turns.TryTake(mac.OpCheck); ok { stole.Store(true) u.Give() } } ended.Store(true) }} x.w.Eng.Xcode = func() bool { return true } // The policy names the Mac's T by its real prefix; the executor // cleans the test's own. x.m.paths.UserTemp = mac.UserTempPrefix + "/wb/T" x.r.XcodeAccess = wire.XcodeAccessSandboxed // The launcher's wait (a 200 ms grace, then the budgets plus no // margin, 1.3 s) passes long // before the cleanup returns. was, grace := macStopMargin, macStopGrace macStopMargin, macStopGrace = 0, 200*time.Millisecond t.Cleanup(func() { macStopMargin, macStopGrace = was, grace }) rep := x.run(t) if !ended.Load() || stole.Load() { t.Fatalf("the cleanup was still running (%v) or the turn was taken during it (%v)", !ended.Load(), stole.Load()) } if rep.Termination || rep.State == wire.StepDone { t.Fatalf("report %+v", rep) } if u, ok := x.m.turns.TryTake(mac.OpCheck); !ok { t.Fatal("the turn was not given back") } else { u.Give() } }