| 1 | package rootparticipation |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestHoldUpdateRefusesAcquisitionsWithoutACensus(t *testing.T) { |
| 11 | l := censusFixture(t) |
| 12 | refused := errors.New("census refused") |
| 13 | l.VerifyLaunches = func(context.Context, uint64) error { return refused } |
| 14 | // BeginUpdate cannot hold: its census refuses and it withdraws the record. |
| 15 | if err := l.BeginUpdate(t.Context(), "tx-1"); !errors.Is(err, refused) { |
| 16 | t.Fatalf("BeginUpdate = %v", err) |
| 17 | } |
| 18 | if err := l.Acquire(t.Context(), func() error { return nil }); err != nil { |
| 19 | t.Fatalf("fixture: acquisition refused before the hold: %v", err) |
| 20 | } |
| 21 | if err := l.HoldUpdate(t.Context(), "tx-1"); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if id, err := l.UpdateID(); err != nil || id != "tx-1" { |
| 25 | t.Fatalf("held record = %q, %v", id, err) |
| 26 | } |
| 27 | if err := l.Acquire(t.Context(), func() error { return nil }); !errors.Is(err, ErrUnavailable) { |
| 28 | t.Fatalf("acquisition under the hold = %v, want refused", err) |
| 29 | } |
| 30 | // A second hold keeps the record as it is; only EndUpdateWith, with its |
| 31 | // census, removes it. |
| 32 | if err := l.HoldUpdate(t.Context(), "tx-2"); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if id, _ := l.UpdateID(); id != "tx-1" { |
| 36 | t.Fatalf("a second hold replaced the record with %q", id) |
| 37 | } |
| 38 | if err := l.EndUpdateWith(t.Context(), "tx-1", nil); !errors.Is(err, refused) { |
| 39 | t.Fatalf("EndUpdateWith under a refusing census = %v", err) |
| 40 | } |
| 41 | l.VerifyLaunches = func(context.Context, uint64) error { return nil } |
| 42 | if err := l.EndUpdateWith(t.Context(), "tx-1", nil); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if err := l.Acquire(t.Context(), func() error { return nil }); err != nil { |
| 46 | t.Fatalf("acquisition after settlement = %v", err) |
| 47 | } |
| 48 | if err := l.HoldUpdate(t.Context(), "bad id"); !errors.Is(err, ErrUnavailable) { |
| 49 | t.Fatalf("invalid id = %v", err) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestHoldUpdateLeavesAPendingAcquisitionCensusAlone(t *testing.T) { |
| 54 | l := censusFixture(t) |
| 55 | b, _ := json.Marshal(Census{Schema: 1, Box: "box", Enrollment: "en", Migration: 1, Revision: 2, Complete: true, LaunchRevision: 1, Pending: "acquisition"}) |
| 56 | if err := l.State.Write("census.json", b); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if err := l.HoldUpdate(t.Context(), "tx-1"); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if _, err := l.State.Read("update.json"); err == nil { |
| 63 | t.Fatal("hold wrote a record over a pending acquisition") |
| 64 | } |
| 65 | if err := l.Acquire(t.Context(), func() error { return nil }); err == nil { |
| 66 | t.Fatal("a pending acquisition census accepted a new acquisition") |
| 67 | } |
| 68 | } |
| 69 | |