| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/hostclaim" |
| 9 | "github.com/carlosframework/platform/internal/provision" |
| 10 | "github.com/carlosframework/platform/internal/release" |
| 11 | ) |
| 12 | |
| 13 | type plan9Fix2PhaseStore struct { |
| 14 | release.Store |
| 15 | phases []string |
| 16 | key string |
| 17 | } |
| 18 | |
| 19 | func (s *plan9Fix2PhaseStore) Put(ctx context.Context, key string, body []byte, etag string) (string, error) { |
| 20 | next, err := s.Store.Put(ctx, key, body, etag) |
| 21 | if err == nil && key == s.key { |
| 22 | var c hostclaim.Claim |
| 23 | if json.Unmarshal(body, &c) == nil { |
| 24 | s.phases = append(s.phases, c.Op.Phase) |
| 25 | } |
| 26 | } |
| 27 | return next, err |
| 28 | } |
| 29 | |
| 30 | func TestPlan9Fix2LandingCatchUpPersistsEachAdjacentStep(t *testing.T) { |
| 31 | t.Parallel() |
| 32 | h := newLandingHarness(t) |
| 33 | h.flip(t) |
| 34 | h.lw = &movesWorker{cfg: h.landingCfg(t)} |
| 35 | key, err := hostclaim.Key(p1Host) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | store := &plan9Fix2PhaseStore{Store: h.s, key: key} |
| 40 | rec := h.read(t, p1Host) |
| 41 | rec.Move.LandingPhase = provision.LandProbed |
| 42 | run := landingRun{w: h.lw, s: store, rec: rec, g: provision.MoveGuard{ID: p1ID, Incarnation: h.inc}} |
| 43 | before := h.claim(t) |
| 44 | if _, err = run.syncClaimPhase(t.Context()); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | order := provision.LandingOrder() |
| 48 | if len(store.phases) != len(order)-1 { |
| 49 | t.Fatalf("catch-up skipped durable phase transitions: %v", store.phases) |
| 50 | } |
| 51 | for i, phase := range store.phases { |
| 52 | if phase != order[i+1] { |
| 53 | t.Fatalf("transition %d: %s", i, phase) |
| 54 | } |
| 55 | } |
| 56 | after := h.claim(t) |
| 57 | if before.Holder.Session != after.Holder.Session || before.Holder.Stamp != after.Holder.Stamp || before.Holder.Generation != after.Holder.Generation { |
| 58 | t.Fatal("catch-up changed admitted writer") |
| 59 | } |
| 60 | } |
| 61 | |