|
| 1 | +package state_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stablekernel/crucible/state" |
| 10 | +) |
| 11 | + |
| 12 | +// txnCtx is the value context for the failed-Fire transactionality regression. It |
| 13 | +// carries a balance an entry assign would fold so a rolled-back Fire is provably a |
| 14 | +// context no-op, and a JSON-marshalable shape so the snapshot round-trip works with |
| 15 | +// the default codec. |
| 16 | +type txnCtx struct { |
| 17 | + Balance int `json:"balance"` |
| 18 | +} |
| 19 | + |
| 20 | +// TestFire_FailedEntryAction_RollsBackConfiguration pins the full-transactionality |
| 21 | +// contract: when a transition's ENTRY action fails, the failed Fire is a no-op on |
| 22 | +// the instance's persisted internal state. Config, current state, and context are |
| 23 | +// all left at their pre-Fire values, FireResult.NewState reports the ORIGINAL state |
| 24 | +// (not the abandoned target), and a snapshot taken afterward round-trips to an |
| 25 | +// instance identical to one that never Fired. |
| 26 | +// |
| 27 | +// The machine moves off -> target on "go"; target's OnEntry action errors. Before |
| 28 | +// the fix the kernel advanced i.current/i.config to "target" and reported |
| 29 | +// NewState=target while leaving the context rolled back — a split. The fix rolls |
| 30 | +// the configuration back together with the (already transactional) context and |
| 31 | +// effects. |
| 32 | +func TestFire_FailedEntryAction_RollsBackConfiguration(t *testing.T) { |
| 33 | + boom := errors.New("entry boom") |
| 34 | + bump := func(in state.AssignCtx[txnCtx]) txnCtx { c := in.Entity; c.Balance += 100; return c } |
| 35 | + fail := func(state.ActionCtx[txnCtx]) (state.Effect, error) { return nil, boom } |
| 36 | + |
| 37 | + build := func() *state.Machine[string, string, txnCtx] { |
| 38 | + return state.Forge[string, string, txnCtx]("txn"). |
| 39 | + Action("explode", fail). |
| 40 | + Reducer("bump", bump). |
| 41 | + State("off"). |
| 42 | + Transition("off").On("go").GoTo("target"). |
| 43 | + State("target").OnEntry("explode").OnEntryAssign("bump"). |
| 44 | + Initial("off"). |
| 45 | + CurrentStateFn(func(txnCtx) string { return "off" }). |
| 46 | + Quench() |
| 47 | + } |
| 48 | + |
| 49 | + m := build() |
| 50 | + ctx := context.Background() |
| 51 | + |
| 52 | + // A never-Fired control instance and its snapshot: the post-failure instance must |
| 53 | + // be indistinguishable from this one. |
| 54 | + control := m.Cast(txnCtx{Balance: 1}, state.WithInitialState("off")) |
| 55 | + wantSnap := control.Snapshot() |
| 56 | + |
| 57 | + inst := m.Cast(txnCtx{Balance: 1}, state.WithInitialState("off")) |
| 58 | + |
| 59 | + res := inst.Fire(ctx, "go") |
| 60 | + if res.Err == nil { |
| 61 | + t.Fatalf("expected the failed entry action to error, got nil (state=%v)", res.NewState) |
| 62 | + } |
| 63 | + if !errors.Is(res.Err, boom) { |
| 64 | + t.Fatalf("err = %v, want it to wrap %v", res.Err, boom) |
| 65 | + } |
| 66 | + |
| 67 | + // (b) FireResult reports the actual resulting (original) state. |
| 68 | + if res.NewState != "off" { |
| 69 | + t.Fatalf("NewState = %v on a failed Fire; want the original state off (no half-advanced config)", res.NewState) |
| 70 | + } |
| 71 | + if got := inst.Current(); got != "off" { |
| 72 | + t.Fatalf("instance current = %v after a failed Fire; want off", got) |
| 73 | + } |
| 74 | + if cfg := inst.Configuration(); len(cfg) != 1 || cfg[0] != "off" { |
| 75 | + t.Fatalf("configuration = %v after a failed Fire; want [off]", cfg) |
| 76 | + } |
| 77 | + |
| 78 | + // (c) Context unchanged: the entry assign that follows the failed action never |
| 79 | + // commits, and there is no separate rollback to verify since context was already |
| 80 | + // transactional — but assert it to lock the whole-instance no-op. |
| 81 | + if got := inst.Entity().Balance; got != 1 { |
| 82 | + t.Fatalf("context balance = %d after a failed Fire; want 1 (unchanged)", got) |
| 83 | + } |
| 84 | + |
| 85 | + // (d) Snapshot the post-failure instance: it must equal a snapshot of the |
| 86 | + // never-Fired control, so nothing split was persisted. |
| 87 | + gotSnap := inst.Snapshot() |
| 88 | + if !reflect.DeepEqual(gotSnap, wantSnap) { |
| 89 | + t.Fatalf("post-failure snapshot != never-Fired snapshot:\n got = %+v\nwant = %+v", gotSnap, wantSnap) |
| 90 | + } |
| 91 | + |
| 92 | + // The control instance can still Fire cleanly through a non-failing path, proving |
| 93 | + // the rolled-back instance is genuinely re-fireable from its original state. |
| 94 | + clean := build0(t) |
| 95 | + if r := clean.Fire(ctx, "go"); r.Err != nil { |
| 96 | + t.Fatalf("clean machine should advance: %v", r.Err) |
| 97 | + } else if r.NewState != "target" { |
| 98 | + t.Fatalf("clean machine NewState = %v, want target", r.NewState) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// build0 casts a no-fail variant of the txn machine for the re-fireability control. |
| 103 | +func build0(t *testing.T) *state.Instance[string, string, txnCtx] { |
| 104 | + t.Helper() |
| 105 | + m := state.Forge[string, string, txnCtx]("txn-ok"). |
| 106 | + State("off"). |
| 107 | + Transition("off").On("go").GoTo("target"). |
| 108 | + State("target"). |
| 109 | + Initial("off"). |
| 110 | + CurrentStateFn(func(txnCtx) string { return "off" }). |
| 111 | + Quench() |
| 112 | + return m.Cast(txnCtx{Balance: 1}, state.WithInitialState("off")) |
| 113 | +} |
| 114 | + |
| 115 | +// TestFire_FailedRegionEntry_RollsBackEarlierRegion covers the parallel-region |
| 116 | +// split: regions fire in declaration order within one macrostep, each committing its |
| 117 | +// own config/context fold to the live instance. When a LATER region's entry action |
| 118 | +// fails, an EARLIER region's already-committed transition must roll back too — a |
| 119 | +// partial-region commit is the parallel analog of the half-advanced config. |
| 120 | +// |
| 121 | +// r1 takes r1a -> r1b on "go" (committing a config swap and a context fold); r2 then |
| 122 | +// takes r2a -> r2b on "go" whose entry action errors. The whole Fire must be a no-op: |
| 123 | +// r1 stays at r1a and the r1 fold is discarded. |
| 124 | +func TestFire_FailedRegionEntry_RollsBackEarlierRegion(t *testing.T) { |
| 125 | + boom := errors.New("region entry boom") |
| 126 | + bump := func(in state.AssignCtx[txnCtx]) txnCtx { c := in.Entity; c.Balance += 10; return c } |
| 127 | + fail := func(state.ActionCtx[txnCtx]) (state.Effect, error) { return nil, boom } |
| 128 | + |
| 129 | + m := state.Forge[string, string, txnCtx]("txn-par"). |
| 130 | + Action("explode", fail). |
| 131 | + Reducer("bump", bump). |
| 132 | + SuperState("live"). |
| 133 | + Region("r1"). |
| 134 | + Initial("r1a"). |
| 135 | + SubState("r1a").On("go").GoTo("r1b").Assign("bump"). |
| 136 | + SubState("r1b"). |
| 137 | + EndRegion(). |
| 138 | + Region("r2"). |
| 139 | + Initial("r2a"). |
| 140 | + SubState("r2a").On("go").GoTo("r2b"). |
| 141 | + SubState("r2b").OnEntry("explode"). |
| 142 | + EndRegion(). |
| 143 | + EndSuperState(). |
| 144 | + Initial("live"). |
| 145 | + Quench() |
| 146 | + |
| 147 | + ctx := context.Background() |
| 148 | + control := m.Cast(txnCtx{Balance: 1}, state.WithInitialState("live")) |
| 149 | + wantSnap := control.Snapshot() |
| 150 | + |
| 151 | + inst := m.Cast(txnCtx{Balance: 1}, state.WithInitialState("live")) |
| 152 | + res := inst.Fire(ctx, "go") |
| 153 | + if res.Err == nil { |
| 154 | + t.Fatalf("expected the failed region entry to error, got nil (state=%v)", res.NewState) |
| 155 | + } |
| 156 | + if !errors.Is(res.Err, boom) { |
| 157 | + t.Fatalf("err = %v, want it to wrap %v", res.Err, boom) |
| 158 | + } |
| 159 | + |
| 160 | + // r1 must not have committed: configuration still holds r1a (and r2a), context |
| 161 | + // fold discarded. |
| 162 | + cfg := inst.Configuration() |
| 163 | + if !containsLeaf(cfg, "r1a") || containsLeaf(cfg, "r1b") { |
| 164 | + t.Fatalf("configuration = %v after a failed parallel Fire; r1 must stay at r1a", cfg) |
| 165 | + } |
| 166 | + if got := inst.Entity().Balance; got != 1 { |
| 167 | + t.Fatalf("context balance = %d after a failed parallel Fire; want 1 (r1 fold rolled back)", got) |
| 168 | + } |
| 169 | + |
| 170 | + gotSnap := inst.Snapshot() |
| 171 | + if !reflect.DeepEqual(gotSnap, wantSnap) { |
| 172 | + t.Fatalf("post-failure parallel snapshot != never-Fired snapshot:\n got = %+v\nwant = %+v", gotSnap, wantSnap) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +// containsLeaf reports whether leaf is present in cfg. |
| 177 | +func containsLeaf(cfg []string, leaf string) bool { |
| 178 | + for _, s := range cfg { |
| 179 | + if s == leaf { |
| 180 | + return true |
| 181 | + } |
| 182 | + } |
| 183 | + return false |
| 184 | +} |
0 commit comments