Skip to content

Commit

Permalink
testutil: increase settle timeout when running -race
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Pires <[email protected]>
  • Loading branch information
miguelpires committed Apr 29, 2024
1 parent 2ea10e8 commit 4be3095
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
8 changes: 6 additions & 2 deletions testutil/timeouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package testutil

import (
"os"
"runtime"
"time"
)
Expand All @@ -31,12 +32,15 @@ var runtimeGOARCH = runtime.GOARCH
//
// This should only be used in tests and is a bit of a guess.
func HostScaledTimeout(t time.Duration) time.Duration {
switch runtimeGOARCH {
case "riscv64":
switch {
case runtimeGOARCH == "riscv64":
// virt riscv64 builders are 5x times slower than
// armhf when building golang-1.14. These tests
// timeout, hence bump timeouts by 6x
return t * 6
case os.Getenv("GO_TEST_RACE") == "1":
// the -race detector makes test execution time 2-20x slower
return t * 5
default:
return t
}
Expand Down
73 changes: 67 additions & 6 deletions testutil/timeouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package testutil_test

import (
"os"
"time"

. "gopkg.in/check.v1"
Expand All @@ -31,15 +32,75 @@ var _ = Suite(&TimeoutTestSuite{})

type TimeoutTestSuite struct{}

func mockEnvVar(envVar, value string) (restore func()) {
oldVal, ok := os.LookupEnv(envVar)
if value == "" {
os.Unsetenv(envVar)
} else {
os.Setenv(envVar, value)
}

return func() {
if ok {
os.Setenv(envVar, oldVal)
} else {
os.Unsetenv(envVar)
}
}
}

func (ts *TimeoutTestSuite) TestHostScaledTimeout(c *C) {
restore := testutil.MockRuntimeARCH("some-fast-arch")
restore := mockEnvVar("GO_TEST_RACE", "")
defer restore()
default_timeout := testutil.HostScaledTimeout(2 * time.Second)

restore = testutil.MockRuntimeARCH("riscv64")
restore = testutil.MockRuntimeARCH("default")
defer restore()
riscv64_timeout := testutil.HostScaledTimeout(2 * time.Second)

c.Check(default_timeout, Equals, 2*time.Second)
c.Check(riscv64_timeout > default_timeout, Equals, true)
origDuration := 2 * time.Second

type testcase struct {
name string
setup func() (restore func())
expected time.Duration
}

testcases := []testcase{
{
name: "default ",
setup: func() func() { return testutil.MockRuntimeARCH("some-fast-arch") },
expected: origDuration,
},
{
name: "riscv64 arch",
setup: func() func() { return testutil.MockRuntimeARCH("riscv64") },
expected: 6 * origDuration,
},
{

name: "go test -race",
setup: func() func() { return mockEnvVar("GO_TEST_RACE", "1") },
expected: 5 * origDuration,
},
{

name: "go test -race and riscv64 arch",
setup: func() func() {
archRestore := testutil.MockRuntimeARCH("riscv64")
envVarRestore := mockEnvVar("GO_TEST_RACE", "1")
return func() {
archRestore()
envVarRestore()
}
},
// the arch scaling takes precedence
expected: 6 * origDuration,
},
}

for _, tc := range testcases {
restore := tc.setup()
out := testutil.HostScaledTimeout(origDuration)
c.Check(out, Equals, tc.expected, Commentf("test %q failed", tc.name))
restore()
}
}

0 comments on commit 4be3095

Please sign in to comment.