Skip to content

Commit d988acb

Browse files
committed
cmd/roachtest: prevent macOS host from idle-sleeping during test
These tests run for 20mins+ so it is common to step away to make coffee or something while they are running. Unfortunately this can mean the host goes to sleep, failing the test. To avoid this we can invoke the macOS utility 'caffeinate' with '-i' to prevent only machine idle sleep (but not display sleep/lock), and with the roachtest process, so it will inhibit sleep until the roachtest process exits. Release note: none. Epic: none.
1 parent 0d5068e commit d988acb

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

pkg/cmd/roachtest/roachtestflags/flags.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,14 @@ var (
497497
Always collect artifacts during test teardown, even if the test did not
498498
time out or fail.`,
499499
})
500+
501+
Caffeinate bool = true
502+
_ = registerRunFlag(&Caffeinate, FlagInfo{
503+
Name: "caffeinate",
504+
Usage: `
505+
On Darwin, prevent the system from sleeping while roachtest is running
506+
by invoking caffeinate -i -w <pid>. Default is true.`,
507+
})
500508
)
501509

502510
// The flags below override the final cluster configuration. They have no

pkg/cmd/roachtest/run.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"math/rand"
1212
"net/http"
1313
"os"
14+
"os/exec"
1415
"os/signal"
1516
"os/user"
1617
"path/filepath"
@@ -52,6 +53,21 @@ const (
5253
// runTests is the main function for the run and bench commands.
5354
// Assumes initRunFlagsBinariesAndLibraries was called.
5455
func runTests(register func(registry.Registry), filter *registry.TestFilter) error {
56+
// On Darwin, start caffeinate to prevent the system from sleeping.
57+
if runtime.GOOS == "darwin" && roachtestflags.Caffeinate {
58+
pid := os.Getpid()
59+
cmd := exec.Command("caffeinate", "-i", "-w", strconv.Itoa(pid))
60+
if err := cmd.Start(); err != nil {
61+
fmt.Fprintf(os.Stderr, "warning: failed to start caffeinate: %v\n", err)
62+
} else {
63+
defer func() {
64+
if cmd.Process != nil {
65+
_ = cmd.Process.Kill()
66+
}
67+
}()
68+
}
69+
}
70+
5571
globalSeed := randutil.NewPseudoSeed()
5672
if globalSeedEnv := os.Getenv("ROACHTEST_GLOBAL_SEED"); globalSeedEnv != "" {
5773
if parsed, err := strconv.ParseInt(globalSeedEnv, 0, 64); err == nil {

0 commit comments

Comments
 (0)