How to use failpoint in go test
Open
Nobody has claimed this yet.
question
- Dominant language
- Go
- Stars
- 895
- Forks
- 67
- PR merge metrics
- No merged PRs in 30d
Description
[root@xxx]# cat main_test.go
package main
import (
"testing"
"github.com/pingcap/failpoint"
)
func TestGetValue(t *testing.T) {
failpoint.Enable("main/failpoint-name","return(4)")
t.Log("result:", GetValue())
}
[root@xxx]# cat main.go
package main
import (
"fmt"
"github.com/pingcap/failpoint"
)
func GetValue() int {
if val, _err_ := failpoint.Eval(_curpkg_("failpoint-name")); _err_ == nil {
return val.(int)
}
return 1
}
func main() {
fmt.Println("Hello world ", GetValue())
}
[root@xxx]# GOCACHE=/tmp/failpoint-cache GO_FAILPOINTS="main/failpoint-name=return(4)" CGO_ENABLED=1 GO111MODULE=on go test-v -toolexec /usr/bin/failpoint-toolexec main_test.go main.go binding__failpoint_binding__.go
=== RUN TestGetValue
main_test.go:10: result: 1
--- PASS: TestGetValue (0.00s)
PASS
ok command-line-arguments 0.006s
expect:
[root@xxx]# GOCACHE=/tmp/failpoint-cache GO_FAILPOINTS="main/failpoint-name=return(4)" CGO_ENABLED=1 GO111MODULE=on go test-v -toolexec /usr/bin/failpoint-toolexec main_test.go main.go binding__failpoint_binding__.go
=== RUN TestGetValue
main_test.go:10: result: 4
--- PASS: TestGetValue (0.00s)
PASS
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the report with main_test.go, main.go, binding__failpoint_binding__.go, and the shown go test command. Compare the observed result with the expected result of 4, then inspect the failpoint-toolexec and GO_FAILPOINTS integration to identify where the configuration is lost. Done means the supplied test command produces result: 4 or the usage limitation is documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100