Testing is an important part of the line before the function.
During the test, to try to cover various scenarios. Fault conditions or scenario testing under unusual circumstances, it is also essential.
How to simulate malfunction?
In FreeBSD, failpoints often used to simulate the failure.
In golang, but also to achieve failpoint, the company is currently pingcap open source project failpoint
.
Here achieve golang in failpoint introduction.
1. Install failpoint Tools
cd $GOPATH/src
mkdir -p github.com/pingcap
cd github.com/pingcap
git clone https://github.com/pingcap/failpoint.git
cd failpoint
make
GO111MODULE=on CGO_ENABLED=0 GO111MODULE=on go build -ldflags '-X "github.com/pingcap/failpoint/failpoint-ctl/version.releaseVersion=12f4ac2-dev" -X "github.com/pingcap/failpoint/failpoint-ctl/version.buildTS=2019-11-15 09:41:49" -X "github.com/pingcap/failpoint/failpoint-ctl/version.gitHash=12f4ac2fd11dfc6b2f7018b00bb90f61a5b6b692" -X "github.com/pingcap/failpoint/failpoint-ctl/version.gitBranch=master" -X "github.com/pingcap/failpoint/failpoint-ctl/version.goVersion=go version go1.13 darwin/amd64"' -o bin/failpoint-ctl failpoint-ctl/main.go
failpoint-ctl build successfully :-) !
Note: double-check these steps. If wrong, the situation can not be converted code will appear.
After compilation, generate an executable file failpoint-ctl:
ll bin
total 6840
-rwxr-xr-x 1 lanyang staff 3.3M 11 15 17:41 failpoint-ctl
2. Test the code
Code structure is as follows:
cd $GOPATH/src
mkdir fp_example
cd fp_example
# tree ./
./
├── fruit
│ └── banana.go
├── main.go
└── util
├── apple.go
└── orange.go
Which, main.go contents of the file:
package main
import (
"fmt"
"fp_example/hot_fruit"
"fp_example/fruit"
)
func main() {
hot_fruit.Apple()
hot_fruit.Orange()
fruit.Banana()
fmt.Println("main end")
}
fruit/banana.go:
package fruit
import (
"fmt"
"github.com/pingcap/failpoint"
)
func Banana() {
fmt.Println("banana....")
failpoint.Inject("bananaPanic", func() {
panic("banana failpoint triggerd")
})
}
Injecting a panic.
hot_fruit/apple.go:
package hot_fruit
import (
"fmt"
"github.com/pingcap/failpoint"
)
func Apple() {
fmt.Println("apple....")
failpoint.Inject("applePanic", func() {
panic("apple failpoint triggerd")
})
}
Injecting a panic again.
hot_fruit/orange.go:
package hot_fruit
import (
"fmt"
"github.com/pingcap/failpoint"
)
func Orange() {
fmt.Println("orange....")
failpoint.Inject("orangePanic", func() {
panic("orange failpoint triggerd")
})
}
Injecting a panic again.
3. Transferable failpoint
$GOPATH/src/github.com/pingcap/failpoint/bin/failpoint-ctl enable
After transcoding, look at the code file structure:
tree ./
./
├── fruit
│ ├── banana.go
│ ├── banana.go__failpoint_stash__
│ └── binding__failpoint_binding__.go
├── hot_fruit
│ ├── apple.go
│ ├── apple.go__failpoint_stash__
│ ├── binding__failpoint_binding__.go
│ ├── orange.go
│ └── orange.go__failpoint_stash__
├── main.go
2 directories, 10 files
We can see more than a few files.
If you do disable, those files will disappear.
$GOPATH/src/github.com/pingcap/failpoint/bin/failpoint-ctl disable
4. compile and execute
Using the code after the change means, to compile:
go build -o simple main.go
Generate an executable file simple
.
Normal execution:
./simple
apple....
orange....
banana....
main end
BananaPanic fault trigger execution:
GO_FAILPOINTS="fp_example/fruit/bananaPanic=return(true)" ./simple
apple....
orange....
banana....
panic: banana failpoint triggerd
goroutine 1 [running]:
fp_example/fruit.Banana()
/Users/lanyang/workspace/go_projects/src/fp_example/fruit/banana.go:12 +0x10e
main.main()
/Users/lanyang/workspace/go_projects/src/fp_example/main.go:13 +0x2c
Among them, fp_example/fruit
is the package name, bananaPanic
is injected fault name.
ApplePanic fault trigger execution:
GO_FAILPOINTS="fp_example/hot_fruit/applePanic=return(true)" ./simple
apple....
panic: apple failpoint triggerd
goroutine 1 [running]:
fp_example/hot_fruit.Apple()
/Users/lanyang/workspace/go_projects/src/fp_example/hot_fruit/apple.go:12 +0x10e
main.main()
/Users/lanyang/workspace/go_projects/src/fp_example/main.go:11 +0x22
OrangePanic fault trigger execution:
GO_FAILPOINTS="fp_example/hot_fruit/orangePanic=return(true)" ./simple
apple....
orange....
panic: orange failpoint triggerd
goroutine 1 [running]:
fp_example/hot_fruit.Orange()
/Users/lanyang/workspace/go_projects/src/fp_example/hot_fruit/orange.go:12 +0x10e
main.main()
/Users/lanyang/workspace/go_projects/src/fp_example/main.go:12 +0x27