| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 5 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 6 | "context" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestDirectAdmissionIntentBeforeRPC(t *testing.T) { |
| 11 | s, r, _, _ := cleanupFixture(t) |
| 12 | q := r.request("admit") |
| 13 | q.ActionID = 42 |
| 14 | s.api = apiFuncR3(func(ctx context.Context, op string, _ runnerproto.RunnerRequest, _ any) error { |
| 15 | var states []attemptState |
| 16 | mustJournal(t, s.journal.readStates(ctx, "attempt", &states)) |
| 17 | if len(states) != 1 || states[0].InFlightAction != 42 { |
| 18 | t.Fatal("admission reached instance before durable action intent") |
| 19 | } |
| 20 | return nil |
| 21 | }) |
| 22 | var ticket team.AdmissionTicket |
| 23 | mustJournal(t, s.admitDirect(context.Background(), r, q, &ticket)) |
| 24 | } |
| 25 | |
| 26 | type recoveryCounter struct{ calls int } |
| 27 | |
| 28 | func (a *recoveryCounter) Call(context.Context, string, runnerproto.RunnerRequest, any) error { |
| 29 | return nil |
| 30 | } |
| 31 | func (a *recoveryCounter) RestoreClaim(RunClaim) error { a.calls++; return nil } |
| 32 | |
| 33 | type admissionFailureSource struct { |
| 34 | journal *Journal |
| 35 | polls int |
| 36 | } |
| 37 | |
| 38 | func (a *admissionFailureSource) Poll(context.Context) ([]runnerproto.Assignment, error) { |
| 39 | a.polls++ |
| 40 | if a.polls > 1 { |
| 41 | return nil, context.Canceled |
| 42 | } |
| 43 | a.journal.maxBytes = 0 |
| 44 | return []runnerproto.Assignment{{ID: "work"}}, nil |
| 45 | } |
| 46 | func (a *admissionFailureSource) Claim(context.Context, runnerproto.ClaimRequest) (runnerproto.ClaimResponse, error) { |
| 47 | return runnerproto.ClaimResponse{}, nil |
| 48 | } |
| 49 | func TestServiceRecoversAdmissionFailure(t *testing.T) { |
| 50 | s, r, _, _ := cleanupFixture(t) |
| 51 | r.Direct = true |
| 52 | r.Phase = "contained" |
| 53 | mustJournal(t, s.save(context.Background(), r)) |
| 54 | api := &recoveryCounter{} |
| 55 | s.api = api |
| 56 | service := Service{Supervisor: s, Source: &admissionFailureSource{journal: s.journal}} |
| 57 | _ = service.Run(context.Background()) |
| 58 | if api.calls < 2 { |
| 59 | t.Fatal("admission failure skipped recovery", api.calls) |
| 60 | } |
| 61 | } |
| 62 | |