From 1041eff8d99eef4ae668edf347468864f4d6b45f Mon Sep 17 00:00:00 2001 From: D3Hunter Date: Mon, 27 May 2024 12:09:59 +0800 Subject: [PATCH] move eval inside to avoid line number change --- Makefile | 4 ++++ README.md | 2 +- code/expr_rewriter.go | 39 +++------------------------------------ failpoints.go | 3 +++ 4 files changed, 11 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index 9703ac2..22a2c1c 100644 --- a/Makefile +++ b/Makefile @@ -72,3 +72,7 @@ test-examples: $(GO) run failpoint-ctl/main.go enable ./examples $(GOTEST) -covermode=atomic -coverprofile=coverage.txt -coverpkg=./... -v ./examples/... $(GO) run failpoint-ctl/main.go disable ./examples + +test-examples-toolexec: build + @ echo "----------- go test examples using toolexec ---------------" + GOCACHE=/tmp/failpoint-cache $(GOTEST) -covermode=atomic -coverprofile=coverage.txt -coverpkg=./... -toolexec="$(PWD)/bin/failpoint-toolexec" -v ./examples/... diff --git a/README.md b/README.md index 5fea838..95046e4 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ An implementation of [failpoints][failpoint] for Golang. Fail points are used to - `func Inject(fpname string, fpblock func(val Value)) {}` - `func InjectContext(fpname string, ctx context.Context, fpblock func(val Value)) {}` - - `func InjectCall(fpname string, fn any) {}` + - `func InjectCall(fpname string, args ...any) {}` - `func Break(label ...string) {}` - `func Goto(label string) {}` - `func Continue(label ...string) {}` diff --git a/code/expr_rewriter.go b/code/expr_rewriter.go index 0fd6a8b..fd540e3 100644 --- a/code/expr_rewriter.go +++ b/code/expr_rewriter.go @@ -237,12 +237,10 @@ func (r *Rewriter) rewriteInjectCall(call *ast.CallExpr) (bool, ast.Stmt, error) Args: []ast.Expr{fpname}, } - // failpoint.InjectFn("name", a, b, c) + // failpoint.InjectCall("name", a, b, c) // | // v - // if _, _err_ := failpoint.Eval(_curpkg_("name")); _err_ == nil { - // failpoint.Call(_curpkg_("name"), a, b, c) - // } + // failpoint.Call(_curpkg_("name"), a, b, c) fnArgs := make([]ast.Expr, 0, len(call.Args)) fnArgs = append(fnArgs, fpnameExtendCall) fnArgs = append(fnArgs, call.Args[1:]...) @@ -255,38 +253,7 @@ func (r *Rewriter) rewriteInjectCall(call *ast.CallExpr) (bool, ast.Stmt, error) Args: fnArgs, }, } - ifBody := &ast.BlockStmt{ - Lbrace: call.Pos(), - List: []ast.Stmt{fnCall}, - Rbrace: call.End(), - } - - checkCall := &ast.CallExpr{ - Fun: &ast.SelectorExpr{ - X: &ast.Ident{NamePos: call.Pos(), Name: r.failpointName}, - Sel: ast.NewIdent(evalFunction), - }, - Args: []ast.Expr{fpnameExtendCall}, - } - err := ast.NewIdent("_err_") - init := &ast.AssignStmt{ - Lhs: []ast.Expr{ast.NewIdent("_"), err}, - Rhs: []ast.Expr{checkCall}, - Tok: token.DEFINE, - } - - cond := &ast.BinaryExpr{ - X: err, - Op: token.EQL, - Y: ast.NewIdent("nil"), - } - stmt := &ast.IfStmt{ - If: call.Pos(), - Init: init, - Cond: cond, - Body: ifBody, - } - return true, stmt, nil + return true, fnCall, nil } func (r *Rewriter) rewriteBreak(call *ast.CallExpr) (bool, ast.Stmt, error) { diff --git a/failpoints.go b/failpoints.go index bb4724a..3ebc4f4 100644 --- a/failpoints.go +++ b/failpoints.go @@ -317,5 +317,8 @@ func Eval(failpath string) (Value, error) { // Call calls the function passed by EnableCall with args supplied in InjectCall. func Call(failpath string, args ...any) { + if _, err := failpoints.Eval(failpath); err != nil { + return + } failpoints.Call(failpath, args...) }