From 5d212c9c98a91c51eb47a00f59c903dd333b5814 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 16 Jan 2025 16:13:29 +0100 Subject: [PATCH 1/2] Kill acceptance test after timeout --- acceptance/acceptance_test.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/acceptance/acceptance_test.go b/acceptance/acceptance_test.go index e6eb380a41..9f8eb2b892 100644 --- a/acceptance/acceptance_test.go +++ b/acceptance/acceptance_test.go @@ -12,6 +12,7 @@ import ( "slices" "sort" "strings" + "syscall" "testing" "time" @@ -22,7 +23,10 @@ import ( "github.com/stretchr/testify/require" ) -var KeepTmp = os.Getenv("KEEP_TMP") != "" +var ( + KeepTmp = os.Getenv("KEEP_TMP") != "" + defaultTimeout = 10 * time.Second +) const ( EntryPointScript = "script" @@ -155,7 +159,13 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont cmd.Env = append(os.Environ(), "GOCOVERDIR="+coverDir) } cmd.Dir = tmpDir + if runtime.GOOS != "windows" { + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + } + + timer := KillTimer(cmd, defaultTimeout) outB, err := cmd.CombinedOutput() + timer.Stop() out := formatOutput(string(outB), err) out = repls.Replace(out) @@ -373,3 +383,20 @@ func CopyDir(src, dst string, inputs, outputs map[string]bool) error { return copyFile(path, destPath) }) } + +func KillTimer(cmd *exec.Cmd, timeout time.Duration) *time.Timer { + return time.AfterFunc(timeout, func() { + if cmd.Process == nil { + return + } + if runtime.GOOS == "windows" { + // best effort, not killing the group like on mac/linux + _ = cmd.Process.Kill() + } else { + pgid, err := syscall.Getpgid(cmd.Process.Pid) + if err == nil { + _ = syscall.Kill(-pgid, syscall.SIGKILL) + } + } + }) +} From 023ac9a9df7c31189bacdbd85ddc2e36099ab502 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 16 Jan 2025 17:00:33 +0100 Subject: [PATCH 2/2] trigger timeout --- acceptance/bundle/variables/empty/script | 1 + 1 file changed, 1 insertion(+) diff --git a/acceptance/bundle/variables/empty/script b/acceptance/bundle/variables/empty/script index 72555b332a..f160454382 100644 --- a/acceptance/bundle/variables/empty/script +++ b/acceptance/bundle/variables/empty/script @@ -1 +1,2 @@ +sleep 12 $CLI bundle validate