| 97 | } |
| 98 | |
| 99 | func TestPipelineReadFromSlowly(t *testing.T) { |
| 100 | t.Parallel() |
| 101 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 102 | defer cancel() |
| 103 | |
| 104 | r, w := io.Pipe() |
| 105 | |
| 106 | var buf []byte |
| 107 | readErr := make(chan error, 1) |
| 108 | |
| 109 | go func() { |
| 110 | time.Sleep(200 * time.Millisecond) |
| 111 | var err error |
| 112 | buf, err = io.ReadAll(r) |
| 113 | readErr <- err |
| 114 | }() |
| 115 | |
| 116 | p := pipe.New(pipe.WithStdout(w)) |
| 117 | p.Add(pipe.Command("echo", "hello world")) |
| 118 | assert.NoError(t, p.Run(ctx)) |
| 119 | |
| 120 | time.Sleep(100 * time.Millisecond) |
| 121 | // It's not super-intuitive, but `w` has to be closed here so that |
| 122 | // the `ioutil.ReadAll()` call above knows that it's done: |
| 123 | _ = w.Close() |
| 124 | |
| 125 | assert.NoError(t, <-readErr) |
| 126 | assert.Equal(t, "hello world\n", string(buf)) |
| 127 | } |
| 128 | |
| 129 | func TestPipelineReadFromSlowly2(t *testing.T) { |
| 130 | if runtime.GOOS == "windows" { |