| 1 | //go:build testbuild |
| 2 | |
| 3 | package thin |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "log/slog" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/mac" |
| 13 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 14 | "amadan.net/oficina/equipo/internal/wire" |
| 15 | ) |
| 16 | |
| 17 | // CI fix (hosted run on 5da6f1fb, M9c): the connections' hourly refresh |
| 18 | // of the Mac's one Claude connection took the account's lock and then |
| 19 | // waited, inside its refresh (the darwin auth status), for the Mac's |
| 20 | // turn, while the fix round held that turn and waited for the lock: the |
| 21 | // round gave up after LockWait with provider_account_busy, ending |
| 22 | // account_unavailable. Here that refresh starts exactly while a step |
| 23 | // holds the turn, before it opens its account: the step must run. |
| 24 | func TestWorkerRefreshNeverHoldsTheAccountForTheTurn(t *testing.T) { |
| 25 | x := newEngRig(t) |
| 26 | x.note("edit:Swipe.swift|false|true") |
| 27 | wc := openWorkerConns(x.cfg, Options{Claude: x.opts, Logger: slog.New(slog.NewTextHandler(&x.logs, nil))}, x.state, runnerproto.RunnerCredential{}, x.m, func() {}, func() {}) |
| 28 | if err := wc.conns.load(context.Background()); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | b, ok := x.binding(filepath.Base(x.homes[0])) |
| 32 | if !ok { |
| 33 | t.Fatal("no binding") |
| 34 | } |
| 35 | refreshed := make(chan struct{}) |
| 36 | started := false |
| 37 | ready := x.w.Eng.Status |
| 38 | x.w.Eng.Status = func(ctx context.Context) *wire.EngineeringStatus { |
| 39 | if u, free := x.m.turns.TryTake(mac.OpCheck); free { |
| 40 | u.Give() |
| 41 | } else if !started { |
| 42 | // The step holds the turn and has not opened its account yet. |
| 43 | started = true |
| 44 | go func() { wc.conns.checkOne(context.Background(), b); close(refreshed) }() |
| 45 | time.Sleep(500 * time.Millisecond) |
| 46 | } |
| 47 | return ready(ctx) |
| 48 | } |
| 49 | rep := x.run(t) |
| 50 | if !started { |
| 51 | t.Fatal("the refresh never started inside the step's turn") |
| 52 | } |
| 53 | if rep.State != wire.StepDone || rep.CandidateSHA == "" { |
| 54 | t.Fatalf("a refresh waiting for the turn failed the step: %+v", rep) |
| 55 | } |
| 56 | select { |
| 57 | case <-refreshed: |
| 58 | case <-time.After(time.Minute): |
| 59 | t.Fatal("the refresh never ran after the step") |
| 60 | } |
| 61 | } |
| 62 | |