| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func TestContainTimeAfterClockStep(t *testing.T) { |
| 10 | for _, tc := range []struct { |
| 11 | elapsed time.Duration |
| 12 | seconds int64 |
| 13 | }{ |
| 14 | {-time.Minute, 0}, {-time.Nanosecond, 0}, {0, 0}, {time.Nanosecond, 1}, {1501 * time.Second, 1501}, |
| 15 | } { |
| 16 | t.Run(tc.elapsed.String(), func(t *testing.T) { |
| 17 | e := newEnv(t) |
| 18 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 19 | scope, h := periodJob(e, 1) |
| 20 | _, err := e.store.Cancel(e.ctx, scope, "owner", "stop") |
| 21 | must(t, err) |
| 22 | e.clock.Advance(tc.elapsed) |
| 23 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| 24 | usage := periodUsage(e, 1) |
| 25 | reserved := int64(0) |
| 26 | if tc.elapsed < -2*time.Second { |
| 27 | tc.seconds = int64(testPolicy().AttemptSeconds) |
| 28 | } |
| 29 | if usage.Reserved.Seconds != reserved || usage.Spent.Seconds != tc.seconds { |
| 30 | t.Fatalf("settled %+v, want %d seconds", usage, tc.seconds) |
| 31 | } |
| 32 | e.clock.Advance(time.Hour) |
| 33 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| 34 | if periodUsage(e, 1) != usage { |
| 35 | t.Fatal("retry changed settlement") |
| 36 | } |
| 37 | }) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestContainRequiresOpenTimeRow(t *testing.T) { |
| 42 | for _, corrupt := range []string{"DELETE FROM time_reservations WHERE attempt_id=?", "UPDATE time_reservations SET state='settled' WHERE attempt_id=?"} { |
| 43 | t.Run(corrupt, func(t *testing.T) { |
| 44 | e := newEnv(t) |
| 45 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 46 | scope, h := periodJob(e, 1) |
| 47 | e.clock.Advance(300 * time.Second) |
| 48 | _, err := e.store.db.Exec(corrupt, h.Attempt) |
| 49 | must(t, err) |
| 50 | wantCode(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor"), CodeState) |
| 51 | attempts, err := e.store.Attempts(e.ctx, scope) |
| 52 | must(t, err) |
| 53 | if len(attempts) != 1 || attempts[0].Containment != ContainmentPending || attempts[0].State != AttemptActive { |
| 54 | t.Fatal("failed settlement changed attempt", attempts) |
| 55 | } |
| 56 | steps, err := e.store.Steps(e.ctx, scope) |
| 57 | must(t, err) |
| 58 | if steps[0].Holder != h.Attempt || steps[0].State != StepRunning { |
| 59 | t.Fatal("failed settlement released step") |
| 60 | } |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestTimeMigrationAfterClockStep(t *testing.T) { |
| 66 | e := newEnv(t) |
| 67 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 68 | h := e.claim(scope, "reproduce", "r", "ws") |
| 69 | _, err := e.store.Cancel(e.ctx, scope, "owner", "stop") |
| 70 | must(t, err) |
| 71 | e.clock.Advance(time.Second) |
| 72 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "s")) |
| 73 | must(t, e.store.Close()) |
| 74 | e.store = nil |
| 75 | db, err := sql.Open("sqlite", e.path) |
| 76 | must(t, err) |
| 77 | defer db.Close() |
| 78 | _, err = db.Exec("UPDATE attempts SET stop_acked=? WHERE id=?", ts(e.clock.Now().Add(-time.Minute)), h.Attempt) |
| 79 | must(t, err) |
| 80 | dropRecoverySchema(t, db) |
| 81 | 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"} { |
| 82 | _, err = db.Exec(q) |
| 83 | must(t, err) |
| 84 | } |
| 85 | must(t, db.Close()) |
| 86 | e.open() |
| 87 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 88 | if usage := periodUsage(e, 0); usage.Spent.Seconds != int64(testPolicy().AttemptSeconds) || usage.Reserved.Seconds != 0 { |
| 89 | t.Fatal("migrated backwards time", usage) |
| 90 | } |
| 91 | var state string |
| 92 | var seconds int64 |
| 93 | must(t, e.store.db.QueryRow("SELECT state, settled_seconds FROM time_reservations WHERE attempt_id=?", h.Attempt).Scan(&state, &seconds)) |
| 94 | if state != "settled" || seconds != int64(testPolicy().AttemptSeconds) { |
| 95 | t.Fatal("migrated settlement missing", state, seconds) |
| 96 | } |
| 97 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "s")) |
| 98 | } |
| 99 | |