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 "sync"
6 "testing"
7 "time"
8)
9
10func reproSpec(h Handle) ArtifactSpec {
11 return ArtifactSpec{Role: RoleReproduction, MediaType: "application/json", Payload: FixtureReproduction,
12 Provenance: Provenance{Producer: h.Runner, Provider: "fake-a", Context: "ctx-" + h.Runner}}
13}
14
15func TestClaimFenceLeaseAndReclaim(t *testing.T) {
16 e := newEnv(t)
17 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
18 h1 := e.claim(scope, "reproduce", "runner-a", "ws-1")
19 if h1.Fence != 1 {
20 t.Fatalf("first fence %d", h1.Fence)
21 }
22 _, err := e.store.Claim(e.ctx, scope, "reproduce", "runner-b", "ws-2")
23 wantCode(t, err, CodeState)
24 _, err = e.store.Claim(e.ctx, scope, "implement", "runner-b", "ws-2")
25 wantCode(t, err, CodeDependency)
26 // A forged handle with the right attempt but another runner or fence.
27 _, err = e.store.Register(e.ctx, scope, Handle{Attempt: h1.Attempt, Fence: h1.Fence, Runner: "runner-z"}, reproSpec(h1))
28 wantCode(t, err, CodeFence)
29 _, err = e.store.Register(e.ctx, scope, Handle{Attempt: h1.Attempt, Fence: 7, Runner: h1.Runner}, reproSpec(h1))
30 wantCode(t, err, CodeFence)
31 e.clock.Advance(30 * time.Second)
32 dl, err := e.store.Renew(e.ctx, scope, h1)
33 must(t, err)
34 if dl != e.clock.Now().Add(60*time.Second) {
35 t.Fatalf("deadline %s", dl)
36 }
37 e.clock.Advance(60 * time.Second)
38 _, err = e.store.Renew(e.ctx, scope, h1)
39 wantCode(t, err, CodeLeaseExpired)
40 _, err = e.store.Register(e.ctx, scope, h1, reproSpec(h1))
41 wantCode(t, err, CodeLeaseExpired)
42 wantCode(t, e.store.Complete(e.ctx, scope, h1, AttemptResult{Outcome: OutcomeSuccess}), CodeLeaseExpired)
43 // Expiry is not containment: a reclaim waits for the supervisor.
44 _, err = e.store.Claim(e.ctx, scope, "reproduce", "runner-b", "ws-2")
45 wantCode(t, err, CodeContainment)
46 must(t, e.store.Contain(e.ctx, scope, h1.Attempt, "supervisor"))
47 // The old workspace cannot come back.
48 _, err = e.store.Claim(e.ctx, scope, "reproduce", "runner-b", "ws-1")
49 wantCode(t, err, CodeWorkspace)
50 h2 := e.claim(scope, "reproduce", "runner-b", "ws-2")
51 if h2.Fence != 2 || h2.Attempt == h1.Attempt {
52 t.Fatalf("second claim %+v", h2)
53 }
54 _, err = e.store.Register(e.ctx, scope, h1, reproSpec(h1))
55 wantCode(t, err, CodeFence)
56 _, err = e.store.Renew(e.ctx, scope, h1)
57 wantCode(t, err, CodeFence)
58 if _, err := e.store.Register(e.ctx, scope, h2, reproSpec(h2)); err != nil {
59 t.Fatal(err)
60 }
61 // Second attempt fails: the cap of two is reached and the job fails.
62 must(t, e.store.Complete(e.ctx, scope, h2, AttemptResult{Outcome: OutcomeFailure, Detail: "no"}))
63 if j := e.job(scope); j.State != JobFailed {
64 t.Fatalf("job %s", j.State)
65 }
66 must(t, e.store.Contain(e.ctx, scope, h2.Attempt, "supervisor"))
67 _, err = e.store.Claim(e.ctx, scope, "reproduce", "runner-c", "ws-3")
68 wantCode(t, err, CodeState)
69}
70
71func TestFailedAttemptReturnsStepToPending(t *testing.T) {
72 e := newEnv(t)
73 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
74 h1 := e.claim(scope, "reproduce", "runner-a", "ws-1")
75 must(t, e.store.Complete(e.ctx, scope, h1, AttemptResult{Outcome: OutcomeFailure, Detail: "flaky"}))
76 _, err := e.store.Claim(e.ctx, scope, "reproduce", "runner-a", "ws-2")
77 wantCode(t, err, CodeContainment)
78 must(t, e.store.Contain(e.ctx, scope, h1.Attempt, "supervisor"))
79 h2 := e.claim(scope, "reproduce", "runner-a", "ws-2")
80 if h2.Fence != 2 {
81 t.Fatalf("fence %d", h2.Fence)
82 }
83 steps, _ := e.store.Steps(e.ctx, scope)
84 if steps[0].Attempts != 2 || steps[0].State != StepRunning {
85 t.Fatalf("step %+v", steps[0])
86 }
87}
88
89func TestFailedOrReleasedAttemptDropsUndispatchedIntents(t *testing.T) {
90 e := newEnv(t)
91 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
92 h := e.claim(scope, "reproduce", "runner-a", "ws-1")
93 id, err := e.store.Intend(e.ctx, scope, h, intent("a", Usage{InputTokens: 10, OutputTokens: 10}))
94 must(t, err)
95 must(t, e.store.Release(e.ctx, scope, h, WaitProviderLimit, "rate limited"))
96 a, _ := e.store.GetAction(e.ctx, scope, id)
97 led, _ := e.store.Ledger(e.ctx, scope)
98 if a.State != ActionRevoked || led.Reserved != (Usage{}) {
99 t.Fatalf("after release: action %s ledger %+v", a.State, led)
100 }
101 must(t, e.store.Contain(e.ctx, scope, h.Attempt, "s"))
102 h2 := e.claim(scope, "reproduce", "runner-a", "ws-2")
103 id2, err := e.store.Intend(e.ctx, scope, h2, intent("b", Usage{InputTokens: 10, OutputTokens: 10}))
104 must(t, err)
105 must(t, e.store.Complete(e.ctx, scope, h2, AttemptResult{Outcome: OutcomeFailure, Detail: "no"}))
106 a, _ = e.store.GetAction(e.ctx, scope, id2)
107 led, _ = e.store.Ledger(e.ctx, scope)
108 if a.State != ActionRevoked || led.Reserved != (Usage{}) {
109 t.Fatalf("after failure: action %s ledger %+v", a.State, led)
110 }
111}
112
113func TestConcurrencyCaps(t *testing.T) {
114 e := newEnv(t)
115 spec := softwareSpec(SoftwareSteps(false))
116 spec.Steps = []StepSpec{{Key: "a", Capability: CapReproduce, Version: 1}, {Key: "b", Capability: CapReproduce, Version: 1}, {Key: "c", Capability: CapReproduce, Version: 1}}
117 _, scope := e.createJob(spec)
118 e.claim(scope, "a", "r1", "ws-a")
119 e.claim(scope, "b", "r2", "ws-b")
120 _, err := e.store.Claim(e.ctx, scope, "c", "r3", "ws-c")
121 wantCode(t, err, CodeQuota)
122 // The account cap counts other jobs of the same account.
123 _, scope2 := e.createJob(spec)
124 _, err = e.store.Claim(e.ctx, scope2, "a", "r4", "ws-a")
125 wantCode(t, err, CodeQuota)
126}
127
128func TestConcurrentClaimsOneWinner(t *testing.T) {
129 e := newEnv(t)
130 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
131 const n = 12
132 var wg sync.WaitGroup
133 results := make([]error, n)
134 handles := make([]Handle, n)
135 for i := 0; i < n; i++ {
136 wg.Add(1)
137 go func(i int) {
138 defer wg.Done()
139 handles[i], results[i] = e.store.Claim(context.Background(), scope, "reproduce", "runner", "ws-"+itoa(i))
140 }(i)
141 }
142 wg.Wait()
143 won := 0
144 for i, err := range results {
145 if err == nil {
146 won++
147 continue
148 }
149 if c := CodeOf(err); c != CodeState && c != CodeContainment {
150 t.Fatalf("claim %d: %v", i, err)
151 }
152 }
153 if won != 1 {
154 t.Fatalf("%d winners", won)
155 }
156 steps, _ := e.store.Steps(e.ctx, scope)
157 if steps[0].Fence != 1 || steps[0].Attempts != 1 {
158 t.Fatalf("step %+v", steps[0])
159 }
160}
161
162func TestConcurrentIntentsStayUnderBudget(t *testing.T) {
163 e := newEnv(t)
164 spec := softwareSpec(SoftwareSteps(false))
165 spec.Policy.Job.Calls = 5
166 _, scope := e.createJob(spec)
167 h := e.claim(scope, "reproduce", "runner-a", "ws-1")
168 const n = 16
169 var wg sync.WaitGroup
170 errs := make([]error, n)
171 for i := 0; i < n; i++ {
172 wg.Add(1)
173 go func(i int) {
174 defer wg.Done()
175 _, errs[i] = e.store.Intend(context.Background(), scope, h, IntentSpec{Key: "k" + itoa(i), Operation: OpProviderCall,
176 Payload: []byte(`{"task":"x","prompt":"` + itoa(i) + `"}`), Reserve: Usage{InputTokens: 1, OutputTokens: 1}})
177 }(i)
178 }
179 wg.Wait()
180 ok := 0
181 for _, err := range errs {
182 if err == nil {
183 ok++
184 } else {
185 wantCode(t, err, CodeBudget)
186 }
187 }
188 led, _ := e.store.Ledger(e.ctx, scope)
189 if ok != 5 || led.Reserved.Calls != 5 {
190 t.Fatalf("%d admitted, ledger %+v", ok, led)
191 }
192}
193
194func TestRenewRechecksAuthority(t *testing.T) {
195 e := newEnv(t)
196 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
197 h := e.claim(scope, "reproduce", "runner-a", "ws-1")
198 e.auth.Amend(grant)
199 _, err := e.store.Renew(e.ctx, scope, h)
200 wantCode(t, err, CodeGrantStale)
201 if j := e.job(scope); j.State != JobRunning {
202 t.Fatalf("stale grant changed job to %s", j.State)
203 }
204 // Only Resume moves the snapshot, after its own online check.
205 must(t, e.store.Close())
206 e.open()
207 if _, err := e.store.Renew(e.ctx, scope, h); err == nil {
208 t.Fatal("resume revived an uncontained stale handle")
209 }
210 must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor"))
211 _, err = e.store.Resume(e.ctx)
212 must(t, err)
213 h = e.claim(scope, "reproduce", "runner-a", "ws-2")
214 _, err = e.store.Renew(e.ctx, scope, h)
215 must(t, err)
216 e.auth.RotateEpoch()
217 _, err = e.store.Renew(e.ctx, scope, h)
218 wantCode(t, err, CodeGrantStale)
219 e.auth.Revoke(grant)
220 _, err = e.store.Renew(e.ctx, scope, h)
221 wantCode(t, err, CodeGrantRevoked)
222 j := e.job(scope)
223 if j.State != JobCancelled {
224 t.Fatalf("revocation left job %s", j.State)
225 }
226 if !e.hasEvent(scope, "job.cancelled") {
227 t.Fatal("no cancel event")
228 }
229 atts, _ := e.store.Attempts(e.ctx, scope)
230 if atts[len(atts)-1].State != AttemptStopping {
231 t.Fatalf("attempt %+v", atts[len(atts)-1])
232 }
233}
234
235// laggedAuthority answers like the fake but as if the answer had taken
236// lag to arrive, so the admission is already stale when compared.
237type laggedAuthority struct {
238 inner Authority
239 lag time.Duration
240}
241
242func (l laggedAuthority) Introspect(ctx context.Context, req AdmissionRequest) (Admission, error) {
243 a, err := l.inner.Introspect(ctx, req)
244 a.IssuedAt, a.ExpiresAt = a.IssuedAt.Add(-l.lag), a.ExpiresAt.Add(-l.lag)
245 return a, err
246}
247
248func TestAdmissionWindow(t *testing.T) {
249 e := newEnv(t)
250 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
251 h := e.claim(scope, "reproduce", "runner-a", "ws-1")
252 e.store.auth = laggedAuthority{e.auth, 6 * time.Second}
253 _, err := e.store.Renew(e.ctx, scope, h)
254 wantCode(t, err, CodeAdmissionExpired)
255 e.store.auth = NewFakeAuthority(e.clock, 10*time.Second)
256 e.store.auth.(*FakeAuthority).Issue(grant, Grant{Account: acctA, Operations: map[Operation]bool{OpAttemptContinue: true}})
257 _, err = e.store.Renew(e.ctx, scope, h)
258 wantCode(t, err, CodeAdmissionExpired)
259 e.store.auth = e.auth
260 if _, err := e.store.Renew(e.ctx, scope, h); err != nil {
261 t.Fatal(err)
262 }
263}
264
265func TestAttemptTimeCap(t *testing.T) {
266 e := newEnv(t)
267 spec := softwareSpec(SoftwareSteps(false))
268 spec.Policy.AttemptSeconds = 90
269 _, scope := e.createJob(spec)
270 h := e.claim(scope, "reproduce", "runner-a", "ws-1")
271 e.clock.Advance(50 * time.Second)
272 if _, err := e.store.Renew(e.ctx, scope, h); err != nil {
273 t.Fatal(err)
274 }
275 e.clock.Advance(50 * time.Second)
276 _, err := e.store.Renew(e.ctx, scope, h)
277 wantCode(t, err, CodeQuota)
278}
279