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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
|
package pipe_test
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"github.com/github/git-sizer/internal/pipe"
)
func TestMain(m *testing.M) {
// Check whether this package's test suite leaks any goroutines:
goleak.VerifyTestMain(m)
}
func TestPipelineFirstStageFailsToStart(t *testing.T) {
t.Parallel()
ctx := context.Background()
startErr := errors.New("foo")
p := pipe.New()
p.Add(
ErrorStartingStage{startErr},
ErrorStartingStage{errors.New("this error should never happen")},
)
assert.ErrorIs(t, p.Run(ctx), startErr)
}
func TestPipelineSecondStageFailsToStart(t *testing.T) {
t.Parallel()
ctx := context.Background()
startErr := errors.New("foo")
p := pipe.New()
p.Add(
seqFunction(20000),
ErrorStartingStage{startErr},
)
assert.ErrorIs(t, p.Run(ctx), startErr)
}
func TestPipelineSingleCommandOutput(t *testing.T) {
t.Parallel()
ctx := context.Background()
p := pipe.New()
p.Add(pipe.Command("echo", "hello world"))
out, err := p.Output(ctx)
if assert.NoError(t, err) {
assert.EqualValues(t, "hello world\n", out)
}
}
func TestPipelineSingleCommandWithStdout(t *testing.T) {
t.Parallel()
ctx := context.Background()
stdout := &bytes.Buffer{}
p := pipe.New(pipe.WithStdout(stdout))
p.Add(pipe.Command("echo", "hello world"))
if assert.NoError(t, p.Run(ctx)) {
assert.Equal(t, "hello world\n", stdout.String())
}
}
func TestNontrivialPipeline(t *testing.T) {
t.Parallel()
ctx := context.Background()
p := pipe.New()
p.Add(
pipe.Command("echo", "hello world"),
pipe.Command("sed", "s/hello/goodbye/"),
)
out, err := p.Output(ctx)
if assert.NoError(t, err) {
assert.EqualValues(t, "goodbye world\n", out)
}
}
func TestPipelineReadFromSlowly(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
r, w := io.Pipe()
var buf []byte
readErr := make(chan error, 1)
go func() {
time.Sleep(200 * time.Millisecond)
var err error
buf, err = io.ReadAll(r)
readErr <- err
}()
p := pipe.New(pipe.WithStdout(w))
p.Add(pipe.Command("echo", "hello world"))
assert.NoError(t, p.Run(ctx))
time.Sleep(100 * time.Millisecond)
// It's not super-intuitive, but `w` has to be closed here so that
// the `ioutil.ReadAll()` call above knows that it's done:
_ = w.Close()
assert.NoError(t, <-readErr)
assert.Equal(t, "hello world\n", string(buf))
}
func TestPipelineReadFromSlowly2(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("FIXME: test skipped on Windows: 'seq' unavailable")
}
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
r, w := io.Pipe()
var buf []byte
readErr := make(chan error, 1)
go func() {
time.Sleep(100 * time.Millisecond)
for {
var c [1]byte
_, err := r.Read(c[:])
if err != nil {
if err == io.EOF {
readErr <- nil
return
}
readErr <- err
return
}
buf = append(buf, c[0])
time.Sleep(1 * time.Millisecond)
}
}()
p := pipe.New(pipe.WithStdout(w))
p.Add(pipe.Command("seq", "100"))
assert.NoError(t, p.Run(ctx))
time.Sleep(200 * time.Millisecond)
// It's not super-intuitive, but `w` has to be closed here so that
// the `ioutil.ReadAll()` call above knows that it's done:
_ = w.Close()
assert.NoError(t, <-readErr)
assert.Equal(t, 292, len(buf))
}
func TestPipelineTwoCommandsPiping(t *testing.T) {
t.Parallel()
ctx := context.Background()
p := pipe.New()
p.Add(pipe.Command("echo", "hello world"))
assert.Panics(t, func() { p.Add(pipe.Command("")) })
out, err := p.Output(ctx)
if assert.NoError(t, err) {
assert.EqualValues(t, "hello world\n", out)
}
}
func TestPipelineDir(t *testing.T) {
t.Parallel()
ctx := context.Background()
wdir, err := os.Getwd()
require.NoError(t, err)
dir, err := ioutil.TempDir(wdir, "pipeline-test-")
require.NoError(t, err)
defer os.RemoveAll(dir)
p := pipe.New(pipe.WithDir(dir))
switch runtime.GOOS {
case "windows":
p.Add(pipe.Command("bash", "-c", "pwd -W"))
default:
p.Add(pipe.Command("pwd"))
}
out, err := p.Output(ctx)
if assert.NoError(t, err) {
assert.Equal(t, filepath.Clean(dir), filepath.Clean(strings.TrimSuffix(string(out), "\n")))
}
}
func TestPipelineExit(t *testing.T) {
t.Parallel()
ctx := context.Background()
p := pipe.New()
p.Add(
pipe.Command("false"),
pipe.Command("true"),
)
assert.EqualError(t, p.Run(ctx), "false: exit status 1")
}
func TestPipelineStderr(t *testing.T) {
t.Parallel()
ctx := context.Background()
dir, err := ioutil.TempDir("", "pipeline-test-")
require.NoError(t, err)
defer os.RemoveAll(dir)
p := pipe.New(pipe.WithDir(dir))
p.Add(pipe.Command("ls", "doesnotexist"))
_, err = p.Output(ctx)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "ls: exit status")
}
}
func TestPipelineInterrupted(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("FIXME: test skipped on Windows: 'sleep' unavailable")
}
t.Parallel()
stdout := &bytes.Buffer{}
p := pipe.New(pipe.WithStdout(stdout))
p.Add(pipe.Command("sleep", "10"))
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
err := p.Start(ctx)
require.NoError(t, err)
err = p.Wait()
assert.ErrorIs(t, err, context.DeadlineExceeded)
}
func TestPipelineCanceled(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("FIXME: test skipped on Windows: 'sleep' unavailable")
}
t.Parallel()
stdout := &bytes.Buffer{}
p := pipe.New(pipe.WithStdout(stdout))
p.Add(pipe.Command("sleep", "10"))
ctx, cancel := context.WithCancel(context.Background())
err := p.Start(ctx)
require.NoError(t, err)
cancel()
err = p.Wait()
assert.ErrorIs(t, err, context.Canceled)
}
// Verify the correct error if a command in the pipeline exits before
// reading all of its predecessor's output. Note that the amount of
// unread output in this case *does fit* within the OS-level pipe
// buffer.
func TestLittleEPIPE(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("FIXME: test skipped on Windows: 'sleep' unavailable")
}
t.Parallel()
p := pipe.New()
p.Add(
pipe.Command("sh", "-c", "sleep 1; echo foo"),
pipe.Command("true"),
)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err := p.Run(ctx)
assert.EqualError(t, err, "sh: signal: broken pipe")
}
// Verify the correct error if one command in the pipeline exits
// before reading all of its predecessor's output. Note that the
// amount of unread output in this case *does not fit* within the
// OS-level pipe buffer.
func TestBigEPIPE(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("FIXME: test skipped on Windows: 'seq' unavailable")
}
t.Parallel()
p := pipe.New()
p.Add(
pipe.Command("seq", "10000000"),
pipe.Command("true"),
)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
err := p.Run(ctx)
assert.EqualError(t, err, "seq: signal: broken pipe")
}
// Verify the correct error if one command in the pipeline exits
// before reading all of its predecessor's output. Note that the
// amount of unread output in this case *does not fit* within the
// OS-level pipe buffer.
func TestIgnoredSIGPIPE(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("FIXME: test skipped on Windows: 'seq' unavailable")
}
t.Parallel()
p := pipe.New()
p.Add(
pipe.IgnoreError(pipe.Command("seq", "100000"), pipe.IsSIGPIPE),
pipe.Command("echo", "foo"),
)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
out, err := p.Output(ctx)
assert.NoError(t, err)
assert.EqualValues(t, "foo\n", out)
}
func TestFunction(t *testing.T) {
t.Parallel()
ctx := context.Background()
p := pipe.New()
p.Add(
pipe.Print("hello world"),
pipe.Function(
"farewell",
func(_ context.Context, _ pipe.Env, stdin io.Reader, stdout io.Writer) error {
buf, err := io.ReadAll(stdin)
if err != nil {
return err
}
if string(buf) != "hello world" {
return fmt.Errorf("expected \"hello world\"; got %q", string(buf))
}
_, err = stdout.Write([]byte("goodbye, cruel world"))
return err
},
),
)
out, err := p.Output(ctx)
assert.NoError(t, err)
assert.EqualValues(t, "goodbye, cruel world", out)
}
func TestPipelineWithFunction(t *testing.T) {
t.Parallel()
ctx := context.Background()
p := pipe.New()
p.Add(
pipe.Command("echo", "-n", "hello world"),
pipe.Function(
"farewell",
func(_ context.Context, _ pipe.Env, stdin io.Reader, stdout io.Writer) error {
buf, err := io.ReadAll(stdin)
if err != nil {
return err
}
if string(buf) != "hello world" {
return fmt.Errorf("expected \"hello world\"; got %q", string(buf))
}
_, err = stdout.Write([]byte("goodbye, cruel world"))
return err
},
),
pipe.Command("tr", "a-z", "A-Z"),
)
out, err := p.Output(ctx)
assert.NoError(t, err)
assert.EqualValues(t, "GOODBYE, CRUEL WORLD", out)
}
type ErrorStartingStage struct {
err error
}
func (s ErrorStartingStage) Name() string {
return "errorStartingStage"
}
func (s ErrorStartingStage) Start(
ctx context.Context, env pipe.Env, stdin io.ReadCloser,
) (io.ReadCloser, error) {
return io.NopCloser(&bytes.Buffer{}), s.err
}
func (s ErrorStartingStage) Wait() error {
return nil
}
func seqFunction(n int) pipe.Stage {
return pipe.Function(
"seq",
func(_ context.Context, _ pipe.Env, _ io.Reader, stdout io.Writer) error {
for i := 1; i <= n; i++ {
_, err := fmt.Fprintf(stdout, "%d\n", i)
if err != nil {
return err
}
}
return nil
},
)
}
func TestPipelineWithLinewiseFunction(t *testing.T) {
t.Parallel()
ctx := context.Background()
p := pipe.New()
// Print the numbers from 1 to 20 (generated from scratch):
p.Add(
seqFunction(20),
// Discard all but the multiples of 5, and emit the results
// separated by spaces on one line:
pipe.LinewiseFunction(
"multiples-of-5",
func(_ context.Context, _ pipe.Env, line []byte, w *bufio.Writer) error {
n, err := strconv.Atoi(string(line))
if err != nil {
return err
}
if n%5 != 0 {
return nil
}
_, err = fmt.Fprintf(w, " %d", n)
return err
},
),
// Read the words and square them, emitting the results one per
// line:
pipe.ScannerFunction(
"square-multiples-of-5",
func(r io.Reader) (pipe.Scanner, error) {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanWords)
return scanner, nil
},
func(_ context.Context, _ pipe.Env, line []byte, w *bufio.Writer) error {
n, err := strconv.Atoi(string(line))
if err != nil {
return err
}
_, err = fmt.Fprintf(w, "%d\n", n*n)
return err
},
),
)
out, err := p.Output(ctx)
assert.NoError(t, err)
assert.EqualValues(t, "25\n100\n225\n400\n", out)
}
func TestScannerAlwaysFlushes(t *testing.T) {
t.Parallel()
ctx := context.Background()
var length int64
p := pipe.New()
// Print the numbers from 1 to 20 (generated from scratch):
p.Add(
pipe.IgnoreError(
seqFunction(20),
pipe.IsPipeError,
),
// Pass the numbers through up to 7, then exit with an
// ignored error:
pipe.IgnoreError(
pipe.LinewiseFunction(
"error-after-7",
func(_ context.Context, _ pipe.Env, line []byte, w *bufio.Writer) error {
fmt.Fprintf(w, "%s\n", line)
if string(line) == "7" {
return errors.New("ignore")
}
return nil
},
),
func(err error) bool {
return err.Error() == "ignore"
},
),
// Read the numbers and add them into the sum:
pipe.Function(
"compute-length",
func(_ context.Context, _ pipe.Env, stdin io.Reader, _ io.Writer) error {
var err error
length, err = io.Copy(io.Discard, stdin)
return err
},
),
)
err := p.Run(ctx)
assert.NoError(t, err)
// Make sure that all of the bytes emitted before the second
// stage's error were received by the third stage:
assert.EqualValues(t, 14, length)
}
func TestScannerFinishEarly(t *testing.T) {
t.Parallel()
ctx := context.Background()
var length int64
p := pipe.New()
p.Add(
// Print the numbers from 1 to 20 (generated from scratch):
seqFunction(20),
// Pass the numbers through up to 7, then exit with an ignored
// error:
pipe.LinewiseFunction(
"finish-after-7",
func(_ context.Context, _ pipe.Env, line []byte, w *bufio.Writer) error {
fmt.Fprintf(w, "%s\n", line)
if string(line) == "7" {
return pipe.FinishEarly
}
return nil
},
),
// Read the numbers and add them into the sum:
pipe.Function(
"compute-length",
func(_ context.Context, _ pipe.Env, stdin io.Reader, _ io.Writer) error {
var err error
length, err = io.Copy(io.Discard, stdin)
return err
},
),
)
err := p.Run(ctx)
assert.NoError(t, err)
// Make sure that all of the bytes emitted before the second
// stage's error were received by the third stage:
assert.EqualValues(t, 14, length)
}
func TestPrintln(t *testing.T) {
t.Parallel()
ctx := context.Background()
p := pipe.New()
p.Add(pipe.Println("Look Ma, no hands!"))
out, err := p.Output(ctx)
if assert.NoError(t, err) {
assert.EqualValues(t, "Look Ma, no hands!\n", out)
}
}
func TestPrintf(t *testing.T) {
t.Parallel()
ctx := context.Background()
p := pipe.New()
p.Add(pipe.Printf("Strangely recursive: %T", p))
out, err := p.Output(ctx)
if assert.NoError(t, err) {
assert.EqualValues(t, "Strangely recursive: *pipe.Pipeline", out)
}
}
func TestErrors(t *testing.T) {
t.Parallel()
ctx := context.Background()
err1 := errors.New("error1")
err2 := errors.New("error2")
for _, tc := range []struct {
name string
stages []pipe.Stage
expectedErr error
}{
{
name: "no-error",
stages: []pipe.Stage{
pipe.Function("noop1", genErr(nil)),
pipe.Function("noop2", genErr(nil)),
pipe.Function("noop3", genErr(nil)),
},
expectedErr: nil,
},
{
name: "lonely-error",
stages: []pipe.Stage{
pipe.Function("err1", genErr(err1)),
},
expectedErr: err1,
},
{
name: "error",
stages: []pipe.Stage{
pipe.Function("noop1", genErr(nil)),
pipe.Function("err1", genErr(err1)),
pipe.Function("noop2", genErr(nil)),
},
expectedErr: err1,
},
{
name: "two-consecutive-errors",
stages: []pipe.Stage{
pipe.Function("noop1", genErr(nil)),
pipe.Function("err1", genErr(err1)),
pipe.Function("err2", genErr(err2)),
pipe.Function("noop2", genErr(nil)),
},
expectedErr: err1,
},
{
name: "pipe-then-error",
stages: []pipe.Stage{
pipe.Function("noop1", genErr(nil)),
pipe.Function("pipe-error", genErr(io.ErrClosedPipe)),
pipe.Function("err1", genErr(err1)),
pipe.Function("noop2", genErr(nil)),
},
expectedErr: err1,
},
{
name: "error-then-pipe",
stages: []pipe.Stage{
pipe.Function("noop1", genErr(nil)),
pipe.Function("err1", genErr(err1)),
pipe.Function("pipe-error", genErr(io.ErrClosedPipe)),
pipe.Function("noop2", genErr(nil)),
},
expectedErr: err1,
},
{
name: "two-spaced-errors",
stages: []pipe.Stage{
pipe.Function("noop1", genErr(nil)),
pipe.Function("err1", genErr(err1)),
pipe.Function("noop2", genErr(nil)),
pipe.Function("err2", genErr(err2)),
pipe.Function("noop3", genErr(nil)),
},
expectedErr: err1,
},
{
name: "finish-early-ignored",
stages: []pipe.Stage{
pipe.Function("noop1", genErr(nil)),
pipe.Function("finish-early1", genErr(pipe.FinishEarly)),
pipe.Function("noop2", genErr(nil)),
pipe.Function("finish-early2", genErr(pipe.FinishEarly)),
pipe.Function("noop3", genErr(nil)),
},
expectedErr: nil,
},
{
name: "error-before-finish-early",
stages: []pipe.Stage{
pipe.Function("err1", genErr(err1)),
pipe.Function("finish-early", genErr(pipe.FinishEarly)),
},
expectedErr: err1,
},
{
name: "error-after-finish-early",
stages: []pipe.Stage{
pipe.Function("finish-early", genErr(pipe.FinishEarly)),
pipe.Function("err1", genErr(err1)),
},
expectedErr: err1,
},
{
name: "pipe-then-finish-early",
stages: []pipe.Stage{
pipe.Function("pipe-error", genErr(io.ErrClosedPipe)),
pipe.Function("finish-early", genErr(pipe.FinishEarly)),
},
expectedErr: nil,
},
{
name: "pipe-then-two-finish-early",
stages: []pipe.Stage{
pipe.Function("pipe-error", genErr(io.ErrClosedPipe)),
pipe.Function("finish-early1", genErr(pipe.FinishEarly)),
pipe.Function("finish-early2", genErr(pipe.FinishEarly)),
},
expectedErr: nil,
},
{
name: "two-pipe-then-finish-early",
stages: []pipe.Stage{
pipe.Function("pipe-error1", genErr(io.ErrClosedPipe)),
pipe.Function("pipe-error2", genErr(io.ErrClosedPipe)),
pipe.Function("finish-early", genErr(pipe.FinishEarly)),
},
expectedErr: nil,
},
{
name: "pipe-then-finish-early-with-gap",
stages: []pipe.Stage{
pipe.Function("pipe-error", genErr(io.ErrClosedPipe)),
pipe.Function("noop", genErr(nil)),
pipe.Function("finish-early1", genErr(pipe.FinishEarly)),
},
expectedErr: io.ErrClosedPipe,
},
{
name: "finish-early-then-pipe",
stages: []pipe.Stage{
pipe.Function("finish-early", genErr(pipe.FinishEarly)),
pipe.Function("pipe-error", genErr(io.ErrClosedPipe)),
},
expectedErr: io.ErrClosedPipe,
},
{
name: "error-then-pipe-then-finish-early",
stages: []pipe.Stage{
pipe.Function("err1", genErr(err1)),
pipe.Function("pipe-error", genErr(io.ErrClosedPipe)),
pipe.Function("finish-early", genErr(pipe.FinishEarly)),
},
expectedErr: err1,
},
{
name: "pipe-then-error-then-finish-early",
stages: []pipe.Stage{
pipe.Function("pipe-error", genErr(io.ErrClosedPipe)),
pipe.Function("err1", genErr(err1)),
pipe.Function("finish-early", genErr(pipe.FinishEarly)),
},
expectedErr: err1,
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
p := pipe.New()
p.Add(tc.stages...)
err := p.Run(ctx)
if tc.expectedErr == nil {
assert.NoError(t, err)
} else {
assert.ErrorIs(t, err, tc.expectedErr)
}
})
}
}
func BenchmarkSingleProgram(b *testing.B) {
ctx := context.Background()
for i := 0; i < b.N; i++ {
p := pipe.New()
p.Add(
pipe.Command("true"),
)
assert.NoError(b, p.Run(ctx))
}
}
func BenchmarkTenPrograms(b *testing.B) {
ctx := context.Background()
for i := 0; i < b.N; i++ {
p := pipe.New()
p.Add(
pipe.Command("echo", "hello world"),
pipe.Command("cat"),
pipe.Command("cat"),
pipe.Command("cat"),
pipe.Command("cat"),
pipe.Command("cat"),
pipe.Command("cat"),
pipe.Command("cat"),
pipe.Command("cat"),
pipe.Command("cat"),
)
out, err := p.Output(ctx)
if assert.NoError(b, err) {
assert.EqualValues(b, "hello world\n", out)
}
}
}
func BenchmarkTenFunctions(b *testing.B) {
ctx := context.Background()
for i := 0; i < b.N; i++ {
p := pipe.New()
p.Add(
pipe.Println("hello world"),
pipe.Function("copy1", catFn),
pipe.Function("copy2", catFn),
pipe.Function("copy3", catFn),
pipe.Function("copy4", catFn),
pipe.Function("copy5", catFn),
pipe.Function("copy6", catFn),
pipe.Function("copy7", catFn),
pipe.Function("copy8", catFn),
pipe.Function("copy9", catFn),
)
out, err := p.Output(ctx)
if assert.NoError(b, err) {
assert.EqualValues(b, "hello world\n", out)
}
}
}
func BenchmarkTenMixedStages(b *testing.B) {
ctx := context.Background()
for i := 0; i < b.N; i++ {
p := pipe.New()
p.Add(
pipe.Command("echo", "hello world"),
pipe.Function("copy1", catFn),
pipe.Command("cat"),
pipe.Function("copy2", catFn),
pipe.Command("cat"),
pipe.Function("copy3", catFn),
pipe.Command("cat"),
pipe.Function("copy4", catFn),
pipe.Command("cat"),
pipe.Function("copy5", catFn),
)
out, err := p.Output(ctx)
if assert.NoError(b, err) {
assert.EqualValues(b, "hello world\n", out)
}
}
}
func catFn(_ context.Context, _ pipe.Env, stdin io.Reader, stdout io.Writer) error {
_, err := io.Copy(stdout, stdin)
return err
}
func genErr(err error) pipe.StageFunc {
return func(_ context.Context, _ pipe.Env, _ io.Reader, _ io.Writer) error {
return err
}
}
|