MCPcopy Create free account
hub / github.com/Shopify/goose / TestCommandStdoutPipe

Function TestCommandStdoutPipe

shell/shell_test.go:314–393  ·  view source on GitHub ↗

Verify we can stream while command is running

(t *testing.T)

Source from the content-addressed store, hash-verified

312
313// Verify we can stream while command is running
314func TestCommandStdoutPipe(t *testing.T) {
315 ctx := context.Background()
316 cmd := New(ctx, "cat")
317 c := cmd.Cmd()
318
319 stdin, err := c.StdinPipe()
320 assert.NoError(t, err)
321
322 stdout, err := c.StdoutPipe()
323 assert.NoError(t, err)
324
325 output := make(chan []byte)
326 read := make(chan struct{})
327 chanErr := make(chan error)
328 go func() {
329 for {
330 <-read
331 b := make([]byte, 10)
332 n, err := stdout.Read(b)
333 output <- b[:n]
334 chanErr <- err
335 if err == io.EOF {
336 return
337 }
338 }
339 }()
340
341 err = cmd.Start()
342 assert.NoError(t, err)
343
344 read <- struct{}{}
345 time.Sleep(100 * time.Millisecond)
346
347 select {
348 case b := <-output:
349 assert.NoError(t, <-chanErr)
350 assert.Empty(t, b, "should not have received output")
351 default:
352 }
353
354 _, err = stdin.Write([]byte("foo"))
355 assert.NoError(t, err)
356 time.Sleep(100 * time.Millisecond)
357
358 select {
359 case b := <-output:
360 assert.Equal(t, "foo", string(b))
361 assert.NoError(t, <-chanErr)
362 default:
363 assert.Fail(t, "should have output")
364 }
365
366 _, err = stdin.Write([]byte("bar"))
367 assert.NoError(t, err)
368 time.Sleep(100 * time.Millisecond)
369
370 _, err = stdin.Write([]byte("baz"))
371 assert.NoError(t, err)

Callers

nothing calls this directly

Calls 7

CloseMethod · 0.80
NewFunction · 0.70
CmdMethod · 0.65
StartMethod · 0.65
WaitMethod · 0.65
ReadMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected