Merge branch 'feat/oss-snap-claim' into feat/oss-snap-clean-session

This commit is contained in:
pingqiu committed 2026-11-24 12:55:07 -08:00
commit c4dfbac39b
3 files changed
+196 -96

No files matched your search

@@ -12,83 +12,185 @@ import (
"testing"
)
// funcCalls maps each function of the package's files (tests or not) to
// the names it calls.
func funcCalls(t *testing.T, tests bool) map[string]map[string]bool {
// pkgLevel is the owner of a reference outside any function body: a
// package-level var or const initializer.
const pkgLevel = "<package>"
// refs maps every name used in the given sources (a selector's name, as in
// st.UpdateHead, called or not, or a plain identifier, as in runTransition)
// to the functions that use it: a method value, an alias, a call and a
// package-level initializer all count. Declarations are not uses: function
// and type names, interface method names and struct field names.
func refs(t *testing.T, srcs map[string]string) map[string]map[string]bool {
t.Helper()
files, err := filepath.Glob("*.go")
if err != nil {
t.Fatal(err)
}
out := map[string]map[string]bool{}
for _, f := range files {
if strings.HasSuffix(f, "_test.go") != tests {
continue
add := func(name, owner string) {
if out[name] == nil {
out[name] = map[string]bool{}
}
src, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
file, err := parser.ParseFile(token.NewFileSet(), f, src, 0)
out[name][owner] = true
}
visit := func(n ast.Node, owner string) {
ast.Inspect(n, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.SelectorExpr:
add(x.Sel.Name, owner)
ast.Inspect(x.X, func(n ast.Node) bool {
if id, ok := n.(*ast.Ident); ok {
add(id.Name, owner)
}
return true
})
return false
case *ast.Ident:
add(x.Name, owner)
}
return true
})
}
for name, src := range srcs {
file, err := parser.ParseFile(token.NewFileSet(), name, src, 0)
if err != nil {
t.Fatal(err)
}
for _, d := range file.Decls {
fn, ok := d.(*ast.FuncDecl)
if !ok || fn.Body == nil {
continue
}
name := fn.Name.Name
if fn.Recv != nil && len(fn.Recv.List) == 1 {
switch x := fn.Recv.List[0].Type.(type) {
case *ast.Ident:
name = x.Name + "." + name
case *ast.StarExpr:
name = x.X.(*ast.Ident).Name + "." + name
switch d := d.(type) {
case *ast.FuncDecl:
if d.Body == nil {
continue
}
}
calls := map[string]bool{}
ast.Inspect(fn.Body, func(n ast.Node) bool {
if c, ok := n.(*ast.CallExpr); ok {
switch x := c.Fun.(type) {
case *ast.SelectorExpr:
calls[x.Sel.Name] = true
owner := d.Name.Name
if d.Recv != nil && len(d.Recv.List) == 1 {
switch x := d.Recv.List[0].Type.(type) {
case *ast.Ident:
calls[x.Name] = true
owner = x.Name + "." + owner
case *ast.StarExpr:
owner = x.X.(*ast.Ident).Name + "." + owner
}
}
return true
})
out[name] = calls
visit(d.Body, owner)
case *ast.GenDecl:
for _, s := range d.Specs {
if v, ok := s.(*ast.ValueSpec); ok {
for _, e := range v.Values {
visit(e, pkgLevel)
}
}
}
}
}
}
return out
}
// Guard enumeration for the facts this package writes. It holds at every
// step of the series without edits: a new writer, transition or remover
// fails it until it brings the tests named here.
func packageSources(t *testing.T, tests bool) map[string]string {
t.Helper()
files, err := filepath.Glob("*.go")
if err != nil {
t.Fatal(err)
}
out := map[string]string{}
for _, f := range files {
if strings.HasSuffix(f, "_test.go") != tests {
continue
}
b, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
out[f] = string(b)
}
return out
}
func owners(m map[string]bool) []string {
var out []string
for o := range m {
out = append(out, o)
}
sort.Strings(out)
return out
}
// guardFindings checks the package sources against the guard and returns
// every violation.
//
// - HEAD: the only UpdateHead call is in runTransition (read, one pure
// step, one conditional update); the store has no way to create a HEAD.
// - Transitions: every Run<X> through runTransition has a closure test
// (Test<X>_Closure) and an interrupt test (TestRun<X>_InterruptAtEveryStep).
// - Lease, claim half: only RunClaim calls OldLeaseEnded, and RunClaim,
// where it exists, calls it.
// - Pool: no function removes a store object yet; the removal that comes
// with cleaning must update this guard and bring its lease re-check.
func TestGuard_WritersTransitionsAndRemovers(t *testing.T) {
code := funcCalls(t, false)
tests := funcCalls(t, true)
var writers []string
for fn, cs := range code {
if cs["UpdateHead"] {
writers = append(writers, fn)
// - HEAD: UpdateHead is used only in runTransition (read, one pure step,
// one conditional update): no other call, method value, alias or
// package-level use. The store has no way to create a HEAD.
// - Transitions: runTransition is used only by exported Run functions,
// each with Test<X>_Closure and TestRun<X>_InterruptAtEveryStep.
// - Lease, claim half: OldLeaseEnded is used only in RunClaim, and
// RunClaim, where it exists, uses it.
// - Pool: no name starting with Delete or Remove is used yet; the
// removal that comes with cleaning must bring its lease re-check and
// its own row here.
//
// These are syntactic checks of this package's own files: they see any
// use of these names, but not a store effect reached through a name they
// do not list (for example reflection or another package's helper).
func guardFindings(code map[string]map[string]bool, tests map[string]bool) []string {
var bad []string
if got := owners(code["UpdateHead"]); !reflect.DeepEqual(got, []string{"runTransition"}) {
bad = append(bad, "UpdateHead used by "+strings.Join(got, ", ")+"; want only runTransition")
}
var runs []string
for _, fn := range owners(code["runTransition"]) {
x, ok := strings.CutPrefix(fn, "Run")
if !ok {
bad = append(bad, fn+" uses runTransition; transitions are exported Run functions")
continue
}
runs = append(runs, fn)
for _, want := range []string{"Test" + x + "_Closure", "TestRun" + x + "_InterruptAtEveryStep"} {
if !tests[want] {
bad = append(bad, "transition "+fn+" has no "+want)
}
}
}
sort.Strings(writers)
if !reflect.DeepEqual(writers, []string{"runTransition"}) {
t.Errorf("functions calling UpdateHead: %v; want only runTransition", writers)
if len(runs) == 0 {
bad = append(bad, "no transition uses runTransition: the scan is broken")
}
for _, fn := range owners(code["OldLeaseEnded"]) {
if fn != "RunClaim" {
bad = append(bad, fn+" uses OldLeaseEnded; only RunClaim claims")
}
}
if _, ok := code["RunClaim"]; ok && !code["OldLeaseEnded"]["RunClaim"] {
bad = append(bad, "RunClaim does not wait for the old lease")
}
for name, by := range code {
if strings.HasPrefix(name, "Delete") || strings.HasPrefix(name, "Remove") {
bad = append(bad, strings.Join(owners(by), ", ")+" uses "+name+": a store remover needs its lease re-check and its own guard row")
}
}
sort.Strings(bad)
return bad
}
// declared returns the names of the functions the sources declare.
func declared(t *testing.T, srcs map[string]string) map[string]bool {
t.Helper()
out := map[string]bool{}
for name, src := range srcs {
file, err := parser.ParseFile(token.NewFileSet(), name, src, 0)
if err != nil {
t.Fatal(err)
}
for _, d := range file.Decls {
if fn, ok := d.(*ast.FuncDecl); ok && fn.Recv == nil {
out[fn.Name.Name] = true
}
}
}
return out
}
func TestGuard_WritersTransitionsAndRemovers(t *testing.T) {
code := refs(t, packageSources(t, false))
tests := declared(t, packageSources(t, true))
if bad := guardFindings(code, tests); len(bad) != 0 {
t.Fatalf("guard: %s", strings.Join(bad, "\n"))
}
st := reflect.TypeOf((*HeadStore)(nil)).Elem()
var methods []string
@@ -96,41 +198,37 @@ func TestGuard_WritersTransitionsAndRemovers(t *testing.T) {
methods = append(methods, st.Method(i).Name)
}
if !reflect.DeepEqual(methods, []string{"ReadHead", "UpdateHead"}) {
t.Errorf("HeadStore methods %v; want ReadHead and UpdateHead only (no path creates a HEAD)", methods)
t.Fatalf("HeadStore methods %v; want ReadHead and UpdateHead only (no path creates a HEAD)", methods)
}
var runs []string
for fn, cs := range code {
if !cs["runTransition"] {
continue
}
runs = append(runs, fn)
x, ok := strings.CutPrefix(fn, "Run")
if !ok {
t.Errorf("%s calls runTransition; transitions are exported Run functions", fn)
continue
}
for _, want := range []string{"Test" + x + "_Closure", "TestRun" + x + "_InterruptAtEveryStep"} {
if _, ok := tests[want]; !ok {
t.Errorf("transition %s has no %s", fn, want)
}
}
}
if len(runs) == 0 {
t.Fatal("no transition found: the scan is broken")
}
for fn, cs := range code {
if cs["OldLeaseEnded"] && fn != "RunClaim" {
t.Errorf("%s calls OldLeaseEnded; only RunClaim claims", fn)
}
}
if cs, ok := code["RunClaim"]; ok && !cs["OldLeaseEnded"] {
t.Error("RunClaim does not wait for the old lease")
}
for fn, cs := range code {
for c := range cs {
if strings.HasPrefix(c, "Delete") || strings.HasPrefix(c, "Remove") {
t.Errorf("%s calls %s: a store remover needs its lease re-check and its own guard row", fn, c)
}
}
// Negative controls: each form of an extra HEAD writer, transition or
// remover, added to the package's real sources, is refused; the real
// sources alone pass (the test above).
func TestGuard_Controls(t *testing.T) {
tests := declared(t, packageSources(t, true))
for name, src := range map[string]string{
"direct call": `func w(ctx context.Context, st HeadStore, h Head) error { return st.UpdateHead(ctx, h.Gen, h) }`,
"method value alias": `func w(ctx context.Context, st HeadStore, h Head) error {
update := st.UpdateHead
return update(ctx, h.Gen, h)
}`,
"method expression": `func w(ctx context.Context, st HeadStore, h Head) error { return HeadStore.UpdateHead(st, ctx, h.Gen, h) }`,
"package-level var": `var w = HeadStore.UpdateHead`,
"package-level func lit": `var w = func(ctx context.Context, st HeadStore, h Head) error { return st.UpdateHead(ctx, h.Gen, h) }`,
"inline interface": `func w(st HeadStore) { var u interface{ UpdateHead(context.Context, uint64, Head) error } = st; _ = u }`,
"runTransition alias": `func w(ctx context.Context, st HeadStore) { f := runTransition; _, _ = f(ctx, st, nil) }`,
"untested transition": `func RunZz(ctx context.Context, st HeadStore) (Head, error) { return runTransition(ctx, st, nil) }`,
"lease wait outside claim": `func w(ctx context.Context, l LeaseWait) error { return l.OldLeaseEnded(ctx, 1) }`,
"remover": `type p interface{ DeleteObject(context.Context, string) error }
func w(ctx context.Context, x p) error { return x.DeleteObject(ctx, "o") }`,
"remover method value": `type p interface{ RemoveName(string) }
func w(x p) { f := x.RemoveName; f("o") }`,
} {
srcs := packageSources(t, false)
srcs["zz_control.go"] = "package layout\n\nimport \"context\"\n\nvar _ context.Context\n\n" + src + "\n"
if bad := guardFindings(refs(t, srcs), tests); len(bad) == 0 {
t.Errorf("control %q passed the guard", name)
}
}
}
@@ -65,7 +65,8 @@ type snapDoc struct {
}
// validName refuses a string the JSON encoding would not keep byte for
// byte (invalid UTF-8 becomes U+FFFD).
// byte: invalid UTF-8 is written as an escaped U+FFFD, which DecodeHead's
// canonical check then refuses.
func validName(field, v string) error {
if !utf8.ValidString(v) {
return fmt.Errorf("%w: %s %q is not valid UTF-8", ErrMalformed, field, v)
@@ -330,9 +330,10 @@ func TestRunAbort_InterruptAtEveryStep(t *testing.T) {
})
}
// A name that is not valid UTF-8 would be stored as U+FFFD by the JSON
// encoding (and still re-encode the same), so HEAD would hold another name
// than the caller gave: refused on every string field.
// A name that is not valid UTF-8 is written by the JSON encoding as an
// escaped U+FFFD, and DecodeHead's canonical check then refuses those
// bytes: Encode would succeed with a HEAD its own decoder cannot read.
// Refused at Encode on every string field.
func TestHead_RefusesInvalidUTF8(t *testing.T) {
rn := RootName{Hash: Sum([]byte("r"))}
bad := "a\xff"