Skip to content

Commit

Permalink
Interpret value of kill_delay as milliseconds (#462)
Browse files Browse the repository at this point in the history
* Interpret value of kill_delay as milliseconds

* Add tests for killDelay
  • Loading branch information
lseppala authored Dec 27, 2023
1 parent bd33bda commit 56d3d58
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
11 changes: 11 additions & 0 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,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)
}
Expand Down
28 changes: 28 additions & 0 deletions runner/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime"
"strings"
"testing"
"time"
)

const (
Expand Down Expand Up @@ -175,6 +176,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 {
Expand Down
2 changes: 1 addition & 1 deletion runner/util_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion runner/util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 56d3d58

Please sign in to comment.