-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluent_test.go
More file actions
507 lines (434 loc) · 15.8 KB
/
Copy pathfluent_test.go
File metadata and controls
507 lines (434 loc) · 15.8 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package gloo
import (
"context"
"errors"
"reflect"
"strconv"
"testing"
"github.com/destel/rill"
)
// The fluent builder reports every validation failure as an error you can match
// with errors.Is — never a panic. These tests pin each failure mode to its
// sentinel.
func TestChainErrorsOnNonSource(t *testing.T) {
_, err := Chain(struct{}{}).Collect()
if !errors.Is(err, ErrNotSource) {
t.Errorf("got %v, want ErrNotSource", err)
}
}
func TestToErrorsOnNonCommand(t *testing.T) {
src := SliceSource([]string{"x"})
_, err := Chain(src).To(struct{}{}).Collect()
if !errors.Is(err, ErrNotCommand) {
t.Errorf("got %v, want ErrNotCommand", err)
}
}
type badArityCommand struct{}
func (badArityCommand) Execute() {}
func TestToErrorsOnWrongExecuteSignature(t *testing.T) {
src := SliceSource([]string{"x"})
_, err := Chain(src).To(badArityCommand{}).Collect()
if !errors.Is(err, ErrNotCommand) {
t.Errorf("got %v, want ErrNotCommand", err)
}
}
func TestToErrorsOnStageTypeMismatch(t *testing.T) {
src := SliceSource([]string{"x"})
wantsInts := mapCmd(func(n int) (int, error) { return n, nil })
_, err := Chain(src).To(wantsInts).Collect()
if !errors.Is(err, ErrStageTypeMismatch) {
t.Errorf("got %v, want ErrStageTypeMismatch", err)
}
}
// A mismatch error must name both types so the message is actionable.
func TestStageTypeMismatchNamesTypes(t *testing.T) {
src := SliceSource([]string{"x"})
wantsInts := mapCmd(func(n int) (int, error) { return n, nil })
_, err := Chain(src).To(wantsInts).Collect()
msg := err.Error()
// The ordered phrasing must be correct: the int-command EXPECTS int, while
// the source PRODUCES string. A want/got swap in mismatch() would still
// contain both type names, so assert the full ordered fragments.
if !contains(msg, "stage expects gloo.Stream[int]") {
t.Errorf("message %q must say the stage expects the int stream", msg)
}
if !contains(msg, "upstream produces gloo.Stream[string]") {
t.Errorf("message %q must say the upstream produces the string stream", msg)
}
}
func contains(s, sub string) bool {
for i := 0; i+len(sub) <= len(s); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
}
// The first error is sticky: a later valid stage does not clear it, and the
// terminal still reports the original failure.
func TestFirstErrorIsSticky(t *testing.T) {
src := SliceSource([]string{"x"})
pass := mapCmd(func(s string) (string, error) { return s, nil })
_, err := Chain(src).To(struct{}{}).To(pass).Collect()
if !errors.Is(err, ErrNotCommand) {
t.Errorf("got %v, want the first error ErrNotCommand to stick", err)
}
}
func TestSinkErrorsOnNonSink(t *testing.T) {
src := SliceSource([]string{"x"})
_, err := Chain(src).Sink(struct{}{})
if !errors.Is(err, ErrNotSink) {
t.Errorf("got %v, want ErrNotSink", err)
}
}
type badAritySink struct{}
func (badAritySink) Consume() {}
func TestSinkErrorsOnWrongConsumeSignature(t *testing.T) {
src := SliceSource([]string{"x"})
_, err := Chain(src).Sink(badAritySink{})
if !errors.Is(err, ErrNotSink) {
t.Errorf("got %v, want ErrNotSink", err)
}
}
type intSink struct{}
func (intSink) Consume(_ context.Context, in Stream[int]) (int, error) {
n := 0
err := rill.ForEach(in.Chan(), 1, func(int) error { n++; return nil })
return n, err
}
func TestSinkErrorsOnTypeMismatch(t *testing.T) {
src := SliceSource([]string{"x"})
_, err := Chain(src).Sink(intSink{})
if !errors.Is(err, ErrSinkTypeMismatch) {
t.Errorf("got %v, want ErrSinkTypeMismatch", err)
}
}
func TestSinkReturnsResultAndNilError(t *testing.T) {
src := SliceSource([]int{1, 2, 3})
res, err := Chain(src).Sink(intSink{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res.(int) != 3 {
t.Errorf("res = %v, want 3", res)
}
}
type failingSink struct{ err error }
func (s failingSink) Consume(_ context.Context, in Stream[string]) (int, error) {
for {
if _, ok := <-in.Chan(); !ok {
break
}
}
return 0, s.err
}
func TestSinkReturnsSinkError(t *testing.T) {
wantErr := errors.New("sink exploded")
src := SliceSource([]string{"x"})
_, err := Chain(src).Sink(failingSink{err: wantErr})
if !errors.Is(err, wantErr) {
t.Errorf("got %v, want %v", err, wantErr)
}
}
func TestRunWiresSourceCommandsSink(t *testing.T) {
src := SliceSource([]string{"3", "4"})
parse := mapCmd(strconv.Atoi)
res, err := Run(src, intSink{}, parse)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res.(int) != 2 {
t.Errorf("res = %v, want 2", res)
}
}
// Run surfaces a validation error from any stage as its error return, not a panic.
func TestRunReturnsValidationError(t *testing.T) {
src := SliceSource([]string{"x"})
_, err := Run(src, intSink{}, struct{}{})
if !errors.Is(err, ErrNotCommand) {
t.Errorf("got %v, want ErrNotCommand", err)
}
}
func TestRunContextHonorsContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
src := &ctxCheckSource{}
_, err := RunContext(ctx, src, intSink{})
if !errors.Is(err, context.Canceled) {
t.Errorf("got %v, want context.Canceled", err)
}
}
// ctxCheckSource emits ctx.Err() if cancelled, else a value.
type ctxCheckSource struct{}
func (s *ctxCheckSource) Stream(ctx context.Context) Stream[int] {
return Generate(ctx, func(ctx context.Context, send func(int) bool, sendErr func(error)) {
if err := ctx.Err(); err != nil {
sendErr(err)
return
}
if !send(1) {
return
}
})
}
func TestCollectEmptyStream(t *testing.T) {
src := SliceSource([]string{})
res, err := Chain(src).Collect()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got := res.([]string); len(got) != 0 {
t.Errorf("got %v, want empty slice", got)
}
}
func TestForEachErrorsOnNonFunc(t *testing.T) {
src := SliceSource([]string{"x"})
err := Chain(src).ForEach("not a func")
if !errors.Is(err, ErrNotForEachFunc) {
t.Errorf("got %v, want ErrNotForEachFunc", err)
}
}
func TestForEachErrorsOnTypeMismatch(t *testing.T) {
src := SliceSource([]string{"x"})
err := Chain(src).ForEach(func(int) error { return nil })
if !errors.Is(err, ErrStageTypeMismatch) {
t.Errorf("got %v, want ErrStageTypeMismatch", err)
}
}
// ForEach surfaces an earlier build error too, like the other terminals.
func TestForEachReturnsBuildError(t *testing.T) {
err := Chain(struct{}{}).ForEach(func(int) error { return nil })
if !errors.Is(err, ErrNotSource) {
t.Errorf("got %v, want ErrNotSource", err)
}
}
func TestTerminalAfterTerminalErrors(t *testing.T) {
src := SliceSource([]string{"x"})
p := Chain(src)
if _, err := p.Collect(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, err := p.Collect(); !errors.Is(err, ErrPipelineConsumed) {
t.Errorf("got %v, want ErrPipelineConsumed", err)
}
}
// Recording a stage after the pipeline is consumed sets the consumed error,
// which the next terminal surfaces.
func TestToAfterTerminalErrors(t *testing.T) {
src := SliceSource([]string{"x"})
p := Chain(src)
if _, err := p.Collect(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
pass := mapCmd(func(s string) (string, error) { return s, nil })
if _, err := p.To(pass).Collect(); !errors.Is(err, ErrPipelineConsumed) {
t.Errorf("got %v, want ErrPipelineConsumed", err)
}
}
// tryValueType extracts T from a validated Stream[T] channel.
func TestTryValueTypeReturnsElementType(t *testing.T) {
ch := make(chan rill.Try[string])
if got := tryValueType(reflect.ValueOf(ch)); got != reflect.TypeFor[string]() {
t.Errorf("got %v, want string", got)
}
}
// --- Collect / ForEach value and error paths ---
func TestCollectReturnsValues(t *testing.T) {
got, err := Chain(SliceSource([]int{1, 2, 3})).Collect()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if v := got.([]int); len(v) != 3 || v[0] != 1 || v[2] != 3 {
t.Errorf("got %v, want [1 2 3]", v)
}
}
func TestCollectReturnsStreamError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
got, err := ChainContext(ctx, &ctxCheckSource{}).Collect()
if !errors.Is(err, context.Canceled) {
t.Errorf("got %v, want context.Canceled", err)
}
if got != nil {
t.Errorf("result should be nil on error, got %v", got)
}
}
func TestForEachAppliesFnToEachValue(t *testing.T) {
sum := 0
if err := Chain(SliceSource([]int{1, 2, 3})).ForEach(func(n int) error { sum += n; return nil }); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if sum != 6 {
t.Errorf("sum = %d, want 6", sum)
}
}
func TestForEachReturnsFnError(t *testing.T) {
wantErr := errors.New("boom")
err := Chain(SliceSource([]string{"a", "b"})).ForEach(func(string) error { return wantErr })
if !errors.Is(err, wantErr) {
t.Errorf("got %v, want %v", err, wantErr)
}
}
func TestForEachReturnsStreamError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := ChainContext(ctx, &ctxCheckSource{}).ForEach(func(int) error { return nil })
if !errors.Is(err, context.Canceled) {
t.Errorf("got %v, want context.Canceled", err)
}
}
// badStreamArity has a Stream method with the wrong arity, exercising
// ChainContext's signature validation.
type badStreamArity struct{}
func (badStreamArity) Stream(_ context.Context, _ int) Stream[int] { return StreamOf[int]() }
// badStreamCtx has the right arity but a non-context first parameter, which must
// be rejected as a non-Source rather than failing later in a reflect.Call.
type badStreamCtx struct{}
func (badStreamCtx) Stream(_ int) Stream[int] { return StreamOf[int]() }
// assertNotSource builds a chain from an invalid source and asserts the terminal
// reports ErrNotSource — a value, never a panic.
func assertNotSource(t *testing.T, source any) {
t.Helper()
if _, err := Chain(source).Collect(); !errors.Is(err, ErrNotSource) {
t.Fatalf("got %v, want ErrNotSource", err)
}
}
func TestChain_RejectsBadSourceSignature(t *testing.T) { assertNotSource(t, badStreamArity{}) }
func TestChain_RejectsNonContextStreamParam(t *testing.T) { assertNotSource(t, badStreamCtx{}) }
func TestChain_RejectsNonSource(t *testing.T) { assertNotSource(t, struct{}{}) }
// --- Nil and non-Stream misuse: errors, never panics ---
func TestChainErrorsOnNilSource(t *testing.T) {
if _, err := Chain(nil).Collect(); !errors.Is(err, ErrNotSource) {
t.Errorf("got %v, want ErrNotSource", err)
}
}
// nilPtrSource exists to build a typed-nil source value: a method set on a
// pointer receiver whose receiver is nil at call time.
type nilPtrSource struct{}
func (*nilPtrSource) Stream(context.Context) Stream[int] { return StreamOf[int]() }
func TestChainErrorsOnTypedNilSource(t *testing.T) {
var src *nilPtrSource
if _, err := Chain(src).Collect(); !errors.Is(err, ErrNotSource) {
t.Errorf("got %v, want ErrNotSource", err)
}
}
func TestToErrorsOnNilCommand(t *testing.T) {
src := SliceSource([]string{"x"})
if _, err := Chain(src).To(nil).Collect(); !errors.Is(err, ErrNotCommand) {
t.Errorf("got %v, want ErrNotCommand", err)
}
}
func TestSinkErrorsOnNilSink(t *testing.T) {
src := SliceSource([]string{"x"})
if _, err := Chain(src).Sink(nil); !errors.Is(err, ErrNotSink) {
t.Errorf("got %v, want ErrNotSink", err)
}
}
func TestForEachErrorsOnNilFunc(t *testing.T) {
src := SliceSource([]string{"x"})
if err := Chain(src).ForEach(nil); !errors.Is(err, ErrNotForEachFunc) {
t.Errorf("got %v, want ErrNotForEachFunc", err)
}
}
// nonStreamSource has the right Stream arity but does not return a Stream[T];
// it must be rejected as a non-Source instead of panicking at a terminal.
type nonStreamSource struct{}
func (nonStreamSource) Stream(context.Context) int { return 42 }
func TestChainErrorsOnNonStreamSourceReturn(t *testing.T) {
if _, err := Chain(nonStreamSource{}).Collect(); !errors.Is(err, ErrNotSource) {
t.Errorf("Collect: got %v, want ErrNotSource", err)
}
if err := Chain(nonStreamSource{}).ForEach(func(int) error { return nil }); !errors.Is(err, ErrNotSource) {
t.Errorf("ForEach: got %v, want ErrNotSource", err)
}
}
// nonStreamCommand consumes a Stream but does not produce one; it must be
// rejected as a non-Command.
type nonStreamCommand struct{}
func (nonStreamCommand) Execute(_ context.Context, _ Stream[string]) int { return 0 }
func TestToErrorsOnNonStreamCommandReturn(t *testing.T) {
src := SliceSource([]string{"x"})
if _, err := Chain(src).To(nonStreamCommand{}).Collect(); !errors.Is(err, ErrNotCommand) {
t.Errorf("got %v, want ErrNotCommand", err)
}
}
// A nil context defaults to context.Background instead of panicking inside a
// reflected call, matching the tolerance callers expect from Run-style APIs.
func TestNilContextDefaultsToBackground(t *testing.T) {
src := SliceSource([]string{"a", "b"})
var nilCtx context.Context // deliberately nil — the tolerance under test
got, err := ChainContext(nilCtx, src).Collect()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if lines := got.([]string); len(lines) != 2 {
t.Errorf("got %v, want 2 items", lines)
}
}
// ctxRecordingCmd captures the context its Execute receives, so a test can
// observe which context a stage actually ran under.
type ctxRecordingCmd struct{ got *context.Context }
func (c ctxRecordingCmd) Execute(ctx context.Context, in Stream[string]) Stream[string] {
*c.got = ctx
return in
}
// ctxRecordingSink captures the context its Consume receives.
type ctxRecordingSink struct{ got *context.Context }
func (s ctxRecordingSink) Consume(ctx context.Context, in Stream[string]) (int, error) {
*s.got = ctx
items, err := in.Collect() // fully consume
return len(items), err
}
type ctxKey struct{}
// ToContext must run its stage under the EXPLICIT context, not the chain's.
func TestToContextOverridesStageContext(t *testing.T) {
chainCtx := context.WithValue(context.Background(), ctxKey{}, "chain")
stageCtx := context.WithValue(context.Background(), ctxKey{}, "stage")
var got context.Context
src := SliceSource([]string{"x"})
if _, err := ChainContext(chainCtx, src).ToContext(stageCtx, ctxRecordingCmd{got: &got}).Collect(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Value(ctxKey{}) != "stage" {
t.Errorf("stage ran under %v context, want the ToContext override", got.Value(ctxKey{}))
}
}
// SinkContext must consume under the EXPLICIT context, not the chain's.
func TestSinkContextOverridesConsumeContext(t *testing.T) {
chainCtx := context.WithValue(context.Background(), ctxKey{}, "chain")
sinkCtx := context.WithValue(context.Background(), ctxKey{}, "sink")
var got context.Context
src := SliceSource([]string{"x"})
if _, err := ChainContext(chainCtx, src).SinkContext(sinkCtx, ctxRecordingSink{got: &got}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Value(ctxKey{}) != "sink" {
t.Errorf("sink ran under %v context, want the SinkContext override", got.Value(ctxKey{}))
}
}
// A nil context passed to ToContext falls back to the chain's context.
func TestToContextNilFallsBackToChainContext(t *testing.T) {
chainCtx := context.WithValue(context.Background(), ctxKey{}, "chain")
var got context.Context
src := SliceSource([]string{"x"})
var nilCtx context.Context // deliberately nil — the fallback under test
if _, err := ChainContext(chainCtx, src).ToContext(nilCtx, ctxRecordingCmd{got: &got}).Collect(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Value(ctxKey{}) != "chain" {
t.Errorf("stage ran under %v context, want the chain's", got.Value(ctxKey{}))
}
}
// A nil context passed to SinkContext falls back to the chain's context.
func TestSinkContextNilFallsBackToChainContext(t *testing.T) {
chainCtx := context.WithValue(context.Background(), ctxKey{}, "chain")
var got context.Context
src := SliceSource([]string{"x"})
var nilCtx context.Context // deliberately nil — the fallback under test
if _, err := ChainContext(chainCtx, src).SinkContext(nilCtx, ctxRecordingSink{got: &got}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Value(ctxKey{}) != "chain" {
t.Errorf("sink ran under %v context, want the chain's", got.Value(ctxKey{}))
}
}