Skip to content

Commit

Permalink
move eval inside to avoid line number change
Browse files Browse the repository at this point in the history
  • Loading branch information
D3Hunter committed May 27, 2024
1 parent bb473d9 commit 1041eff
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 37 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/...
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}`
Expand Down
39 changes: 3 additions & 36 deletions code/expr_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]...)
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions failpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}

0 comments on commit 1041eff

Please sign in to comment.