/
makeflaky.go
221 lines (194 loc) · 5.3 KB
/
makeflaky.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strconv"
"syscall"
"time"
"golang.org/x/sys/unix"
)
func isNoSuchProcess(err error) bool {
var errno syscall.Errno
if errors.As(err, &errno) {
return errno == unix.ESRCH
}
return false
}
var errSigint = errors.New("sigint handled")
// runLoop pauses pid using runPeriod and stopPeriod. It returns with there is an error, or if
// ctx is cancelled.
func runLoop(ctx context.Context, pid int, runPeriod time.Duration, stopPeriod time.Duration) error {
runLoopExiting := make(chan struct{})
defer close(runLoopExiting)
// create a sleep function that waits for sigint or the timeout
sigintChan := make(chan os.Signal, 1)
signal.Notify(sigintChan, os.Interrupt)
defer signal.Stop(sigintChan)
sleepOrSigintOrCancel := func(duration time.Duration) error {
select {
case <-time.After(duration):
return nil
case <-sigintChan:
return errSigint
case <-ctx.Done():
return ctx.Err()
}
}
dutyCycle := (runPeriod.Seconds() / (runPeriod + stopPeriod).Seconds()) * 100.0
log.Printf("slowing down pid=%d; runPeriod=%s; stopPeriod=%s (%.1f%% duty cycle)...",
pid, runPeriod.String(), stopPeriod.String(), dutyCycle)
iterationCount := 0
for {
err := unix.Kill(pid, unix.SIGSTOP)
if err != nil {
if isNoSuchProcess(err) {
break
}
panic(err)
}
// make sure we always call SIGCONT, even if CTRL-C is pressed
err = sleepOrSigintOrCancel(stopPeriod)
err2 := unix.Kill(pid, unix.SIGCONT)
if err != nil {
if err == context.Canceled {
break
}
}
if err2 != nil {
return err2
}
err = unix.Kill(pid, unix.SIGCONT)
if err != nil {
if isNoSuchProcess(err) {
break
}
panic(err)
}
err = sleepOrSigintOrCancel(runPeriod)
if err != nil {
if err == context.Canceled {
break
}
return err
}
iterationCount++
}
log.Printf("sent %d signals", iterationCount)
return nil
}
// exitCode returns the exit code from err, 1 if it is not *exec.ExitError, or 0 if nil.
func exitCode(err error) int {
if err == nil {
return 0
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return exitErr.ExitCode()
}
// not exec.ExitError: assume code 1
return 1
}
// exitWithSameErr calls os.Exit with the same code as err if err is nil or exec.ExitCode. If it
// is another error type, it calls panic(err).
func exitWithSameErr(err error) {
if err != nil && !errors.Is(err, &exec.ExitError{}) {
panic(err)
}
os.Exit(exitCode(err))
}
func maybeExecAndRun(execFlag bool, runPeriod time.Duration, stopPeriod time.Duration, args []string) error {
var pidToPause int
var child *exec.Cmd
waitErrChan := make(chan error, 1)
ctx := context.Background()
if execFlag {
child = exec.Command(args[0], args[1:]...)
child.Stdin = os.Stdin
child.Stdout = os.Stdout
child.Stderr = os.Stderr
err := child.Start()
if err != nil {
return err
}
pidToPause = child.Process.Pid
log.Printf("helper started process pid=%d cmd line: %s %#v ...\n",
pidToPause, child.Path, child.Args)
// replace ctx with a context that is cancelled when the child process exits
var cancel func()
ctx, cancel = context.WithCancel(ctx)
go func() {
// ignore the error: it will be checked by the parent
waitErr := child.Wait()
cancel()
waitErrChan <- waitErr
}()
} else {
var err error
pidToPause, err = strconv.Atoi(args[0])
if err != nil {
return err
}
}
err := runLoop(ctx, pidToPause, runPeriod, stopPeriod)
if err != nil {
if err == errSigint {
log.Printf("caught SIGINT (CTRL-C)")
} else {
panic(fmt.Sprintf("BUG: unexpected err=%#v", err))
}
}
if child != nil {
// get the result from wait and exit with the same code
return <-waitErrChan
}
return nil
}
const usageMessage = `Usage: makeflaky [args] (pid) || makeflaky [args] -exec (program args) || makeflaky [args] -goTest (go test args)
Pauses a process periodically to try and cause tests that depend on real time to fail.
`
func main() {
runPeriod := flag.Duration("runPeriod", 10*time.Millisecond, "time to let process run")
stopPeriod := flag.Duration("stopPeriod", time.Millisecond, "time to stop process with SIGSTOP")
goTest := flag.Bool("goTest", false, "pass all other args to go test")
execFlag := flag.Bool("exec", false, "run the program with all other args")
flag.Parse()
if *goTest || *execFlag {
if *goTest && *execFlag {
fmt.Fprintln(os.Stderr, "ERROR: Cannot specify both -goTest and -exec")
os.Exit(1)
}
if flag.NArg() == 0 {
fmt.Fprint(os.Stderr, usageMessage)
os.Exit(1)
}
} else if flag.NArg() != 1 {
fmt.Fprint(os.Stderr, usageMessage)
os.Exit(1)
}
if *goTest {
// Run "go test -exec (self) (args)", so we can start and intercept the child process
exePath, err := os.Executable()
if err != nil {
panic(err)
}
helperArgs := fmt.Sprintf("%s -runPeriod=%s -stopPeriod=%s -exec",
exePath, runPeriod.String(), stopPeriod.String())
args := []string{"test", "-exec", helperArgs}
args = append(args, flag.Args()...)
// fmt.Printf("goTest running: go %s ...\n", strings.Join(args, " "))
cmd := exec.Command("go", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
exitWithSameErr(err)
}
err := maybeExecAndRun(*execFlag, *runPeriod, *stopPeriod, flag.Args())
exitWithSameErr(err)
}