1 package relay
2 3 import (
4 "go/ast"
5 "go/parser"
6 "go/token"
7 "testing"
8 )
9 10 // TestDomainLoopsCheckTheBreaker enforces the shutdown invariant at the source
11 // level: every spawned domain loop must test the breaker as its first act, before
12 // it does any work.
13 //
14 // This is a structural test on purpose. Reachability of the breaker select cannot
15 // be proved by timing: a full inbound channel is drained within the pass that
16 // observes it, so a timing test cannot force a domain to starve. What can be
17 // checked deterministically is the property the loop must have, and deleting the
18 // check is exactly the regression this catches.
19 //
20 // The rule admits the two shapes the tree uses: an `if ch.Stop.Closed()` guard at
21 // the top of the loop (net, acceptor), or a select whose first case is the
22 // breaker (db, blob, verify, reader, writer). The root's own loop in
23 // cmd/torly/main.go is not listed: it is covered by the shutdown tests, which
24 // exercise the signal path end to end.
25 func TestDomainLoopsCheckTheBreaker(t *testing.T) {
26 loops := []struct {
27 file string
28 fn string
29 }{
30 {"../domain/db/db.go", "Run"},
31 {"../domain/blob/blob.go", "Run"},
32 {"../domain/verify/verify.go", "Run"},
33 {"../domain/io/reader.go", "Reader"},
34 {"../domain/io/writer.go", "Writer"},
35 {"../domain/io/acceptor.go", "Acceptor"},
36 {"../domain/net/net.go", "Run"},
37 }
38 39 for _, l := range loops {
40 f, err := parser.ParseFile(token.NewFileSet(), l.file, nil, 0)
41 if err != nil {
42 t.Fatalf("%s: %v", l.file, err)
43 }
44 fd := findFunc(f, l.fn)
45 if fd == nil {
46 t.Fatalf("%s: func %s not found", l.file, l.fn)
47 }
48 loop := firstFor(fd)
49 if loop == nil {
50 t.Fatalf("%s: %s has no top-level for loop", l.file, l.fn)
51 }
52 if len(loop.Body.List) == 0 {
53 t.Fatalf("%s: %s loop is empty", l.file, l.fn)
54 }
55 if !checksBreaker(loop.Body.List[0]) {
56 t.Errorf("%s: %s loop does not check the breaker before working; "+
57 "the loop must return on Stop so a busy domain can still shut down",
58 l.file, l.fn)
59 }
60 }
61 }
62 63 func findFunc(f *ast.File, name string) *ast.FuncDecl {
64 for _, d := range f.Decls {
65 fd, ok := d.(*ast.FuncDecl)
66 if ok && fd.Recv == nil && fd.Name.Name == name {
67 return fd
68 }
69 }
70 return nil
71 }
72 73 // firstFor returns the first for statement directly in the function body. Every
74 // domain loop in this tree is that statement, because a domain body is the loop
75 // and nothing else.
76 func firstFor(fd *ast.FuncDecl) *ast.ForStmt {
77 for _, st := range fd.Body.List {
78 if f, ok := st.(*ast.ForStmt); ok {
79 return f
80 }
81 }
82 return nil
83 }
84 85 // checksBreaker reports whether st is an if whose condition tests Closed(), or a
86 // select with a case receiving from Done().
87 func checksBreaker(st ast.Stmt) bool {
88 switch s := st.(type) {
89 case *ast.IfStmt:
90 return callsMethod(s.Cond, "Closed")
91 case *ast.SelectStmt:
92 for _, c := range s.Body.List {
93 cc, ok := c.(*ast.CommClause)
94 if !ok {
95 continue
96 }
97 if cc.Comm == nil {
98 continue
99 }
100 if callsMethod(cc.Comm, "Done") {
101 return true
102 }
103 }
104 }
105 return false
106 }
107 108 func callsMethod(n ast.Node, name string) bool {
109 found := false
110 ast.Inspect(n, func(node ast.Node) bool {
111 sel, ok := node.(*ast.SelectorExpr)
112 if ok && sel.Sel.Name == name {
113 found = true
114 }
115 return !found
116 })
117 return found
118 }
119