From cdc252b8182deb749150c33a2cc985e12e3c8437 Mon Sep 17 00:00:00 2001 From: Zach Date: Tue, 30 Oct 2018 10:34:51 -0400 Subject: [PATCH] add fail-test file instead of dep, closes #2638 (#2728) original author of this file is @ebuchman: https://github.com/ebuchman/fail-test --- Gopkg.lock | 8 ----- Gopkg.toml | 4 --- consensus/state.go | 2 +- libs/fail/fail.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++ state/execution.go | 2 +- 5 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 libs/fail/fail.go diff --git a/Gopkg.lock b/Gopkg.lock index 513e0bd7a..f4656e6ba 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -35,13 +35,6 @@ revision = "8991bc29aa16c548c550c7ff78260e27b9ab7c73" version = "v1.1.1" -[[projects]] - digest = "1:c7644c73a3d23741fdba8a99b1464e021a224b7e205be497271a8003a15ca41b" - name = "github.com/ebuchman/fail-test" - packages = ["."] - pruneopts = "UT" - revision = "95f809107225be108efcf10a3509e4ea6ceef3c4" - [[projects]] digest = "1:544229a3ca0fb2dd5ebc2896d3d2ff7ce096d9751635301e44e37e761349ee70" name = "github.com/fortytw2/leaktest" @@ -503,7 +496,6 @@ input-imports = [ "github.com/btcsuite/btcutil/base58", "github.com/btcsuite/btcutil/bech32", - "github.com/ebuchman/fail-test", "github.com/fortytw2/leaktest", "github.com/go-kit/kit/log", "github.com/go-kit/kit/log/level", diff --git a/Gopkg.toml b/Gopkg.toml index 955d6c6d1..47418bef3 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -81,10 +81,6 @@ name = "github.com/jmhodges/levigo" revision = "c42d9e0ca023e2198120196f842701bb4c55d7b9" -[[constraint]] - name = "github.com/ebuchman/fail-test" - revision = "95f809107225be108efcf10a3509e4ea6ceef3c4" - # last revision used by go-crypto [[constraint]] name = "github.com/btcsuite/btcutil" diff --git a/consensus/state.go b/consensus/state.go index 0b079f13d..40aeeb7a4 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -9,8 +9,8 @@ import ( "sync" "time" - fail "github.com/ebuchman/fail-test" cmn "github.com/tendermint/tendermint/libs/common" + "github.com/tendermint/tendermint/libs/fail" "github.com/tendermint/tendermint/libs/log" tmtime "github.com/tendermint/tendermint/types/time" diff --git a/libs/fail/fail.go b/libs/fail/fail.go new file mode 100644 index 000000000..edfca13e3 --- /dev/null +++ b/libs/fail/fail.go @@ -0,0 +1,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)) +} diff --git a/state/execution.go b/state/execution.go index 68298a8d2..72f6cc978 100644 --- a/state/execution.go +++ b/state/execution.go @@ -4,9 +4,9 @@ import ( "fmt" "time" - "github.com/ebuchman/fail-test" abci "github.com/tendermint/tendermint/abci/types" dbm "github.com/tendermint/tendermint/libs/db" + "github.com/tendermint/tendermint/libs/fail" "github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/mempool" "github.com/tendermint/tendermint/proxy"