Test_Start_truncation tests that we can read command output without truncation even after the command has exited.
(t *testing.T)
| 56 | // Test_Start_truncation tests that we can read command output without truncation |
| 57 | // even after the command has exited. |
| 58 | func Test_Start_truncation(t *testing.T) { |
| 59 | t.Parallel() |
| 60 | |
| 61 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong) |
| 62 | defer cancel() |
| 63 | |
| 64 | pc, cmd, err := pty.Start(pty.CommandContext(ctx, cmdCount, argCount...)) |
| 65 | |
| 66 | require.NoError(t, err) |
| 67 | readDone := make(chan struct{}) |
| 68 | go func() { |
| 69 | defer close(readDone) |
| 70 | terminalReader := testutil.NewTerminalReader(t, pc.OutputReader()) |
| 71 | n := 1 |
| 72 | for n <= countEnd { |
| 73 | want := fmt.Sprintf("%d", n) |
| 74 | err := terminalReader.ReadUntilString(ctx, want) |
| 75 | assert.NoError(t, err, "want: %s", want) |
| 76 | if err != nil { |
| 77 | return |
| 78 | } |
| 79 | n++ |
| 80 | if (countEnd - n) < 100 { |
| 81 | // If the OS buffers the output, the process can exit even if |
| 82 | // we're not done reading. We want to slow our reads so that |
| 83 | // if there is a race between reading the data and it being |
| 84 | // truncated, we will lose and fail the test. |
| 85 | time.Sleep(testutil.IntervalFast) |
| 86 | } |
| 87 | } |
| 88 | // ensure we still get to EOF |
| 89 | endB := &bytes.Buffer{} |
| 90 | _, err := io.Copy(endB, pc.OutputReader()) |
| 91 | assert.NoError(t, err) |
| 92 | }() |
| 93 | |
| 94 | cmdDone := make(chan error, 1) |
| 95 | go func() { |
| 96 | cmdDone <- cmd.Wait() |
| 97 | }() |
| 98 | |
| 99 | select { |
| 100 | case err := <-cmdDone: |
| 101 | require.NoError(t, err) |
| 102 | case <-ctx.Done(): |
| 103 | t.Fatal("cmd.Wait() timed out") |
| 104 | } |
| 105 | |
| 106 | select { |
| 107 | case <-readDone: |
| 108 | // OK! |
| 109 | case <-ctx.Done(): |
| 110 | t.Fatal("read timed out") |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Test_Start_cancel_context tests that we can cancel the command context and kill the process. |
| 115 | func Test_Start_cancel_context(t *testing.T) { |
nothing calls this directly
no test coverage detected