-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilder_test.go
More file actions
344 lines (323 loc) · 9.61 KB
/
builder_test.go
File metadata and controls
344 lines (323 loc) · 9.61 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package flyt_test
import (
"context"
"errors"
"testing"
"time"
"github.com/mark3labs/flyt"
)
// TestNodeBuilderBackwardsCompatibility verifies that existing code continues to work
func TestNodeBuilderBackwardsCompatibility(t *testing.T) {
tests := []struct {
name string
node func() flyt.Node
}{
{
name: "traditional_with_exec_func",
node: func() flyt.Node {
return flyt.NewNode(
flyt.WithExecFunc(func(ctx context.Context, prepResult flyt.Result) (flyt.Result, error) {
return flyt.R("executed"), nil
}),
)
},
},
{
name: "traditional_with_multiple_options",
node: func() flyt.Node {
return flyt.NewNode(
flyt.WithMaxRetries(3),
flyt.WithWait(time.Second),
flyt.WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
return "executed", nil
}),
)
},
},
{
name: "traditional_with_all_phases",
node: func() flyt.Node {
return flyt.NewNode(
flyt.WithPrepFunc(func(ctx context.Context, shared *flyt.SharedStore) (flyt.Result, error) {
return flyt.R("prepped"), nil
}),
flyt.WithExecFunc(func(ctx context.Context, prepResult flyt.Result) (flyt.Result, error) {
return flyt.R("executed"), nil
}),
flyt.WithPostFunc(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult flyt.Result) (flyt.Action, error) {
return flyt.DefaultAction, nil
}),
)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
node := tt.node()
if node == nil {
t.Fatal("expected non-nil node")
}
// Verify it implements Node interface
ctx := context.Background()
shared := flyt.NewSharedStore()
action, err := flyt.Run(ctx, node, shared)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if action == "" {
t.Error("expected non-empty action")
}
})
}
}
// TestNodeBuilderChaining tests the new builder pattern
func TestNodeBuilderChaining(t *testing.T) {
tests := []struct {
name string
buildFn func() flyt.Node
validate func(t *testing.T, node flyt.Node)
}{
{
name: "simple_exec_chain",
buildFn: func() flyt.Node {
return flyt.NewNode().
WithExecFunc(func(ctx context.Context, prepResult flyt.Result) (flyt.Result, error) {
return flyt.R("chained"), nil
})
},
validate: func(t *testing.T, node flyt.Node) {
ctx := context.Background()
result, err := node.Exec(ctx, nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
s, ok := result.(string)
if !ok {
t.Errorf("expected string, got %T", result)
}
if s != "chained" {
t.Errorf("expected 'chained', got %v", s)
}
},
},
{
name: "full_chain_with_retries",
buildFn: func() flyt.Node {
return flyt.NewNode().
WithMaxRetries(5).
WithWait(100 * time.Millisecond).
WithPrepFunc(func(ctx context.Context, shared *flyt.SharedStore) (flyt.Result, error) {
return flyt.R("prep_data"), nil
}).
WithExecFunc(func(ctx context.Context, prepResult flyt.Result) (flyt.Result, error) {
return flyt.R("exec_result"), nil
}).
WithPostFunc(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult flyt.Result) (flyt.Action, error) {
shared.Set("result", execResult.Value())
return "custom_action", nil
})
},
validate: func(t *testing.T, node flyt.Node) {
// Check if it's retryable
if retryable, ok := node.(flyt.RetryableNode); ok {
if retryable.GetMaxRetries() != 5 {
t.Errorf("expected 5 retries, got %d", retryable.GetMaxRetries())
}
if retryable.GetWait() != 100*time.Millisecond {
t.Errorf("expected 100ms wait, got %v", retryable.GetWait())
}
} else {
t.Error("expected node to implement RetryableNode")
}
// Run the node
ctx := context.Background()
shared := flyt.NewSharedStore()
action, err := flyt.Run(ctx, node, shared)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if action != "custom_action" {
t.Errorf("expected 'custom_action', got %v", action)
}
resultVal, exists := shared.Get("result")
if !exists {
t.Error("expected stored result to exist")
} else if r, ok := resultVal.(flyt.Result); ok {
if r.MustString() != "exec_result" {
t.Errorf("expected stored result 'exec_result', got %v", r.MustString())
}
} else if resultVal != "exec_result" {
t.Errorf("expected stored result 'exec_result', got %v", resultVal)
}
},
},
{
name: "chain_with_any_functions",
buildFn: func() flyt.Node {
return flyt.NewNode().
WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
return map[string]string{"key": "value"}, nil
}).
WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
m := prepResult.(map[string]string)
return m["key"], nil
}).
WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
// Extract value from Result if it's wrapped
var resultValue any
if r, ok := execResult.(flyt.Result); ok {
resultValue = r.Value()
} else {
resultValue = execResult
}
if resultValue == "value" {
return "success", nil
}
return "failure", nil
})
},
validate: func(t *testing.T, node flyt.Node) {
ctx := context.Background()
shared := flyt.NewSharedStore()
action, err := flyt.Run(ctx, node, shared)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if action != "success" {
t.Errorf("expected 'success', got %v", action)
}
},
},
{
name: "chain_with_fallback",
buildFn: func() flyt.Node {
attempts := 0
return flyt.NewNode().
WithMaxRetries(2).
WithExecFunc(func(ctx context.Context, prepResult flyt.Result) (flyt.Result, error) {
attempts++
return flyt.Result{}, errors.New("exec failed")
}).
WithExecFallbackFunc(func(prepResult any, err error) (any, error) {
return "fallback_value", nil
})
},
validate: func(t *testing.T, node flyt.Node) {
ctx := context.Background()
shared := flyt.NewSharedStore()
action, err := flyt.Run(ctx, node, shared)
if err != nil {
t.Errorf("expected fallback to handle error, got: %v", err)
}
if action != flyt.DefaultAction {
t.Errorf("expected DefaultAction, got %v", action)
}
},
},
{
name: "chain_with_build_method",
buildFn: func() flyt.Node {
return flyt.NewNode().
WithExecFunc(func(ctx context.Context, prepResult flyt.Result) (flyt.Result, error) {
return flyt.R("built"), nil
})
},
validate: func(t *testing.T, node flyt.Node) {
ctx := context.Background()
result, err := node.Exec(ctx, nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
s, ok := result.(string)
if !ok {
t.Errorf("expected string, got %T", result)
}
if s != "built" {
t.Errorf("expected 'built', got %v", s)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
node := tt.buildFn()
if node == nil {
t.Fatal("expected non-nil node")
}
tt.validate(t, node)
})
}
}
// TestNodeBuilderMixedStyle tests mixing traditional and builder patterns
func TestNodeBuilderMixedStyle(t *testing.T) {
// Create node with traditional options
builder := flyt.NewNode(
flyt.WithMaxRetries(3),
flyt.WithWait(time.Second),
)
// Continue configuring with builder pattern
var node flyt.Node = builder.
WithExecFunc(func(ctx context.Context, prepResult flyt.Result) (flyt.Result, error) {
return flyt.R("mixed_result"), nil
}).
WithPostFunc(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult flyt.Result) (flyt.Action, error) {
return "mixed_action", nil
})
// Validate
if retryable, ok := node.(flyt.RetryableNode); ok {
if retryable.GetMaxRetries() != 3 {
t.Errorf("expected 3 retries, got %d", retryable.GetMaxRetries())
}
if retryable.GetWait() != time.Second {
t.Errorf("expected 1s wait, got %v", retryable.GetWait())
}
} else {
t.Error("expected node to implement RetryableNode")
}
ctx := context.Background()
shared := flyt.NewSharedStore()
action, err := flyt.Run(ctx, node, shared)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if action != "mixed_action" {
t.Errorf("expected 'mixed_action', got %v", action)
}
}
// TestNodeBuilderInterfaceCompliance ensures NodeBuilder properly implements all interfaces
func TestNodeBuilderInterfaceCompliance(t *testing.T) {
node := flyt.NewNode().
WithMaxRetries(3).
WithWait(time.Second)
// Test Node interface
var _ flyt.Node = node
// Test RetryableNode interface
var _ flyt.RetryableNode = node
// Test FallbackNode interface
var _ flyt.FallbackNode = node
// All interfaces should be satisfied
t.Log("NodeBuilder successfully implements all required interfaces")
}
// BenchmarkNodeCreationTraditional benchmarks traditional node creation
func BenchmarkNodeCreationTraditional(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = flyt.NewNode(
flyt.WithMaxRetries(3),
flyt.WithWait(time.Second),
flyt.WithExecFunc(func(ctx context.Context, prepResult flyt.Result) (flyt.Result, error) {
return flyt.R("result"), nil
}),
)
}
}
// BenchmarkNodeCreationBuilder benchmarks builder pattern node creation
func BenchmarkNodeCreationBuilder(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = flyt.NewNode().
WithMaxRetries(3).
WithWait(time.Second).
WithExecFunc(func(ctx context.Context, prepResult flyt.Result) (flyt.Result, error) {
return flyt.R("result"), nil
})
}
}