-
Notifications
You must be signed in to change notification settings - Fork 0
/
caching_coompiler_test.go
96 lines (78 loc) · 2.39 KB
/
caching_coompiler_test.go
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
86
87
88
89
90
91
92
93
94
95
96
package expr
import (
"testing"
"github.com/google/cel-go/cel"
"github.com/stretchr/testify/require"
)
func TestCachingParser_CachesSame(t *testing.T) {
c := cachingCompiler{env: newEnv()}
a := `event.data.a == "cache"`
b := `event.data.b == "cache"`
var (
prevAST *cel.Ast
prevIssues *cel.Issues
prevVars LiftedArgs
)
t.Run("With an uncached expression", func(t *testing.T) {
prevAST, prevIssues, prevVars = c.Compile(a)
require.NotNil(t, prevAST)
require.Nil(t, prevIssues)
require.NotNil(t, prevVars)
require.EqualValues(t, 0, c.Hits())
require.EqualValues(t, 1, c.Misses())
})
t.Run("With a cached expression", func(t *testing.T) {
ast, issues, vars := c.Compile(a)
require.NotNil(t, ast)
require.Nil(t, issues)
require.Equal(t, prevAST, ast)
require.Equal(t, prevIssues, issues)
require.Equal(t, prevVars, vars)
require.EqualValues(t, 1, c.Hits())
require.EqualValues(t, 1, c.Misses())
})
t.Run("With another uncached expression", func(t *testing.T) {
prevAST, prevIssues, prevVars = c.Compile(b)
require.NotNil(t, prevAST)
require.Nil(t, prevIssues)
// This misses the cache, as the vars have changed - not the
// literals.
require.EqualValues(t, 1, c.Hits())
require.EqualValues(t, 2, c.Misses())
})
}
func TestCachingCompile(t *testing.T) {
c := cachingCompiler{env: newEnv()}
a := `event.data.a == "literal-a" && event.data.b == "yes-1"`
b := `event.data.a == "literal-b" && event.data.b == "yes-2"`
var (
prevAST *cel.Ast
prevIssues *cel.Issues
prevVars LiftedArgs
)
t.Run("With an uncached expression", func(t *testing.T) {
prevAST, prevIssues, prevVars = c.Compile(a)
require.NotNil(t, prevAST)
require.Nil(t, prevIssues)
require.EqualValues(t, 0, c.Hits())
require.EqualValues(t, 1, c.Misses())
})
t.Run("With a cached expression", func(t *testing.T) {
ast, issues, vars := c.Compile(a)
require.NotNil(t, ast)
require.Nil(t, issues)
require.Equal(t, prevAST, ast)
require.Equal(t, prevIssues, issues)
require.Equal(t, prevVars, vars)
require.EqualValues(t, 1, c.Hits())
require.EqualValues(t, 1, c.Misses())
})
t.Run("With a cached expression having different literals ONLY", func(t *testing.T) {
prevAST, prevIssues, _ = c.Compile(b)
require.NotNil(t, prevAST)
require.Nil(t, prevIssues)
// This misses the cache.
require.EqualValues(t, 2, c.Hits())
require.EqualValues(t, 1, c.Misses())
})
}