forked from synthesio/zconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor_test.go
267 lines (225 loc) · 5.73 KB
/
processor_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package zconfig
import (
"context"
"errors"
"reflect"
"strings"
"testing"
"time"
)
type Service struct {
Workers int `key:"workers"`
Dependency SimpleDependency `key:"dependency" inject-as:"dependency"`
Injected SimpleDependency `inject:"dependency"`
Recursive *RecursiveDependency
notExported SimpleDependency
notExportedPtr *SimpleDependency
}
type SimpleDependency struct {
Foo int `key:"foo"`
}
type RecursiveDependency struct {
Service
}
func TestWalk(t *testing.T) {
var v = reflect.ValueOf(new(Service))
// The check itself is done in non-blocking fashion so we can fail in
// case of dependency loop, which can happen when embedding recursive
// pointer types.
var root *Field
var err error
var done = make(chan struct{})
go func() {
root, err = walk(v, reflect.StructField{}, nil)
close(done)
}()
select {
case <-time.After(100 * time.Millisecond):
t.Fatalf("walking service: timeout")
case <-done:
}
if err != nil {
t.Fatalf("walking service: %s", err)
}
expected := &Field{Path: "$", Anonymous: true, Children: []*Field{
{Path: "$.Workers"},
{Path: "$.Dependency", Children: []*Field{
{Path: "$.Dependency.Foo"},
}},
{Path: "$.Injected"},
{Path: "$.Recursive"},
}}
displayGraph(t, root, 0)
testGraph(t, root, expected, nil)
}
func displayGraph(t *testing.T, f *Field, l int) {
t.Log(strings.Repeat("\t", l), f.Path)
for _, c := range f.Children {
displayGraph(t, c, l+1)
}
}
func testGraph(t *testing.T, actual, expected, parent *Field) {
if actual.Path != expected.Path {
t.Fatalf("invalid value for path: expected %s, got %s", expected.Path, actual.Path)
}
if actual.Parent != parent {
t.Fatalf("invalid parent for path %s: wanted %p, got %p", actual.Path, parent, actual.Parent)
}
if len(actual.Children) != len(expected.Children) {
t.Fatalf("invalid number of children for path %s: wanted %d, got %d", actual.Path, len(expected.Children), len(actual.Children))
}
if actual.Anonymous != expected.Anonymous {
t.Fatalf("invalid anonymous field for path %s: wanted %v, got %v", actual.Path, expected.Anonymous, actual.Anonymous)
}
for i := 0; i < len(actual.Children); i++ {
testGraph(t, actual.Children[i], expected.Children[i], actual)
}
}
func TestResolve(t *testing.T) {
var v = reflect.ValueOf(new(Service))
root, err := walk(v, reflect.StructField{}, nil)
if err != nil {
t.Fatalf("walking service: %s", err)
}
fields, err := resolve(root)
if err != nil {
t.Fatalf("resolving graph: %s", err)
}
displayResolvedGraph(t, fields)
expected := map[string]struct{}{
"$": {},
"$.Workers": {},
"$.Dependency": {},
"$.Dependency.Foo": {},
"$.Recursive": {},
}
for _, field := range fields {
if _, ok := expected[field.Path]; ok {
delete(expected, field.Path)
} else {
t.Errorf("got unexpected field %s", field.Path)
}
}
for path := range expected {
t.Errorf("expected field %s not found", path)
}
}
func displayResolvedGraph(t *testing.T, fields []*Field) {
for _, f := range fields {
t.Log(f.Path)
}
}
func TestNewCycleError(t *testing.T) {
// This is the dependency map of the following structure, which is
// valid Go.
// type CycleService struct {
// B *CycleDepB `inject-as:"B"`
// C *CycleDepC `inject-as:"C"`
// }
// type CycleDepB struct {
// D *CycleDepC `inject:"C"`
// }
// type CycleDepC struct {
// E *CycleDepB `inject:"B"`
// }
var deps = dependencies{
"A": {"B": struct{}{}, "C": struct{}{}},
"B": {"D": struct{}{}},
"C": {"E": struct{}{}},
"D": {"C": struct{}{}},
"E": {"B": struct{}{}},
}
var err error
var done = make(chan struct{})
go func() {
err = newCycleError(deps)
close(done)
}()
select {
case <-time.After(1 * time.Second):
t.Fatalf("timeout computing cycle")
case <-done:
}
t.Log(err)
}
func TestDependencies(t *testing.T) {
var deps = dependencies{}
a := &Field{
Path: "A",
}
b := &Field{
Path: "B",
}
deps.add(a, b)
deps.add(b, a)
aDeps, ok := deps[a.Path]
if !ok {
t.Fatal("a path not found")
}
if _, ok := aDeps[b.Path]; !ok {
t.Fatal("b dependency not found")
}
deps.remove(a.Path)
if _, ok = deps[a.Path]; ok {
t.Fatal("a path still found")
}
bDeps, ok := deps[b.Path]
if !ok {
t.Fatal("b path not found")
}
if _, ok := bDeps[a.Path]; ok {
t.Fatal("a dependency found")
}
}
func TestProcessorHooks(t *testing.T) {
t.Run("execution", func(t *testing.T) {
executed := false
testHook := func(ctx context.Context, field *Field) error {
executed = true
return nil
}
err := NewProcessor(testHook).Process(context.Background(), new(Service))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !executed {
t.Fatal("hook was not executed")
}
})
t.Run("fields", func(t *testing.T) {
fields := map[string]bool{
"$": false,
"$.Workers": false,
"$.Dependency": false,
"$.Dependency.Foo": false,
"$.Recursive": false,
}
testHook := func(ctx context.Context, field *Field) error {
visited, ok := fields[field.Path]
if !ok {
t.Fatalf("unexpected field: %s", field.Path)
}
if visited {
t.Fatalf("field %s already visited", field.Path)
}
fields[field.Path] = true
return nil
}
err := NewProcessor(testHook).Process(context.Background(), new(Service))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("error", func(t *testing.T) {
testHook := func(ctx context.Context, field *Field) error {
return errors.New("an error")
}
err := NewProcessor(testHook).Process(context.Background(), new(Service))
if err == nil {
t.Fatalf("expected an error, got nil")
}
if !strings.HasPrefix(err.Error(), "executing hook on field") {
t.Fatalf("unexpected error: %v", err)
}
})
}