Test_Start_cancel_context tests that we can cancel the command context and kill the process.
(t *testing.T)
| 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) { |
| 116 | t.Parallel() |
| 117 | |
| 118 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium) |
| 119 | defer cancel() |
| 120 | cmdCtx, cmdCancel := context.WithCancel(ctx) |
| 121 | |
| 122 | pc, cmd, err := pty.Start(pty.CommandContext(cmdCtx, cmdSleep, argSleep...)) |
| 123 | require.NoError(t, err) |
| 124 | defer func() { |
| 125 | _ = pc.Close() |
| 126 | }() |
| 127 | cmdCancel() |
| 128 | |
| 129 | cmdDone := make(chan struct{}) |
| 130 | go func() { |
| 131 | defer close(cmdDone) |
| 132 | _ = cmd.Wait() |
| 133 | }() |
| 134 | |
| 135 | select { |
| 136 | case <-cmdDone: |
| 137 | // OK! |
| 138 | case <-ctx.Done(): |
| 139 | t.Error("cmd.Wait() timed out") |
| 140 | } |
| 141 | } |