-
Notifications
You must be signed in to change notification settings - Fork 8
/
lazy_test.go
191 lines (153 loc) · 3.6 KB
/
lazy_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package vfilter
import (
"context"
"fmt"
"sync"
"testing"
"github.com/Velocidex/ordereddict"
"github.com/sebdah/goldie/v2"
"www.velocidex.com/golang/vfilter/arg_parser"
"www.velocidex.com/golang/vfilter/types"
"www.velocidex.com/golang/vfilter/utils/dict"
)
var (
mu sync.Mutex
markers = []string{}
)
type lazyTypeTest struct {
Const string
}
func (self lazyTypeTest) Foo() string {
logMarkers("Foo ran")
return "Hello"
}
func (self lazyTypeTest) Bar() string {
logMarkers("Bar ran")
return "Goodbye"
}
func (self lazyTypeTest) Close() {
logMarkers("Close ran")
}
// Advertise only some fields and methods
func (self lazyTypeTest) Members() []string {
return []string{"Foo", "Const"}
}
type LazyPluginArgs struct {
Rows int64 `vfilter:"optional,field=rows"`
}
type LazyPlugin struct {
count int
}
func (self *LazyPlugin) Call(
ctx context.Context, scope types.Scope,
args *ordereddict.Dict) <-chan Row {
output_chan := make(chan Row)
go func() {
defer close(output_chan)
arg := LazyPluginArgs{}
err := arg_parser.ExtractArgsWithContext(ctx, scope, args, &arg)
if err != nil {
panic(err)
}
if arg.Rows == 0 {
arg.Rows = 1
}
for i := int64(0); i < arg.Rows; i++ {
output_chan <- lazyTypeTest{}
}
}()
return output_chan
}
func (self LazyPlugin) Info(scope types.Scope, type_map *TypeMap) *PluginInfo {
return &PluginInfo{
Name: "lazy",
}
}
type LazyDictPlugin struct {
count int
}
func (self *LazyDictPlugin) Call(
ctx context.Context, scope types.Scope,
args *ordereddict.Dict) <-chan Row {
output_chan := make(chan Row)
go func() {
defer close(output_chan)
arg := LazyPluginArgs{}
err := arg_parser.ExtractArgsWithContext(ctx, scope, args, &arg)
if err != nil {
panic(err)
}
if arg.Rows == 0 {
arg.Rows = 1
}
for i := int64(0); i < arg.Rows; i++ {
output_chan <- ordereddict.NewDict().
Set("Const", 10).
Set("Foo", func() Any {
logMarkers("Foo ran")
return "Hello"
}).
Set("Bar", func() Any {
logMarkers("Bar ran")
return "Goodbye"
})
}
}()
return output_chan
}
func (self LazyDictPlugin) Info(scope types.Scope, type_map *TypeMap) *PluginInfo {
return &PluginInfo{
Name: "lazy_dict",
}
}
var lazyTests = []vqlTest{
{"Lazy function hide method",
"SELECT * FROM lazy()"},
{"Lazy function extra method",
"SELECT *, Bar FROM lazy()"},
{"Lazy dict plugin",
"SELECT Bar FROM lazy_dict()"},
}
// Test the correct destructor call order
func TestLazy(t *testing.T) {
result := ordereddict.NewDict()
for i, testCase := range lazyTests {
mu.Lock()
markers = []string{}
mu.Unlock()
scope := NewScope().AppendPlugins(&LazyPlugin{}, &LazyDictPlugin{})
multi_vql, err := MultiParse(testCase.vql)
if err != nil {
t.Fatalf("Failed to parse %v: %v", testCase.vql, err)
}
query := ""
var output []Row
for _, vql := range multi_vql {
ctx := context.Background()
for row := range vql.Eval(ctx, scope) {
output = append(output, dict.RowToDict(ctx, scope, row))
}
query += FormatToString(scope, vql)
}
// Close the scope to force destructors to be called.
scope.Close()
result.Set(fmt.Sprintf(
"%03d %s: %s", i, testCase.name, query),
output)
result.Set(fmt.Sprintf(
"%03d %s: %s - markers", i, testCase.name, query),
markers)
}
g := goldie.New(
t,
goldie.WithFixtureDir("fixtures"),
goldie.WithNameSuffix(".golden"),
goldie.WithDiffEngine(goldie.ColoredDiff),
)
g.AssertJson(t, "TestLazy", result)
}
func logMarkers(format string, args ...interface{}) {
mu.Lock()
defer mu.Unlock()
markers = append(markers, fmt.Sprintf(format, args...))
}