Skip to content

Commit

Permalink
Stop timer when command exits in WaitTimeout (influxdata#6296)
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton authored and bitcharmer committed Oct 18, 2019
1 parent c24017b commit e9eeff3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func WaitTimeout(c *exec.Cmd, timeout time.Duration) error {

err := c.Wait()
if err == nil {
timer.Stop()
return nil
}

Expand Down
25 changes: 25 additions & 0 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"compress/gzip"
"io/ioutil"
"log"
"os/exec"
"testing"
"time"
Expand Down Expand Up @@ -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.")
Expand Down

0 comments on commit e9eeff3

Please sign in to comment.