package relay import ( "go/ast" "go/parser" "go/token" "testing" ) // TestDomainLoopsCheckTheBreaker enforces the shutdown invariant at the source // level: every spawned domain loop must test the breaker as its first act, before // it does any work. // // This is a structural test on purpose. Reachability of the breaker select cannot // be proved by timing: a full inbound channel is drained within the pass that // observes it, so a timing test cannot force a domain to starve. What can be // checked deterministically is the property the loop must have, and deleting the // check is exactly the regression this catches. // // The rule admits the two shapes the tree uses: an `if ch.Stop.Closed()` guard at // the top of the loop (net, acceptor), or a select whose first case is the // breaker (db, blob, verify, reader, writer). The root's own loop in // cmd/torly/main.go is not listed: it is covered by the shutdown tests, which // exercise the signal path end to end. func TestDomainLoopsCheckTheBreaker(t *testing.T) { loops := []struct { file string fn string }{ {"../domain/db/db.go", "Run"}, {"../domain/blob/blob.go", "Run"}, {"../domain/verify/verify.go", "Run"}, {"../domain/io/reader.go", "Reader"}, {"../domain/io/writer.go", "Writer"}, {"../domain/io/acceptor.go", "Acceptor"}, {"../domain/net/net.go", "Run"}, } for _, l := range loops { f, err := parser.ParseFile(token.NewFileSet(), l.file, nil, 0) if err != nil { t.Fatalf("%s: %v", l.file, err) } fd := findFunc(f, l.fn) if fd == nil { t.Fatalf("%s: func %s not found", l.file, l.fn) } loop := firstFor(fd) if loop == nil { t.Fatalf("%s: %s has no top-level for loop", l.file, l.fn) } if len(loop.Body.List) == 0 { t.Fatalf("%s: %s loop is empty", l.file, l.fn) } if !checksBreaker(loop.Body.List[0]) { t.Errorf("%s: %s loop does not check the breaker before working; "+ "the loop must return on Stop so a busy domain can still shut down", l.file, l.fn) } } } func findFunc(f *ast.File, name string) *ast.FuncDecl { for _, d := range f.Decls { fd, ok := d.(*ast.FuncDecl) if ok && fd.Recv == nil && fd.Name.Name == name { return fd } } return nil } // firstFor returns the first for statement directly in the function body. Every // domain loop in this tree is that statement, because a domain body is the loop // and nothing else. func firstFor(fd *ast.FuncDecl) *ast.ForStmt { for _, st := range fd.Body.List { if f, ok := st.(*ast.ForStmt); ok { return f } } return nil } // checksBreaker reports whether st is an if whose condition tests Closed(), or a // select with a case receiving from Done(). func checksBreaker(st ast.Stmt) bool { switch s := st.(type) { case *ast.IfStmt: return callsMethod(s.Cond, "Closed") case *ast.SelectStmt: for _, c := range s.Body.List { cc, ok := c.(*ast.CommClause) if !ok { continue } if cc.Comm == nil { continue } if callsMethod(cc.Comm, "Done") { return true } } } return false } func callsMethod(n ast.Node, name string) bool { found := false ast.Inspect(n, func(node ast.Node) bool { sel, ok := node.(*ast.SelectorExpr) if ok && sel.Sel.Name == name { found = true } return !found }) return found }