From eb412791559ebab5a058c8104881ff3ec06f1428 Mon Sep 17 00:00:00 2001 From: Lane Seppala Date: Wed, 30 Aug 2023 03:13:46 +0000 Subject: [PATCH 1/2] Interpret value of kill_delay as milliseconds --- runner/config.go | 11 +++++++++++ runner/util_linux.go | 2 +- runner/util_unix.go | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/runner/config.go b/runner/config.go index 47f238ed..feb43593 100644 --- a/runner/config.go +++ b/runner/config.go @@ -340,6 +340,17 @@ func (c *Config) rerunDelay() time.Duration { return time.Duration(c.Build.RerunDelay) * time.Millisecond } +func (c *Config) killDelay() time.Duration { + // kill_delay can be specified as an integer or duration string + // interpret as milliseconds if less than the value of 1 millisecond + if c.Build.KillDelay < time.Millisecond { + return c.Build.KillDelay * time.Millisecond + } else { + // normalize kill delay to milliseconds + return time.Duration(c.Build.KillDelay.Milliseconds()) * time.Millisecond + } +} + func (c *Config) binPath() string { return filepath.Join(c.Root, c.Build.Bin) } diff --git a/runner/util_linux.go b/runner/util_linux.go index 5df91e84..658671b3 100644 --- a/runner/util_linux.go +++ b/runner/util_linux.go @@ -17,7 +17,7 @@ func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) { if err = syscall.Kill(-pid, syscall.SIGINT); err != nil { return } - time.Sleep(e.config.Build.KillDelay) + time.Sleep(e.config.killDelay()) } // https://stackoverflow.com/questions/22470193/why-wont-go-kill-a-child-process-correctly diff --git a/runner/util_unix.go b/runner/util_unix.go index 4590434f..afd47c89 100644 --- a/runner/util_unix.go +++ b/runner/util_unix.go @@ -18,7 +18,7 @@ func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) { if err = syscall.Kill(-pid, syscall.SIGINT); err != nil { return } - time.Sleep(e.config.Build.KillDelay) + time.Sleep(e.config.killDelay()) } // https://stackoverflow.com/questions/22470193/why-wont-go-kill-a-child-process-correctly err = syscall.Kill(-pid, syscall.SIGKILL) From c28d9a8b16daaada57fef420b0193e7f9c6653c7 Mon Sep 17 00:00:00 2001 From: Lane Seppala Date: Fri, 22 Dec 2023 10:58:20 -0700 Subject: [PATCH 2/2] Add tests for killDelay --- runner/config_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/runner/config_test.go b/runner/config_test.go index 5f999c94..77ca0683 100644 --- a/runner/config_test.go +++ b/runner/config_test.go @@ -6,6 +6,7 @@ import ( "runtime" "strings" "testing" + "time" ) const ( @@ -171,6 +172,33 @@ func TestReadConfigWithWrongPath(t *testing.T) { } } +func TestKillDelay(t *testing.T) { + config := Config{ + Build: cfgBuild{ + KillDelay: 1000, + }, + } + if config.killDelay() != (1000 * time.Millisecond) { + t.Fatal("expect KillDelay 1000 to be interpreted as 1000 milliseconds, got ", config.killDelay()) + } + config.Build.KillDelay = 1 + if config.killDelay() != (1 * time.Millisecond) { + t.Fatal("expect KillDelay 1 to be interpreted as 1 millisecond, got ", config.killDelay()) + } + config.Build.KillDelay = 1_000_000 + if config.killDelay() != (1 * time.Millisecond) { + t.Fatal("expect KillDelay 1_000_000 to be interpreted as 1 millisecond, got ", config.killDelay()) + } + config.Build.KillDelay = 100_000_000 + if config.killDelay() != (100 * time.Millisecond) { + t.Fatal("expect KillDelay 100_000_000 to be interpreted as 100 milliseconds, got ", config.killDelay()) + } + config.Build.KillDelay = 0 + if config.killDelay() != 0 { + t.Fatal("expect KillDelay 0 to be interpreted as 0, got ", config.killDelay()) + } +} + func contains(sl []string, target string) bool { for _, c := range sl { if c == target {