| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func TestUnknownChargesStayInTheirMonth(t *testing.T) { |
| 10 | for _, outcome := range []string{"completed", "lost"} { |
| 11 | t.Run(outcome, func(t *testing.T) { |
| 12 | e := newEnv(t) |
| 13 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 14 | s, h := runnerJob(e, runnerPolicy(t, "subscription")) |
| 15 | month := e.clock.Now() |
| 16 | id, err := e.store.Intend(e.ctx, s, h, intent("turn", Usage{})) |
| 17 | must(t, err) |
| 18 | _, err = e.store.Admit(e.ctx, s, id) |
| 19 | must(t, err) |
| 20 | if outcome == "lost" { |
| 21 | must(t, e.store.Lost(e.ctx, s, id, "unknown")) |
| 22 | } else { |
| 23 | must(t, e.store.Record(e.ctx, s, id, Receipt{ID: "done", Outcome: OutcomeSuccess})) |
| 24 | } |
| 25 | must(t, e.store.ContainUntrusted(e.ctx, s, h)) |
| 26 | must(t, e.store.Pause(e.ctx, "recovery")) |
| 27 | must(t, e.store.RecordRecoveryCharge(e.ctx, RecoveryCharge{Key: "missing", Account: acctA, Project: 1, Reserved: PeriodLimits{Calls: 2, Seconds: 10}}, "owner", "missing history")) |
| 28 | for _, project := range []ProjectID{0, 1} { |
| 29 | current, err := e.store.PeriodUsage(e.ctx, acctA, project, month) |
| 30 | must(t, err) |
| 31 | if current.Spent.Calls != 3 || current.Spent.InputTokens != 100 || current.Spent.OutputTokens != 50 || current.Spent.Seconds != 1210 || current.Reserved != (PeriodLimits{}) { |
| 32 | t.Errorf("unknown charge not settled in its month: %+v", current) |
| 33 | } |
| 34 | next, err := e.store.PeriodUsage(e.ctx, acctA, project, month.AddDate(0, 1, 0)) |
| 35 | must(t, err) |
| 36 | if next != (PeriodUsage{}) { |
| 37 | t.Errorf("unknown charge leaked into next month: %+v", next) |
| 38 | } |
| 39 | } |
| 40 | must(t, e.store.read(e.ctx, func(tx *sql.Tx) error { |
| 41 | var state, by string |
| 42 | if err := tx.QueryRow(`SELECT state,settled_by FROM reservations`).Scan(&state, &by); err != nil { |
| 43 | return err |
| 44 | } |
| 45 | if state != "settled" || by != "unknown" { |
| 46 | t.Errorf("unknown usage not marked: %s %s", state, by) |
| 47 | } |
| 48 | var unknown int |
| 49 | if err := tx.QueryRow(`SELECT elapsed_unknown FROM time_reservations`).Scan(&unknown); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | if unknown != 1 { |
| 53 | t.Error("elapsed uncertainty erased") |
| 54 | } |
| 55 | return nil |
| 56 | })) |
| 57 | }) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestOpenChargesStayInTheirMonth(t *testing.T) { |
| 62 | e := newEnv(t) |
| 63 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 64 | s, h := runnerJob(e, runnerPolicy(t, "subscription")) |
| 65 | _, err := e.store.Intend(e.ctx, s, h, intent("pending", Usage{})) |
| 66 | must(t, err) |
| 67 | next, err := e.store.PeriodUsage(e.ctx, acctA, 0, e.clock.Now().Add(32*24*time.Hour)) |
| 68 | must(t, err) |
| 69 | if next != (PeriodUsage{}) { |
| 70 | t.Fatal("old open reservations reduce new month", next) |
| 71 | } |
| 72 | } |
| 73 | |