(t *testing.T)
| 491 | } |
| 492 | |
| 493 | func TestScannerAlwaysFlushes(t *testing.T) { |
| 494 | t.Parallel() |
| 495 | ctx := context.Background() |
| 496 | |
| 497 | var length int64 |
| 498 | |
| 499 | p := pipe.New() |
| 500 | // Print the numbers from 1 to 20 (generated from scratch): |
| 501 | p.Add( |
| 502 | pipe.IgnoreError( |
| 503 | seqFunction(20), |
| 504 | pipe.IsPipeError, |
| 505 | ), |
| 506 | // Pass the numbers through up to 7, then exit with an |
| 507 | // ignored error: |
| 508 | pipe.IgnoreError( |
| 509 | pipe.LinewiseFunction( |
| 510 | "error-after-7", |
| 511 | func(_ context.Context, _ pipe.Env, line []byte, w *bufio.Writer) error { |
| 512 | fmt.Fprintf(w, "%s\n", line) |
| 513 | if string(line) == "7" { |
| 514 | return errors.New("ignore") |
| 515 | } |
| 516 | return nil |
| 517 | }, |
| 518 | ), |
| 519 | func(err error) bool { |
| 520 | return err.Error() == "ignore" |
| 521 | }, |
| 522 | ), |
| 523 | // Read the numbers and add them into the sum: |
| 524 | pipe.Function( |
| 525 | "compute-length", |
| 526 | func(_ context.Context, _ pipe.Env, stdin io.Reader, _ io.Writer) error { |
| 527 | var err error |
| 528 | length, err = io.Copy(io.Discard, stdin) |
| 529 | return err |
| 530 | }, |
| 531 | ), |
| 532 | ) |
| 533 | |
| 534 | err := p.Run(ctx) |
| 535 | assert.NoError(t, err) |
| 536 | // Make sure that all of the bytes emitted before the second |
| 537 | // stage's error were received by the third stage: |
| 538 | assert.EqualValues(t, 14, length) |
| 539 | } |
| 540 | |
| 541 | func TestScannerFinishEarly(t *testing.T) { |
| 542 | t.Parallel() |
nothing calls this directly
no test coverage detected