test: stale-holder table covers another server and every transition

Each transition is refused for an older epoch of the same server and
for the same epoch held by another server, in idle, snap and clean; the
table must name every Head method returning (Head, bool, error), so a
new transition fails until it is added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-24 13:12:45 -08:00
1 parent 43811e9907
commit f31db828e7
1 file changed
+42 -14
@@ -1,24 +1,52 @@
package layout
import "testing"
import (
"reflect"
"sort"
"testing"
)
// Every pure transition refuses a caller that is not the holder at HEAD's
// epoch (Claim refuses an older epoch or the same epoch of another server).
// epoch: an older epoch of the same server, and the same epoch held by
// another server, in idle, snap and clean. The table must name every
// method of Head that returns (Head, bool, error), so a new transition
// fails here until it is added.
func TestTransitions_RefuseAStaleHolder(t *testing.T) {
stale := HolderID{Epoch: 2, Holder: "srv-a"}
rn := RootName{Hash: Sum([]byte("r"))}
for name, step := range map[string]func(Head) error{
"Begin": func(h Head) error { _, _, err := h.Begin(stale, "s"); return err },
"Abort": func(h Head) error { _, _, err := h.Abort(stale, "s"); return err },
"Commit": func(h Head) error { _, _, err := h.Commit(stale, "s", rn, 1, ""); return err },
"BeginClean": func(h Head) error { _, _, err := h.BeginClean(stale, "c"); return err },
"EndClean": func(h Head) error { _, _, err := h.EndClean(stale, "c"); return err },
"Claim": func(h Head) error { _, _, err := h.Claim(stale); return err },
} {
for _, h := range []Head{idleHead(), snapHead("s"), cleanHead("c")} {
if err := step(h); err == nil {
t.Errorf("%s by epoch 2 over a HEAD at epoch 3 (%s) succeeded", name, h.Activity.Kind)
steps := map[string]func(Head, HolderID) error{
"Begin": func(h Head, c HolderID) error { _, _, err := h.Begin(c, "s"); return err },
"Abort": func(h Head, c HolderID) error { _, _, err := h.Abort(c, "s"); return err },
"Commit": func(h Head, c HolderID) error { _, _, err := h.Commit(c, "s", rn, 1, ""); return err },
"BeginClean": func(h Head, c HolderID) error { _, _, err := h.BeginClean(c, "c"); return err },
"EndClean": func(h Head, c HolderID) error { _, _, err := h.EndClean(c, "c"); return err },
"Claim": func(h Head, c HolderID) error { _, _, err := h.Claim(c); return err },
}
callers := map[string]HolderID{
"older epoch, same server": {Epoch: 2, Holder: "srv-a"},
"same epoch, another server": {Epoch: 3, Holder: "srv-b"},
}
for name, step := range steps {
for who, c := range callers {
for _, h := range []Head{idleHead(), snapHead("s"), cleanHead("c")} {
if err := step(h, c); err == nil {
t.Errorf("%s by %s over a HEAD at epoch 3 held by srv-a (%s) succeeded", name, who, h.Activity.Kind)
}
}
}
}
headT, boolT := reflect.TypeOf(Head{}), reflect.TypeOf(false)
errT := reflect.TypeOf((*error)(nil)).Elem()
var missing []string
for i := 0; i < headT.NumMethod(); i++ {
m := headT.Method(i).Type
if m.NumOut() == 3 && m.Out(0) == headT && m.Out(1) == boolT && m.Out(2) == errT {
if _, ok := steps[headT.Method(i).Name]; !ok {
missing = append(missing, headT.Method(i).Name)
}
}
}
sort.Strings(missing)
if len(missing) != 0 {
t.Fatalf("transitions not in the stale-holder table: %v", missing)
}
}