| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "errors" |
| 6 | "math" |
| 7 | "sync" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func periodSet() BudgetSet { |
| 13 | l := PeriodLimits{Calls: 20, InputTokens: 10000, OutputTokens: 10000, Seconds: 10000} |
| 14 | return BudgetSet{Team: l, Projects: map[ProjectID]PeriodLimits{1: l, 2: l}} |
| 15 | } |
| 16 | func periodJob(e *env, project ProjectID) (Scope, Handle) { |
| 17 | e.t.Helper() |
| 18 | spec := softwareSpec(SoftwareSteps(false)) |
| 19 | spec.Project = project |
| 20 | _, scope := e.createJob(spec) |
| 21 | return scope, e.claim(scope, "reproduce", "runner", "ws") |
| 22 | } |
| 23 | func periodUsage(e *env, project ProjectID) PeriodUsage { |
| 24 | e.t.Helper() |
| 25 | u, err := e.store.PeriodUsage(e.ctx, acctA, project, e.clock.Now()) |
| 26 | must(e.t, err) |
| 27 | return u |
| 28 | } |
| 29 | func TestPeriodBudgetReservations(t *testing.T) { |
| 30 | for _, dimension := range []string{"calls", "input", "output"} { |
| 31 | t.Run(dimension, func(t *testing.T) { |
| 32 | e := newEnv(t) |
| 33 | b := periodSet() |
| 34 | want := Usage{InputTokens: 1, OutputTokens: 1} |
| 35 | switch dimension { |
| 36 | case "calls": |
| 37 | b.Team.Calls = 3 |
| 38 | case "input": |
| 39 | b.Team.InputTokens = 3 |
| 40 | case "output": |
| 41 | b.Team.OutputTokens = 3 |
| 42 | } |
| 43 | for id, p := range b.Projects { |
| 44 | switch dimension { |
| 45 | case "calls": |
| 46 | p.Calls = 2 |
| 47 | case "input": |
| 48 | p.InputTokens = 2 |
| 49 | case "output": |
| 50 | p.OutputTokens = 2 |
| 51 | } |
| 52 | b.Projects[id] = p |
| 53 | } |
| 54 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, b)) |
| 55 | a, ha := periodJob(e, 1) |
| 56 | c, hc := periodJob(e, 2) |
| 57 | for _, key := range []string{"a", "b"} { |
| 58 | _, err := e.store.Intend(e.ctx, a, ha, intent(key, want)) |
| 59 | must(t, err) |
| 60 | } |
| 61 | _, err := e.store.Intend(e.ctx, a, ha, intent("project-full", want)) |
| 62 | wantCode(t, err, CodeBudget) |
| 63 | var wg sync.WaitGroup |
| 64 | results := make(chan error, 8) |
| 65 | for i := 0; i < 8; i++ { |
| 66 | wg.Add(1) |
| 67 | go func(i int) { |
| 68 | defer wg.Done() |
| 69 | _, err := e.store.Intend(e.ctx, c, hc, intent(itoa(i), want)) |
| 70 | results <- err |
| 71 | }(i) |
| 72 | } |
| 73 | wg.Wait() |
| 74 | close(results) |
| 75 | successes := 0 |
| 76 | for err := range results { |
| 77 | if err == nil { |
| 78 | successes++ |
| 79 | } else if !errors.Is(err, ErrBudget) { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | } |
| 83 | if successes != 1 { |
| 84 | t.Fatal("last budget slot was not atomic", successes) |
| 85 | } |
| 86 | if periodUsage(e, 0).Reserved.Calls != 3 { |
| 87 | t.Fatal("team rollup lost calls") |
| 88 | } |
| 89 | }) |
| 90 | } |
| 91 | } |
| 92 | func TestTimeChargeWaitsForContainment(t *testing.T) { |
| 93 | for _, stop := range []string{"cancel", "expiry"} { |
| 94 | t.Run(stop, func(t *testing.T) { |
| 95 | e := newEnv(t) |
| 96 | b := periodSet() |
| 97 | p := b.Projects[1] |
| 98 | p.Seconds = 1500 |
| 99 | b.Projects[1] = p |
| 100 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, b)) |
| 101 | a, h := periodJob(e, 1) |
| 102 | spec := softwareSpec(SoftwareSteps(false)) |
| 103 | spec.Project = 1 |
| 104 | _, other := e.createJob(spec) |
| 105 | _, err := e.store.Claim(e.ctx, other, "reproduce", "r", "ws") |
| 106 | wantCode(t, err, CodeBudget) |
| 107 | if stop == "cancel" { |
| 108 | _, err = e.store.Cancel(e.ctx, a, "owner", "stop") |
| 109 | must(t, err) |
| 110 | } |
| 111 | e.clock.Advance(300 * time.Second) |
| 112 | if stop == "expiry" { |
| 113 | e.crash() |
| 114 | e.openNoResume() |
| 115 | report, err := e.store.Resume(e.ctx) |
| 116 | must(t, err) |
| 117 | if len(report.Expired) != 1 || report.Expired[0] != h.Attempt { |
| 118 | t.Fatalf("expiry sweep: %+v", report) |
| 119 | } |
| 120 | attempts, err := e.store.Attempts(e.ctx, a) |
| 121 | must(t, err) |
| 122 | if len(attempts) != 1 || attempts[0].State != AttemptExpired { |
| 123 | t.Fatal("expiry sweep did not expire the attempt") |
| 124 | } |
| 125 | } |
| 126 | if periodUsage(e, 1).Reserved.Seconds != 1200 { |
| 127 | t.Fatal("time refunded before containment") |
| 128 | } |
| 129 | _, err = e.store.Claim(e.ctx, other, "reproduce", "r", "ws") |
| 130 | wantCode(t, err, CodeBudget) |
| 131 | must(t, e.store.Contain(e.ctx, a, h.Attempt, "supervisor")) |
| 132 | e.clock.Advance(7 * time.Second) |
| 133 | must(t, e.store.Contain(e.ctx, a, h.Attempt, "supervisor")) |
| 134 | u := periodUsage(e, 1) |
| 135 | if u.Spent.Seconds != 300 || u.Reserved.Seconds != 0 { |
| 136 | t.Fatal("contained time settled incorrectly", u) |
| 137 | } |
| 138 | _, err = e.store.Claim(e.ctx, other, "reproduce", "r", "ws") |
| 139 | must(t, err) |
| 140 | }) |
| 141 | } |
| 142 | } |
| 143 | func TestBudgetLoweringBelowUsage(t *testing.T) { |
| 144 | e := newEnv(t) |
| 145 | b := periodSet() |
| 146 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, b)) |
| 147 | a, h := periodJob(e, 1) |
| 148 | for _, key := range []string{"a", "b"} { |
| 149 | _, err := e.store.Intend(e.ctx, a, h, intent(key, Usage{})) |
| 150 | must(t, err) |
| 151 | } |
| 152 | before := periodUsage(e, 0) |
| 153 | b.Revision = 1 |
| 154 | b.Team.Calls = 1 |
| 155 | for id, p := range b.Projects { |
| 156 | p.Calls = 1 |
| 157 | b.Projects[id] = p |
| 158 | } |
| 159 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 1, b)) |
| 160 | if periodUsage(e, 0) != before { |
| 161 | t.Fatal("lowering changed usage") |
| 162 | } |
| 163 | _, err := e.store.Intend(e.ctx, a, h, intent("c", Usage{})) |
| 164 | wantCode(t, err, CodeBudget) |
| 165 | wantCode(t, e.store.ConfigureBudgets(e.ctx, acctA, 1, b), CodeRevision) |
| 166 | _, err = e.store.Cancel(e.ctx, a, "owner", "release unused calls") |
| 167 | must(t, err) |
| 168 | other, oh := periodJob(e, 2) |
| 169 | _, err = e.store.Intend(e.ctx, other, oh, intent("headroom returned", Usage{})) |
| 170 | must(t, err) |
| 171 | |
| 172 | } |
| 173 | func TestBudgetPeriodBoundary(t *testing.T) { |
| 174 | e := newEnv(t) |
| 175 | e.clock.Advance(29*24*time.Hour - time.Minute) |
| 176 | b := periodSet() |
| 177 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, b)) |
| 178 | a, h := periodJob(e, 1) |
| 179 | ids := []ActionID{} |
| 180 | for _, key := range []string{"settled", "late"} { |
| 181 | id, err := e.store.Intend(e.ctx, a, h, intent(key, Usage{InputTokens: 10})) |
| 182 | must(t, err) |
| 183 | _, err = e.store.Admit(e.ctx, a, id) |
| 184 | must(t, err) |
| 185 | ids = append(ids, id) |
| 186 | } |
| 187 | receipt := Receipt{ID: "first", Outcome: OutcomeSuccess, Usage: &Usage{Calls: 1, InputTokens: 5}} |
| 188 | must(t, e.store.Record(e.ctx, a, ids[0], receipt)) |
| 189 | nov := e.clock.Now() |
| 190 | e.clock.Advance(13 * time.Hour) |
| 191 | u := periodUsage(e, 0) |
| 192 | if u != (PeriodUsage{}) { |
| 193 | t.Fatal("period rollover carried unresolved usage", u) |
| 194 | } |
| 195 | receipt.ID = "late" |
| 196 | must(t, e.store.Record(e.ctx, a, ids[1], receipt)) |
| 197 | must(t, e.store.Record(e.ctx, a, ids[1], receipt)) |
| 198 | must(t, e.store.Contain(e.ctx, a, h.Attempt, "s")) |
| 199 | must(t, e.store.Contain(e.ctx, a, h.Attempt, "s")) |
| 200 | if periodUsage(e, 0) != (PeriodUsage{}) { |
| 201 | t.Fatal("November charge counted in December") |
| 202 | } |
| 203 | old, err := e.store.PeriodUsage(e.ctx, acctA, 1, nov) |
| 204 | must(t, err) |
| 205 | if old.Spent.Calls != 2 || old.Spent.InputTokens != 10 || old.Spent.Seconds != 13*3600 { |
| 206 | t.Fatal("late settlement or overrun lost", old) |
| 207 | } |
| 208 | } |
| 209 | func TestTimeReservationsDoNotChangeCallLedger(t *testing.T) { |
| 210 | shared := newEnv(t) |
| 211 | b := periodSet() |
| 212 | for project := ProjectID(3); project <= 5; project++ { |
| 213 | b.Projects[project] = b.Team |
| 214 | } |
| 215 | must(t, shared.store.ConfigureBudgets(shared.ctx, acctA, 0, b)) |
| 216 | t.Run("period boundary", func(t *testing.T) { |
| 217 | e := *shared |
| 218 | e.t = t |
| 219 | nov := time.Date(2026, 11, 30, 23, 59, 0, 0, time.UTC) |
| 220 | e.clock.Advance(nov.Sub(e.clock.Now())) |
| 221 | a, h := periodJob(&e, 1) |
| 222 | led, err := e.store.Ledger(e.ctx, a) |
| 223 | must(t, err) |
| 224 | if led != (LedgerSummary{}) { |
| 225 | t.Fatal("Claim changed call ledger") |
| 226 | } |
| 227 | rs, err := e.store.Reservations(e.ctx, a) |
| 228 | must(t, err) |
| 229 | if len(rs) != 0 { |
| 230 | t.Fatal("Claim created a call reservation") |
| 231 | } |
| 232 | id, err := e.store.Intend(e.ctx, a, h, intent("late", Usage{InputTokens: 10})) |
| 233 | must(t, err) |
| 234 | _, err = e.store.Admit(e.ctx, a, id) |
| 235 | must(t, err) |
| 236 | e.clock.Advance(300 * time.Second) |
| 237 | if periodUsage(&e, 1).Reserved.Seconds != 0 { |
| 238 | t.Fatal("rollover carried uncontained time into next month") |
| 239 | } |
| 240 | old, err := e.store.PeriodUsage(e.ctx, acctA, 1, nov) |
| 241 | must(t, err) |
| 242 | if old.Reserved.Seconds != 1200 { |
| 243 | t.Fatal("rollover lost original time charge", old) |
| 244 | } |
| 245 | receipt := Receipt{ID: "late", Outcome: OutcomeSuccess, Usage: &Usage{Calls: 1, InputTokens: 5}} |
| 246 | must(t, e.store.Record(e.ctx, a, id, receipt)) |
| 247 | before, err := e.store.Ledger(e.ctx, a) |
| 248 | must(t, err) |
| 249 | if before.Reserved != (Usage{}) || before.Spent != *receipt.Usage || before.Charged != *receipt.Usage { |
| 250 | t.Fatal("late receipt changed call accounting", before) |
| 251 | } |
| 252 | old, err = e.store.PeriodUsage(e.ctx, acctA, 1, nov) |
| 253 | must(t, err) |
| 254 | if old.Reserved.Seconds != 1200 { |
| 255 | t.Fatal("late receipt settled time", old) |
| 256 | } |
| 257 | must(t, e.store.Contain(e.ctx, a, h.Attempt, "s")) |
| 258 | e.clock.Advance(7 * time.Second) |
| 259 | must(t, e.store.Contain(e.ctx, a, h.Attempt, "s")) |
| 260 | must(t, e.store.Record(e.ctx, a, id, receipt)) |
| 261 | after, err := e.store.Ledger(e.ctx, a) |
| 262 | must(t, err) |
| 263 | if before != after { |
| 264 | t.Fatal("Contain or receipt retry changed call ledger across period boundary", before, after) |
| 265 | } |
| 266 | rs, err = e.store.Reservations(e.ctx, a) |
| 267 | must(t, err) |
| 268 | if len(rs) != 1 { |
| 269 | t.Fatal("containment created a call reservation") |
| 270 | } |
| 271 | if u := periodUsage(&e, 1); u != (PeriodUsage{}) { |
| 272 | t.Fatal("November charge counted in December", u) |
| 273 | } |
| 274 | old, err = e.store.PeriodUsage(e.ctx, acctA, 1, nov) |
| 275 | must(t, err) |
| 276 | if old != (PeriodUsage{Spent: PeriodLimits{Calls: 1, InputTokens: 5, Seconds: 300}}) { |
| 277 | t.Fatal("late settlement did not stay in November", old) |
| 278 | } |
| 279 | }) |
| 280 | for i, stop := range []string{"cancel", "release", "complete", "reopen"} { |
| 281 | t.Run(stop, func(t *testing.T) { |
| 282 | e := *shared |
| 283 | e.t = t |
| 284 | defer func() { shared.store = e.store }() |
| 285 | project := ProjectID(i + 2) |
| 286 | a, h := periodJob(&e, project) |
| 287 | led, err := e.store.Ledger(e.ctx, a) |
| 288 | must(t, err) |
| 289 | if led != (LedgerSummary{}) { |
| 290 | t.Fatal("Claim changed call ledger") |
| 291 | } |
| 292 | id, err := e.store.Intend(e.ctx, a, h, intent("call", Usage{InputTokens: 10})) |
| 293 | must(t, err) |
| 294 | _, err = e.store.Admit(e.ctx, a, id) |
| 295 | must(t, err) |
| 296 | must(t, e.store.Record(e.ctx, a, id, Receipt{ID: "unknown", Outcome: OutcomeSuccess})) |
| 297 | rs, err := e.store.Reservations(e.ctx, a) |
| 298 | must(t, err) |
| 299 | _, err = e.store.SettleAudited(e.ctx, a, rs[0].ID, "ops", "unknown") |
| 300 | must(t, err) |
| 301 | if periodUsage(&e, project).Reserved.Seconds != 1200 { |
| 302 | t.Fatal("call settlement touched time") |
| 303 | } |
| 304 | switch stop { |
| 305 | case "cancel": |
| 306 | _, err = e.store.Cancel(e.ctx, a, "o", "stop") |
| 307 | case "release": |
| 308 | err = e.store.Release(e.ctx, a, h, WaitProviderLimit, "stop") |
| 309 | case "complete": |
| 310 | err = e.store.Complete(e.ctx, a, h, AttemptResult{Outcome: OutcomeSuccess}) |
| 311 | case "reopen": |
| 312 | e.crash() |
| 313 | e.clock.Advance(61 * time.Second) |
| 314 | e.open() |
| 315 | } |
| 316 | must(t, err) |
| 317 | if periodUsage(&e, project).Reserved.Seconds != 1200 { |
| 318 | t.Fatal("uncontained time released") |
| 319 | } |
| 320 | before, err := e.store.Ledger(e.ctx, a) |
| 321 | must(t, err) |
| 322 | e.clock.Advance(1501 * time.Second) |
| 323 | must(t, e.store.Contain(e.ctx, a, h.Attempt, "s")) |
| 324 | must(t, e.store.Contain(e.ctx, a, h.Attempt, "s")) |
| 325 | after, err := e.store.Ledger(e.ctx, a) |
| 326 | must(t, err) |
| 327 | if before != after { |
| 328 | t.Fatal("Contain changed call ledger") |
| 329 | } |
| 330 | u := periodUsage(&e, project) |
| 331 | if u.Reserved.Seconds != 0 || u.Spent.Seconds < 1501 { |
| 332 | t.Fatal("overrun lost", u) |
| 333 | } |
| 334 | }) |
| 335 | } |
| 336 | } |
| 337 | func TestConfiguredBudgetDropsAccountCostCap(t *testing.T) { |
| 338 | for _, configured := range []bool{false, true} { |
| 339 | t.Run(itoa(map[bool]int{false: 0, true: 1}[configured]), func(t *testing.T) { |
| 340 | e := newEnv(t) |
| 341 | if configured { |
| 342 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 343 | } |
| 344 | spec := softwareSpec(SoftwareSteps(false)) |
| 345 | spec.Project = 1 |
| 346 | spec.Policy.Account.CostMicros = 1000 |
| 347 | spec.Policy.Job.CostMicros = 1500 |
| 348 | _, a := e.createJob(spec) |
| 349 | h := e.claim(a, "reproduce", "r", "ws") |
| 350 | _, err := e.store.Intend(e.ctx, a, h, intent("cost", Usage{InputTokens: 1})) |
| 351 | if configured { |
| 352 | must(t, err) |
| 353 | } else { |
| 354 | wantCode(t, err, CodeBudget) |
| 355 | } |
| 356 | _, err = e.store.Intend(e.ctx, a, h, intent("per-call", Usage{CostMicros: spec.Policy.PerCall.CostMicros})) |
| 357 | wantCode(t, err, CodeBudget) |
| 358 | if configured { |
| 359 | _, err = e.store.Intend(e.ctx, a, h, intent("job cost", Usage{})) |
| 360 | wantCode(t, err, CodeBudget) |
| 361 | } |
| 362 | }) |
| 363 | } |
| 364 | } |
| 365 | func TestPeriodBudgetConfiguration(t *testing.T) { |
| 366 | e := newEnv(t) |
| 367 | _, err := e.store.PeriodUsage(e.ctx, acctA, 0, e.clock.Now()) |
| 368 | if err == nil { |
| 369 | t.Fatal("unset usage presented as known") |
| 370 | } |
| 371 | b := periodSet() |
| 372 | bad := periodSet() |
| 373 | bad.Team.Seconds = 0 |
| 374 | wantCode(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, bad), CodeInvalid) |
| 375 | bad = periodSet() |
| 376 | bad.Team.Calls = 1 |
| 377 | wantCode(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, bad), CodeInvalid) |
| 378 | bad = periodSet() |
| 379 | bad.Team.Calls = math.MaxInt64 |
| 380 | for id, p := range bad.Projects { |
| 381 | p.Calls = math.MaxInt64 |
| 382 | bad.Projects[id] = p |
| 383 | } |
| 384 | wantCode(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, bad), CodeOverflow) |
| 385 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, b)) |
| 386 | for _, p := range []ProjectID{0} { |
| 387 | spec := softwareSpec(SoftwareSteps(false)) |
| 388 | spec.Project = p |
| 389 | _, err = e.store.CreateJob(e.ctx, spec) |
| 390 | wantCode(t, err, CodeBudget) |
| 391 | } |
| 392 | spec := softwareSpec(SoftwareSteps(false)) |
| 393 | spec.Project = 1 |
| 394 | parent, _ := e.createJob(spec) |
| 395 | spec.Parent = parent.ID |
| 396 | spec.Project = 2 |
| 397 | _, err = e.store.CreateJob(e.ctx, spec) |
| 398 | wantCode(t, err, CodeInvalid) |
| 399 | spec.Project = 0 |
| 400 | child, _ := e.createJob(spec) |
| 401 | if child.Project != parent.Project { |
| 402 | t.Fatal("child lost parent project") |
| 403 | } |
| 404 | } |
| 405 | func TestProjectBudgetMigration(t *testing.T) { |
| 406 | e := newEnv(t) |
| 407 | _, a := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 408 | h := e.claim(a, "reproduce", "r", "ws") |
| 409 | _, err := e.store.Intend(e.ctx, a, h, intent("old", Usage{InputTokens: 10})) |
| 410 | must(t, err) |
| 411 | before, err := e.store.Ledger(e.ctx, a) |
| 412 | must(t, err) |
| 413 | must(t, e.store.Close()) |
| 414 | e.store = nil |
| 415 | db, err := sql.Open("sqlite", e.path) |
| 416 | must(t, err) |
| 417 | dropRecoverySchema(t, db) |
| 418 | for _, q := range []string{"DROP TABLE time_reservations", "DROP TABLE period_budgets", "ALTER TABLE jobs DROP COLUMN project_id", "UPDATE service SET schema_version=2 WHERE id=1"} { |
| 419 | _, err = db.Exec(q) |
| 420 | must(t, err) |
| 421 | } |
| 422 | must(t, db.Close()) |
| 423 | e.open() |
| 424 | after, err := e.store.Ledger(e.ctx, a) |
| 425 | must(t, err) |
| 426 | if before != after || e.job(a).Project != 0 { |
| 427 | t.Fatal("migration changed legacy ledger/job") |
| 428 | } |
| 429 | st, err := e.store.Status(e.ctx) |
| 430 | must(t, err) |
| 431 | if st.Quarantine || st.SchemaVersion != SchemaVersion { |
| 432 | t.Fatal("migration changed witness", st) |
| 433 | } |
| 434 | _, err = e.store.Claim(e.ctx, a, "reproduce", "r2", "ws2") |
| 435 | wantCode(t, err, CodeState) |
| 436 | } |
| 437 | |
| 438 | func TestPeriodBudgetSecondsAndOverflow(t *testing.T) { |
| 439 | t.Run("team seconds atomic", func(t *testing.T) { |
| 440 | e := newEnv(t) |
| 441 | b := periodSet() |
| 442 | b.Team.Seconds = 1500 |
| 443 | for id, p := range b.Projects { |
| 444 | p.Seconds = 1500 |
| 445 | b.Projects[id] = p |
| 446 | } |
| 447 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, b)) |
| 448 | spec := softwareSpec(SoftwareSteps(false)) |
| 449 | spec.Project = 1 |
| 450 | _, a := e.createJob(spec) |
| 451 | spec.Project = 2 |
| 452 | _, c := e.createJob(spec) |
| 453 | results := make(chan error, 2) |
| 454 | for _, scope := range []Scope{a, c} { |
| 455 | go func(scope Scope) { _, err := e.store.Claim(e.ctx, scope, "reproduce", "r", "ws"); results <- err }(scope) |
| 456 | } |
| 457 | successes := 0 |
| 458 | for i := 0; i < 2; i++ { |
| 459 | err := <-results |
| 460 | if err == nil { |
| 461 | successes++ |
| 462 | } else { |
| 463 | wantCode(t, err, CodeBudget) |
| 464 | } |
| 465 | } |
| 466 | if successes != 1 { |
| 467 | t.Fatal("time last slot not atomic", successes) |
| 468 | } |
| 469 | if periodUsage(e, 0).Reserved.Seconds != 1200 { |
| 470 | t.Fatal("team seconds wrong") |
| 471 | } |
| 472 | }) |
| 473 | t.Run("usage overflow", func(t *testing.T) { |
| 474 | e := newEnv(t) |
| 475 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 476 | a, h := periodJob(e, 1) |
| 477 | for _, key := range []string{"a", "b"} { |
| 478 | id, err := e.store.Intend(e.ctx, a, h, intent(key, Usage{})) |
| 479 | must(t, err) |
| 480 | _, err = e.store.Admit(e.ctx, a, id) |
| 481 | must(t, err) |
| 482 | must(t, e.store.Record(e.ctx, a, id, Receipt{ID: key, Outcome: OutcomeSuccess})) |
| 483 | } |
| 484 | acts, err := e.store.Actions(e.ctx, a) |
| 485 | must(t, err) |
| 486 | for _, act := range acts { |
| 487 | must(t, e.store.Record(e.ctx, a, act.ID, Receipt{ID: act.Key, Outcome: OutcomeSuccess, Usage: &Usage{Calls: 1, InputTokens: math.MaxInt64}})) |
| 488 | } |
| 489 | _, err = e.store.PeriodUsage(e.ctx, acctA, 0, e.clock.Now()) |
| 490 | wantCode(t, err, CodeOverflow) |
| 491 | spec := softwareSpec(SoftwareSteps(false)) |
| 492 | spec.Project = 2 |
| 493 | _, other := e.createJob(spec) |
| 494 | _, err = e.store.Claim(e.ctx, other, "reproduce", "r", "ws") |
| 495 | wantCode(t, err, CodeOverflow) |
| 496 | }) |
| 497 | } |
| 498 | func TestProjectBudgetMigrationKeepsUncontainedTime(t *testing.T) { |
| 499 | e := newEnv(t) |
| 500 | _, a := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 501 | h := e.claim(a, "reproduce", "r", "ws") |
| 502 | must(t, e.store.Close()) |
| 503 | e.store = nil |
| 504 | db, err := sql.Open("sqlite", e.path) |
| 505 | must(t, err) |
| 506 | dropRecoverySchema(t, db) |
| 507 | for _, q := range []string{"DROP TABLE time_reservations", "DROP TABLE period_budgets", "ALTER TABLE jobs DROP COLUMN project_id", "UPDATE service SET schema_version=2 WHERE id=1"} { |
| 508 | _, err = db.Exec(q) |
| 509 | must(t, err) |
| 510 | } |
| 511 | must(t, db.Close()) |
| 512 | e.clock.Advance(300 * time.Second) |
| 513 | e.open() |
| 514 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 515 | if periodUsage(e, 0).Reserved.Seconds != 1200 { |
| 516 | t.Fatal("migration lost uncontained time") |
| 517 | } |
| 518 | must(t, e.store.Contain(e.ctx, a, h.Attempt, "s")) |
| 519 | if periodUsage(e, 0).Spent.Seconds != 300 { |
| 520 | t.Fatal("migrated time not settled") |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | func TestPeriodBudgetFailuresAreAtomic(t *testing.T) { |
| 525 | e := newEnv(t) |
| 526 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 527 | spec := softwareSpec(SoftwareSteps(false)) |
| 528 | spec.Project = 1 |
| 529 | _, a := e.createJob(spec) |
| 530 | _, err := e.store.db.Exec(`CREATE TRIGGER refuse_time BEFORE INSERT ON time_reservations BEGIN SELECT RAISE(ABORT,'test failure'); END`) |
| 531 | must(t, err) |
| 532 | _, err = e.store.Claim(e.ctx, a, "reproduce", "r", "ws") |
| 533 | if err == nil { |
| 534 | t.Fatal("missing time write accepted") |
| 535 | } |
| 536 | attempts, err := e.store.Attempts(e.ctx, a) |
| 537 | must(t, err) |
| 538 | if len(attempts) != 0 { |
| 539 | t.Fatal("failed Claim persisted attempt") |
| 540 | } |
| 541 | steps, err := e.store.Steps(e.ctx, a) |
| 542 | must(t, err) |
| 543 | if steps[0].Fence != 0 { |
| 544 | t.Fatal("failed Claim advanced fence") |
| 545 | } |
| 546 | _, err = e.store.db.Exec(`DROP TRIGGER refuse_time`) |
| 547 | must(t, err) |
| 548 | h := e.claim(a, "reproduce", "r", "ws") |
| 549 | wantCode(t, e.store.Contain(e.ctx, a, h.Attempt, "s"), CodeState) |
| 550 | if periodUsage(e, 1).Reserved.Seconds != 1200 { |
| 551 | t.Fatal("failed containment refunded time") |
| 552 | } |
| 553 | e.clock.Advance(300 * time.Second) |
| 554 | _, err = e.store.db.Exec(`CREATE TRIGGER refuse_settle BEFORE UPDATE ON time_reservations BEGIN SELECT RAISE(ABORT,'test failure'); END`) |
| 555 | must(t, err) |
| 556 | if err := e.store.Contain(e.ctx, a, h.Attempt, "s"); err == nil { |
| 557 | t.Fatal("failed time settlement accepted") |
| 558 | } |
| 559 | attempts, err = e.store.Attempts(e.ctx, a) |
| 560 | must(t, err) |
| 561 | if attempts[0].Containment != ContainmentPending { |
| 562 | t.Fatal("failed settlement marked containment") |
| 563 | } |
| 564 | _, err = e.store.db.Exec(`DROP TRIGGER refuse_settle`) |
| 565 | must(t, err) |
| 566 | must(t, e.store.Contain(e.ctx, a, h.Attempt, "s")) |
| 567 | _, err = e.store.db.Exec(`ALTER TABLE time_reservations RENAME TO unavailable_time`) |
| 568 | must(t, err) |
| 569 | if _, err := e.store.PeriodUsage(e.ctx, acctA, 0, e.clock.Now()); err == nil { |
| 570 | t.Fatal("time read failed open") |
| 571 | } |
| 572 | _, err = e.store.Claim(e.ctx, a, "reproduce", "r", "ws2") |
| 573 | if err == nil { |
| 574 | t.Fatal("time check failed open") |
| 575 | } |
| 576 | _, err = e.store.db.Exec(`ALTER TABLE unavailable_time RENAME TO time_reservations`) |
| 577 | must(t, err) |
| 578 | } |
| 579 | |