forked from tendermint/tendermint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fail.go
78 lines (63 loc) · 1.34 KB
/
fail.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
package fail
import (
"fmt"
"math/rand"
"os"
"strconv"
)
var callIndexToFail int
func init() {
callIndexToFailS := os.Getenv("FAIL_TEST_INDEX")
if callIndexToFailS == "" {
callIndexToFail = -1
} else {
var err error
callIndexToFail, err = strconv.Atoi(callIndexToFailS)
if err != nil {
callIndexToFail = -1
}
}
}
// Fail when FAIL_TEST_INDEX == callIndex
var (
callIndex int //indexes Fail calls
callRandIndex int // indexes a run of FailRand calls
callRandIndexToFail = -1 // the callRandIndex to fail on in FailRand
)
func Fail() {
if callIndexToFail < 0 {
return
}
if callIndex == callIndexToFail {
Exit()
}
callIndex += 1
}
// FailRand should be called n successive times.
// It will fail on a random one of those calls
// n must be greater than 0
func FailRand(n int) {
if callIndexToFail < 0 {
return
}
if callRandIndexToFail < 0 {
// first call in the loop, pick a random index to fail at
callRandIndexToFail = rand.Intn(n)
callRandIndex = 0
}
if callIndex == callIndexToFail {
if callRandIndex == callRandIndexToFail {
Exit()
}
}
callRandIndex += 1
if callRandIndex == n {
callIndex += 1
}
}
func Exit() {
fmt.Printf("*** fail-test %d ***\n", callIndex)
proc, _ := os.FindProcess(os.Getpid())
proc.Signal(os.Interrupt)
// panic(fmt.Sprintf("*** fail-test %d ***", callIndex))
}