Stop timer when command exits in WaitTimeout (#6296)
This commit is contained in:
parent
139fcc5805
commit
10671d2641
|
@ -237,6 +237,7 @@ func WaitTimeout(c *exec.Cmd, timeout time.Duration) error {
|
|||
|
||||
err := c.Wait()
|
||||
if err == nil {
|
||||
timer.Stop()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"compress/gzip"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -64,6 +65,30 @@ func TestRunTimeout(t *testing.T) {
|
|||
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) {
|
||||
// TODO: Fix this test
|
||||
t.Skip("Test failing too often, skip for now and revisit later.")
|
||||
|
|
Loading…
Reference in New Issue