pingcap / failpoint

An implementation of failpoints for Golang.
Apache License 2.0
817 stars 63 forks source link

failpoint not activated #41

Closed yechenyuyu closed 1 year ago

yechenyuyu commented 5 years ago

I write this in foo.go:

func pingCapFail() (string, failpoint.Value) {
    failpoint.Inject("failpoint-name", func(val failpoint.Value) {
        failpoint.Return("unit-test", val)
    })
    return "success", nil
}

and i run failpoint-ctl enable , code becomes this;

func pingCapFail() (string, failpoint.Value) {
    if val, ok := failpoint.Eval(_curpkg_("failpoint-name")); ok {
        return "unit-test", val
    }
    return "success", nil
}

then in foo_test.go ,i write my test code like this:

func Test_pingCapFail(t *testing.T) {
    failpoint.Enable("failpoint-name", "return(5)")
    got, got1 := pingCapFail()
    if got != "unit-test" {
        t.Errorf("pingCapFail() got = %v, want %v", got, "unit-test")
    }
    if !reflect.DeepEqual(got1, 5) {
        t.Errorf("pingCapFail() got1 = %v, want %v", got1, 5)
    }
}

and it fails, and obviously the failpoint is not activated, can someone tell me why?

lentil1016 commented 5 years ago

Add the package name of foo.go before the failpoint label name when you call failpoint.Enable will probably fix your problem.

$ cat test.go              
package main

import "github.com/pingcap/failpoint"

func main() {
    failpoint.Enable("main/testPanic", "return(true)")
    if _, ok := failpoint.Eval(_curpkg_("testPanic")); ok {
        panic("failpoint triggerd")
    }
}
$ failpoint-ctl enable
$ go run ./
panic: failpoint triggerd

goroutine 1 [running]:
main.main()
    /root/test-go/test.go:8 +0xe5
exit status 2
tisonkun commented 1 year ago

Closed as stale and answered.

Please provide a complete repro if the problem is still relevant.