Repository navigation
Expand file tree
/
Copy pathcache_test.go
More file actions
85 lines (77 loc) · 2.36 KB
/
Copy pathcache_test.go
File metadata and controls
85 lines (77 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package validator
import (
"fmt"
"testing"
)
// TestExprCacheBounded checks the compiled-expression cache stays bounded.
func TestExprCacheBounded(t *testing.T) {
v := MustNew(WithoutBuiltinRules(), WithRuleFunc("tag", func(*Field) bool { return true }, "m"))
for i := 0; i < maxExprCache+50; i++ {
if _, err := v.compile(fmt.Sprintf("tag:%d", i)); err != nil {
t.Fatal(err)
}
}
if got := v.exprCache.len(); got > maxExprCache {
t.Errorf("exprCache grew to %d, exceeds bound %d", got, maxExprCache)
}
}
func TestExprCache(t *testing.T) {
v := MustNew(
WithoutBuiltinRules(),
WithRuleFunc("yes", func(*Field) bool { return true }, "m"),
WithRuleFunc("no", func(*Field) bool { return false }, "m"),
)
c1, err := v.compile("yes && yes")
if err != nil {
t.Fatal(err)
}
c2, err := v.compile("yes && yes")
if err != nil {
t.Fatal(err)
}
if c1 != c2 {
t.Error("the same expression must be compiled once and served from cache")
}
if passed, err := c1.Fast(&Field{name: "x"}); err != nil || !passed {
t.Error("yes && yes should pass")
}
c3, _ := v.compile("yes && no")
if passed, err := c3.Fast(&Field{name: "x"}); err != nil || passed {
t.Error("yes && no should fail")
}
// unknown rule must fail at compile time, not validation time
if _, err := v.compile("ghost"); err == nil {
t.Error("unknown rule should fail compilation")
}
}
// TestExprCompileAllocFree guards that evaluating a cached program over the root *Field is alloc-free.
func TestExprCompileAllocFree(t *testing.T) {
v := MustNew(WithoutBuiltinRules(), WithRuleFunc("yes", func(*Field) bool { return true }, "m"))
c, err := v.compile("yes && yes")
if err != nil {
t.Fatal(err)
}
f := &Field{name: "x"}
allocs := testing.AllocsPerRun(100, func() {
_, _ = c.Fast(f)
})
if allocs != 0 {
t.Errorf("Fast eval allocated %v times, want 0", allocs)
}
}
// TestExprCompileAllocFreeWithArgs guards alloc-free eval with rule args and multiple leaves.
func TestExprCompileAllocFreeWithArgs(t *testing.T) {
v := MustNew(WithoutBuiltinRules(),
WithRuleFunc("has", func(f *Field) bool { return len(f.Attrs()) > 0 }, "m"))
c, err := v.compile("has:a && has:b")
if err != nil {
t.Fatal(err)
}
f := &Field{name: "x"}
allocs := testing.AllocsPerRun(100, func() {
_, _ = c.Fast(f)
})
if allocs != 0 {
t.Errorf("parameterized multi-leaf Fast over *Field allocated %v, want 0", allocs)
}
}