Stop timer when command exits in WaitTimeout (#6296)

This commit is contained in:
Greg 2019-08-21 12:13:38 -06:00 committed by Daniel Nelson
parent 139fcc5805
commit 10671d2641
2 changed files with 26 additions and 0 deletions

View File

@ -237,6 +237,7 @@ func WaitTimeout(c *exec.Cmd, timeout time.Duration) error {
err := c.Wait() err := c.Wait()
if err == nil { if err == nil {
timer.Stop()
return nil return nil
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"io/ioutil" "io/ioutil"
"log"
"os/exec" "os/exec"
"testing" "testing"
"time" "time"
@ -64,6 +65,30 @@ func TestRunTimeout(t *testing.T) {
assert.True(t, elapsed < time.Millisecond*75) assert.True(t, elapsed < time.Millisecond*75)
} }
// Verifies behavior of a command that doesn't get killed.
func TestRunTimeoutFastExit(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test due to random failures.")
}
if echobin == "" {
t.Skip("'echo' binary not available on OS, skipping.")
}
cmd := exec.Command(echobin)
start := time.Now()
err := RunTimeout(cmd, time.Millisecond*20)
buf := &bytes.Buffer{}
log.SetOutput(buf)
elapsed := time.Since(start)
require.NoError(t, err)
// Verify that command gets killed in 20ms, with some breathing room
assert.True(t, elapsed < time.Millisecond*75)
// Verify "process already finished" log doesn't occur.
time.Sleep(time.Millisecond * 75)
require.Equal(t, "", buf.String())
}
func TestCombinedOutputTimeout(t *testing.T) { func TestCombinedOutputTimeout(t *testing.T) {
// TODO: Fix this test // TODO: Fix this test
t.Skip("Test failing too often, skip for now and revisit later.") t.Skip("Test failing too often, skip for now and revisit later.")