| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | type noPublicationEffect struct{ *FakeForge } |
| 9 | |
| 10 | func (noPublicationEffect) Execute(context.Context, Dispatch) (Receipt, error) { |
| 11 | return Receipt{}, ErrNotDispatched |
| 12 | } |
| 13 | func (noPublicationEffect) Reconcile(context.Context, Dispatch) (ReconcileResult, error) { |
| 14 | return ReconcileResult{}, ErrNotDispatched |
| 15 | } |
| 16 | |
| 17 | func TestPublishNoEffectSettlement(t *testing.T) { |
| 18 | for _, recover := range []bool{false, true} { |
| 19 | t.Run(map[bool]string{false: "dispatch", true: "reconcile"}[recover], func(t *testing.T) { |
| 20 | g := newGitEnv(t) |
| 21 | g.build() |
| 22 | g.review() |
| 23 | g.verify(true) |
| 24 | g.store.adapters[OpForgePublish] = noPublicationEffect{NewFakeForge()} |
| 25 | h := g.claimStep("publish") |
| 26 | p, err := g.store.Publication(g.ctx, g.scope, h) |
| 27 | must(t, err) |
| 28 | id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(p), Reserve: Usage{Calls: 1}}) |
| 29 | must(t, err) |
| 30 | if recover { |
| 31 | _, err = g.store.Admit(g.ctx, g.scope, id) |
| 32 | must(t, err) |
| 33 | must(t, g.store.Lost(g.ctx, g.scope, id, "reply lost")) |
| 34 | _, err = g.store.Reconcile(g.ctx, g.scope, id) |
| 35 | must(t, err) |
| 36 | } else { |
| 37 | r, e := g.store.Dispatch(g.ctx, g.scope, id) |
| 38 | must(t, e) |
| 39 | if r.Outcome != OutcomeFailure { |
| 40 | t.Fatal("no dispatch was not failed", r) |
| 41 | } |
| 42 | } |
| 43 | a, err := g.store.GetAction(g.ctx, g.scope, id) |
| 44 | must(t, err) |
| 45 | if a.State != ActionFailed || a.Receipt == nil || a.Receipt.Usage == nil || *a.Receipt.Usage != (Usage{}) { |
| 46 | t.Fatalf("no dispatch stayed charged or unknown: %+v", a) |
| 47 | } |
| 48 | rows, err := g.store.Reservations(g.ctx, g.scope) |
| 49 | must(t, err) |
| 50 | for _, r := range rows { |
| 51 | if r.ID == a.Reservation && recover { |
| 52 | if r.State != ReservationSettled || r.Settled != r.Reserved { |
| 53 | t.Fatal("unknown charge refunded", r) |
| 54 | } |
| 55 | continue |
| 56 | } |
| 57 | if r.ID == a.Reservation && (r.State != ReservationReleased || r.Settled.Calls != 0 || r.Settled.CostMicros != 0) { |
| 58 | t.Fatalf("no dispatch retained charge: %+v", r) |
| 59 | } |
| 60 | } |
| 61 | must(t, g.store.Record(g.ctx, g.scope, id, *a.Receipt)) |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestPublishZeroReceiptDoesNotProveNoDispatch(t *testing.T) { |
| 67 | g := newGitEnv(t) |
| 68 | g.build() |
| 69 | g.review() |
| 70 | g.verify(true) |
| 71 | h := g.claimStep("publish") |
| 72 | p, err := g.store.Publication(g.ctx, g.scope, h) |
| 73 | must(t, err) |
| 74 | id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(p), Reserve: Usage{Calls: 1}}) |
| 75 | must(t, err) |
| 76 | d, err := g.store.Admit(g.ctx, g.scope, id) |
| 77 | must(t, err) |
| 78 | must(t, g.store.Record(g.ctx, g.scope, id, notDispatchedReceipt(d))) |
| 79 | rows, err := g.store.Reservations(g.ctx, g.scope) |
| 80 | must(t, err) |
| 81 | if len(rows) != 1 || rows[0].State != ReservationSettled || rows[0].Settled.Calls != 1 { |
| 82 | t.Fatal("ordinary zero receipt released charge", rows) |
| 83 | } |
| 84 | } |
| 85 | |