|
@ -2,36 +2,30 @@ package fail |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
"math/rand" |
|
|
|
|
|
"os" |
|
|
"os" |
|
|
"strconv" |
|
|
"strconv" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
var callIndexToFail int |
|
|
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
|
|
|
|
|
func envSet() int { |
|
|
callIndexToFailS := os.Getenv("FAIL_TEST_INDEX") |
|
|
callIndexToFailS := os.Getenv("FAIL_TEST_INDEX") |
|
|
|
|
|
|
|
|
if callIndexToFailS == "" { |
|
|
if callIndexToFailS == "" { |
|
|
callIndexToFail = -1 |
|
|
|
|
|
|
|
|
return -1 |
|
|
} else { |
|
|
} else { |
|
|
var err error |
|
|
var err error |
|
|
callIndexToFail, err = strconv.Atoi(callIndexToFailS) |
|
|
|
|
|
|
|
|
callIndexToFail, err := strconv.Atoi(callIndexToFailS) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
callIndexToFail = -1 |
|
|
|
|
|
|
|
|
return -1 |
|
|
} |
|
|
} |
|
|
|
|
|
return callIndexToFail |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Fail when FAIL_TEST_INDEX == callIndex
|
|
|
// 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
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
var callIndex int //indexes Fail calls
|
|
|
|
|
|
|
|
|
func Fail() { |
|
|
func Fail() { |
|
|
|
|
|
callIndexToFail := envSet() |
|
|
if callIndexToFail < 0 { |
|
|
if callIndexToFail < 0 { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
@ -43,33 +37,6 @@ func Fail() { |
|
|
callIndex += 1 |
|
|
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() { |
|
|
func Exit() { |
|
|
fmt.Printf("*** fail-test %d ***\n", callIndex) |
|
|
fmt.Printf("*** fail-test %d ***\n", callIndex) |
|
|
os.Exit(1) |
|
|
os.Exit(1) |
|
|