mirror of
https://github.com/clearlinux/docker.git
synced 2026-09-06 21:51:33 +00:00
Merge pull request #10798 from ahmetalpbalkan/win-cli/TestEventsUntag-fix
integration-cli: Remove timeout dependency on TestEventsUntag
This commit is contained in:
+39
-17
@@ -65,27 +65,49 @@ func runCommandWithStdoutStderr(cmd *exec.Cmd) (stdout string, stderr string, ex
|
||||
return
|
||||
}
|
||||
|
||||
func runCommandWithOutputForDuration(cmd *exec.Cmd, duration time.Duration) (output string, exitCode int, timedOut bool, err error) {
|
||||
var outputBuffer bytes.Buffer
|
||||
if cmd.Stdout != nil {
|
||||
err = errors.New("cmd.Stdout already set")
|
||||
return
|
||||
}
|
||||
cmd.Stdout = &outputBuffer
|
||||
|
||||
if cmd.Stderr != nil {
|
||||
err = errors.New("cmd.Stderr already set")
|
||||
return
|
||||
}
|
||||
cmd.Stderr = &outputBuffer
|
||||
|
||||
done := make(chan error)
|
||||
go func() {
|
||||
exitErr := cmd.Run()
|
||||
exitCode = processExitCode(exitErr)
|
||||
done <- exitErr
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-time.After(duration):
|
||||
killErr := cmd.Process.Kill()
|
||||
if killErr != nil {
|
||||
fmt.Printf("failed to kill (pid=%d): %v\n", cmd.Process.Pid, killErr)
|
||||
}
|
||||
timedOut = true
|
||||
break
|
||||
case err = <-done:
|
||||
break
|
||||
}
|
||||
output = outputBuffer.String()
|
||||
return
|
||||
}
|
||||
|
||||
var ErrCmdTimeout = fmt.Errorf("command timed out")
|
||||
|
||||
func runCommandWithOutputAndTimeout(cmd *exec.Cmd, timeout time.Duration) (output string, exitCode int, err error) {
|
||||
done := make(chan error)
|
||||
go func() {
|
||||
output, exitCode, err = runCommandWithOutput(cmd)
|
||||
if err != nil || exitCode != 0 {
|
||||
done <- fmt.Errorf("failed to run command: %s", err)
|
||||
return
|
||||
}
|
||||
done <- nil
|
||||
}()
|
||||
select {
|
||||
case <-time.After(timeout):
|
||||
killFailed := cmd.Process.Kill()
|
||||
if killFailed == nil {
|
||||
fmt.Printf("failed to kill (pid=%d): %v\n", cmd.Process.Pid, err)
|
||||
}
|
||||
var timedOut bool
|
||||
output, exitCode, timedOut, err = runCommandWithOutputForDuration(cmd, timeout)
|
||||
if timedOut {
|
||||
err = ErrCmdTimeout
|
||||
case <-done:
|
||||
break
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user