github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/k8s/client/cmd_test.go (about) 1 package client 2 3 import ( 4 "io" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestReadStdPipeWithLongString(t *testing.T) { 12 // Create a string with a million characters ('a'). 13 longString := strings.Repeat("a", 1000000) 14 15 // Use an io.Pipe to simulate the stdout or stderr pipe. 16 reader, writer := io.Pipe() 17 18 // Channel to communicate errors from the writing goroutine. 19 errChan := make(chan error, 1) 20 21 // Write the long string to the pipe in a goroutine. 22 go func() { 23 _, err := writer.Write([]byte(longString)) 24 if err != nil { 25 // Send any errors to the main test goroutine via the channel. 26 errChan <- err 27 } 28 writer.Close() 29 errChan <- nil // Send nil to indicate successful write. 30 }() 31 32 // Variable to store the output from the readStdPipe function. 33 var output string 34 outputFunction := func(s string) { 35 output = s 36 } 37 38 // Call the readStdPipe function with the reader part of the pipe. 39 readStdPipe(reader, outputFunction) 40 41 // Check for errors from the write goroutine. 42 err := <-errChan 43 require.NoError(t, err, "Failed to write to pipe") 44 require.Equal(t, longString, output, "Output did not match the input long string") 45 }