From 10671d2641f084d3ed96fd28fa76091bd9981878 Mon Sep 17 00:00:00 2001 From: Greg <2653109+glinton@users.noreply.github.com> Date: Wed, 21 Aug 2019 12:13:38 -0600 Subject: [PATCH] Stop timer when command exits in WaitTimeout (#6296) --- internal/internal.go | 1 + internal/internal_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/internal/internal.go b/internal/internal.go index 6f135938a..893f34383 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -237,6 +237,7 @@ func WaitTimeout(c *exec.Cmd, timeout time.Duration) error { err := c.Wait() if err == nil { + timer.Stop() return nil } diff --git a/internal/internal_test.go b/internal/internal_test.go index da2fe01c5..5e9b9a97c 100644 --- a/internal/internal_test.go +++ b/internal/internal_test.go @@ -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.")