Skip to content

Commit 249cea8

Browse files
authored
acc: kill tests after timeout (#2793)
## Changes - Kill tests after timeout expires. - New test configuration: Timeout/TimeoutWindows/TimeoutCloud to set a timeout for a test with possible overrides for known slow environments (windows and cloud). ## Why I saw a few cases where test suite is hanging. Some tests take a surprisingly long time currently (apps/run-local), having explicit setting that increases default timeout means we are tracking those. ## Tests New acceptance test: selftest/timeout. Manually checked its run time.
1 parent fdc9949 commit 249cea8

File tree

8 files changed

+56
-4
lines changed

8 files changed

+56
-4
lines changed

acceptance/acceptance_test.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,22 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont
368368
err = CopyDir(dir, tmpDir, inputs, outputs)
369369
require.NoError(t, err)
370370

371+
timeout := config.Timeout
372+
373+
if runtime.GOOS == "windows" {
374+
if isRunningOnCloud {
375+
timeout = max(timeout, config.TimeoutWindows, config.TimeoutCloud)
376+
} else {
377+
timeout = max(timeout, config.TimeoutWindows)
378+
}
379+
} else if isRunningOnCloud {
380+
timeout = max(timeout, config.TimeoutCloud)
381+
}
382+
383+
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
384+
defer cancelFunc()
371385
args := []string{"bash", "-euo", "pipefail", EntryPointScript}
372-
cmd := exec.Command(args[0], args[1:]...)
386+
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
373387
cmd.Env = os.Environ()
374388
cmd.Env = append(cmd.Env, "UNIQUE_NAME="+uniqueName)
375389
cmd.Env = append(cmd.Env, "TEST_TMP_DIR="+tmpDir)
@@ -806,15 +820,24 @@ func runWithLog(t *testing.T, cmd *exec.Cmd, out *os.File, tail bool) error {
806820
r, w := io.Pipe()
807821
cmd.Stdout = w
808822
cmd.Stderr = w
823+
processErrCh := make(chan error, 1)
824+
825+
cmd.Cancel = func() error {
826+
processErrCh <- errors.New("Test script killed due to a timeout")
827+
_ = cmd.Process.Kill()
828+
_ = w.Close()
829+
return nil
830+
}
809831

810832
start := time.Now()
811833
err := cmd.Start()
812-
require.NoError(t, err)
834+
if err != nil {
835+
return err
836+
}
813837

814-
processErrCh := make(chan error, 1)
815838
go func() {
816839
processErrCh <- cmd.Wait()
817-
w.Close()
840+
_ = w.Close()
818841
}()
819842

820843
reader := bufio.NewReader(r)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TimeoutWindows = '120s'

acceptance/cmd/workspace/apps/run-local/test.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
RecordRequests = false
2+
Timeout = '1m'
3+
TimeoutWindows = '5m'
24

35
Ignore = [
46
'.venv',

acceptance/internal/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ type TestConfig struct {
9696

9797
// List of keys for which to do string replacement value -> [KEY]. If not set, defaults to true.
9898
EnvRepl map[string]bool
99+
100+
// Maximum amount of time the test is allowed to run, will be killed after that.
101+
Timeout time.Duration
102+
103+
// On windows/local run, max(Timeout, TimeoutWindows) is used for timeout
104+
TimeoutWindows time.Duration
105+
106+
// For cloud tests, max(Timeout, TimeoutCloud) is used for timeout
107+
// For cloud+windows tests, max(Timeout, TimeoutWindows, TimeoutCloud) is used for timeout
108+
TimeoutCloud time.Duration
99109
}
100110

101111
type ServerStub struct {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
>>> sleep 2
3+
4+
Error: Test script killed due to a timeout

acceptance/selftest/timeout/script

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
trace sleep 2
2+
echo "This is never printed"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Manually check that test takes about 0.1s:
2+
# PASS: TestAccept/selftest/timeout (0.10s)
3+
Timeout = '100ms'
4+
TimeoutWindows = '300ms'
5+
TimeoutCloud = '300ms'

acceptance/test.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
Local = true
33
Cloud = false
44

5+
# default timeouts
6+
Timeout = '20s'
7+
TimeoutWindows = '60s'
8+
TimeoutCloud = '120s'
9+
510
Env.PYTHONDONTWRITEBYTECODE = "1"
611
EnvRepl.PYTHONDONTWRITEBYTECODE = false

0 commit comments

Comments
 (0)