Add more debugging to run_with_timeout
Print more messages in run_with_timeout to make it easier to determine which log statements came before the timeout and the on_timeout command. Test: run_with_timeout_test.go Change-Id: I92ad663529fa03d91185ba382654a73f0eba1ab7
This commit is contained in:
@@ -47,7 +47,7 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if flag.NArg() < 1 {
|
if flag.NArg() < 1 {
|
||||||
fmt.Fprintln(os.Stderr, "command is required")
|
fmt.Fprintf(os.Stderr, "%s: error: command is required\n", os.Args[0])
|
||||||
usage()
|
usage()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,9 +55,9 @@ func main() {
|
|||||||
os.Stdin, os.Stdout, os.Stderr)
|
os.Stdin, os.Stdout, os.Stderr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||||
fmt.Fprintln(os.Stderr, "process exited with error:", exitErr.Error())
|
fmt.Fprintf(os.Stderr, "%s: process exited with error: %s\n", os.Args[0], exitErr.Error())
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(os.Stderr, "error:", err.Error())
|
fmt.Fprintf(os.Stderr, "%s: error: %s\n", os.Args[0], err.Error())
|
||||||
}
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -115,6 +115,7 @@ func runWithTimeout(command string, args []string, timeout time.Duration, onTime
|
|||||||
if timeout > 0 {
|
if timeout > 0 {
|
||||||
timeoutCh = time.After(timeout)
|
timeoutCh = time.After(timeout)
|
||||||
}
|
}
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case err := <-waitCh:
|
case err := <-waitCh:
|
||||||
@@ -126,10 +127,12 @@ func runWithTimeout(command string, args []string, timeout time.Duration, onTime
|
|||||||
// Continue below.
|
// Continue below.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(concurrentStderr, "%s: process timed out after %s\n", os.Args[0], time.Since(startTime))
|
||||||
// Process timed out before exiting.
|
// Process timed out before exiting.
|
||||||
defer cmd.Process.Signal(syscall.SIGKILL)
|
defer cmd.Process.Signal(syscall.SIGKILL)
|
||||||
|
|
||||||
if onTimeoutCmdStr != "" {
|
if onTimeoutCmdStr != "" {
|
||||||
|
fmt.Fprintf(concurrentStderr, "%s: running on_timeout command `%s`\n", os.Args[0], onTimeoutCmdStr)
|
||||||
onTimeoutCmd := exec.Command("sh", "-c", onTimeoutCmdStr)
|
onTimeoutCmd := exec.Command("sh", "-c", onTimeoutCmdStr)
|
||||||
onTimeoutCmd.Stdin, onTimeoutCmd.Stdout, onTimeoutCmd.Stderr = stdin, concurrentStdout, concurrentStderr
|
onTimeoutCmd.Stdin, onTimeoutCmd.Stdout, onTimeoutCmd.Stderr = stdin, concurrentStdout, concurrentStderr
|
||||||
onTimeoutCmd.Env = append(os.Environ(), fmt.Sprintf("PID=%d", cmd.Process.Pid))
|
onTimeoutCmd.Env = append(os.Environ(), fmt.Sprintf("PID=%d", cmd.Process.Pid))
|
||||||
|
@@ -17,6 +17,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -60,6 +61,7 @@ func Test_runWithTimeout(t *testing.T) {
|
|||||||
args: []string{"-c", "sleep 1 && echo foo"},
|
args: []string{"-c", "sleep 1 && echo foo"},
|
||||||
timeout: 1 * time.Millisecond,
|
timeout: 1 * time.Millisecond,
|
||||||
},
|
},
|
||||||
|
wantStderr: ".*: process timed out after .*\n",
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -71,6 +73,7 @@ func Test_runWithTimeout(t *testing.T) {
|
|||||||
onTimeoutCmd: "echo bar",
|
onTimeoutCmd: "echo bar",
|
||||||
},
|
},
|
||||||
wantStdout: "bar\n",
|
wantStdout: "bar\n",
|
||||||
|
wantStderr: ".*: process timed out after .*\n.*: running on_timeout command `echo bar`\n",
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -86,7 +89,7 @@ func Test_runWithTimeout(t *testing.T) {
|
|||||||
if gotStdout := stdout.String(); gotStdout != tt.wantStdout {
|
if gotStdout := stdout.String(); gotStdout != tt.wantStdout {
|
||||||
t.Errorf("runWithTimeout() gotStdout = %v, want %v", gotStdout, tt.wantStdout)
|
t.Errorf("runWithTimeout() gotStdout = %v, want %v", gotStdout, tt.wantStdout)
|
||||||
}
|
}
|
||||||
if gotStderr := stderr.String(); gotStderr != tt.wantStderr {
|
if gotStderr := stderr.String(); !regexp.MustCompile(tt.wantStderr).MatchString(gotStderr) {
|
||||||
t.Errorf("runWithTimeout() gotStderr = %v, want %v", gotStderr, tt.wantStderr)
|
t.Errorf("runWithTimeout() gotStderr = %v, want %v", gotStderr, tt.wantStderr)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user