| new file mode 100644 |
| index 00000000..ed02c349 |
| --- /dev/null |
| +++ b/internal/equipo/team/settletime_test.go |
| @@ -0,0 +1,93 @@ |
| +package team |
| + |
| +import ( |
| + "database/sql" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestContainTimeAfterClockStep(t *testing.T) { |
| + for _, tc := range []struct { |
| + elapsed time.Duration |
| + seconds int64 |
| + }{ |
| + {-time.Minute, 0}, {-time.Nanosecond, 0}, {0, 0}, {time.Nanosecond, 1}, {1501 * time.Second, 1501}, |
| + } { |
| + t.Run(tc.elapsed.String(), func(t *testing.T) { |
| + e := newEnv(t) |
| + must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| + scope, h := periodJob(e, 1) |
| + _, err := e.store.Cancel(e.ctx, scope, "owner", "stop") |
| + must(t, err) |
| + e.clock.Advance(tc.elapsed) |
| + must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| + usage := periodUsage(e, 1) |
| + if usage.Reserved.Seconds != 0 || usage.Spent.Seconds != tc.seconds { |
| + t.Fatalf("settled %+v, want %d seconds", usage, tc.seconds) |
| + } |
| + e.clock.Advance(time.Hour) |
| + must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| + if periodUsage(e, 1) != usage { |
| + t.Fatal("retry changed settlement") |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestContainRequiresOpenTimeRow(t *testing.T) { |
| + for _, corrupt := range []string{"DELETE FROM time_reservations WHERE attempt_id=?", "UPDATE time_reservations SET state='settled' WHERE attempt_id=?"} { |
| + t.Run(corrupt, func(t *testing.T) { |
| + e := newEnv(t) |
| + must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| + scope, h := periodJob(e, 1) |
| + e.clock.Advance(300 * time.Second) |
| + _, err := e.store.db.Exec(corrupt, h.Attempt) |
| + must(t, err) |
| + wantCode(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor"), CodeState) |
| + attempts, err := e.store.Attempts(e.ctx, scope) |
| + must(t, err) |
| + if len(attempts) != 1 || attempts[0].Containment != ContainmentPending || attempts[0].State != AttemptActive { |
| + t.Fatal("failed settlement changed attempt", attempts) |
| + } |
| + steps, err := e.store.Steps(e.ctx, scope) |
| + must(t, err) |
| + if steps[0].Holder != h.Attempt || steps[0].State != StepRunning { |
| + t.Fatal("failed settlement released step") |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestTimeMigrationAfterClockStep(t *testing.T) { |
| + e := newEnv(t) |
| + _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| + h := e.claim(scope, "reproduce", "r", "ws") |
| + _, err := e.store.Cancel(e.ctx, scope, "owner", "stop") |
| + must(t, err) |
| + e.clock.Advance(time.Second) |
| + must(t, e.store.Contain(e.ctx, scope, h.Attempt, "s")) |
| + must(t, e.store.Close()) |
| + e.store = nil |
| + db, err := sql.Open("sqlite", e.path) |
| + must(t, err) |
| + defer db.Close() |
| + _, err = db.Exec("UPDATE attempts SET stop_acked=? WHERE id=?", ts(e.clock.Now().Add(-time.Minute)), h.Attempt) |
| + must(t, err) |
| + 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"} { |
| + _, err = db.Exec(q) |
| + must(t, err) |
| + } |
| + must(t, db.Close()) |
| + e.open() |
| + must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| + if usage := periodUsage(e, 0); usage != (PeriodUsage{}) { |
| + t.Fatal("migrated backwards time", usage) |
| + } |
| + var state string |
| + var seconds int64 |
| + must(t, e.store.db.QueryRow("SELECT state, settled_seconds FROM time_reservations WHERE attempt_id=?", h.Attempt).Scan(&state, &seconds)) |
| + if state != "settled" || seconds != 0 { |
| + t.Fatal("migrated settlement missing", state, seconds) |
| + } |
| + must(t, e.store.Contain(e.ctx, scope, h.Attempt, "s")) |
| +} |