src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/prog/progtest/progtest_test.go (about)

     1  package progtest
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"src.elv.sh/pkg/prog"
     8  )
     9  
    10  // Verify we don't deadlock if more output is written to stdout than can be
    11  // buffered by a pipe.
    12  func TestOutputCaptureDoesNotDeadlock(t *testing.T) {
    13  	Test(t, noisyProgram{},
    14  		ThatElvish().WritesStdoutContaining("hello"),
    15  	)
    16  }
    17  
    18  type noisyProgram struct{}
    19  
    20  func (noisyProgram) RegisterFlags(f *prog.FlagSet) {}
    21  
    22  func (noisyProgram) Run(fds [3]*os.File, args []string) error {
    23  	// We need enough data to verify whether we're likely to deadlock due to
    24  	// filling the pipe before the test completes. Pipes typically buffer 8 to
    25  	// 128 KiB.
    26  	bytes := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
    27  	for i := 0; i < 128*1024/len(bytes); i++ {
    28  		fds[1].Write(bytes)
    29  	}
    30  	fds[1].WriteString("hello")
    31  	return nil
    32  }