pingcap / failpoint

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

go test failed #30

Closed nkbai closed 3 years ago

nkbai commented 5 years ago

Bug Report

go get github.com/pingcap/failpoint/ cd failpoint go test .

/V/d/g/s/g/p/failpoint (master|✔) [2] $ go test .
failed to parse "invalid" past "invalid"
failed to enable "failpoint-name=invalid" (failpoint: could not parse terms)

----------------------------------------------------------------------
FAIL: http_test.go:47: httpSuite.TestServeHTTP

http_test.go:124:
    c.Assert(err, IsNil)
... value *url.Error = &url.Error{Op:"Get", URL:"http://127.0.0.1:23389/failpoint-env1", Err:(*net.OpError)(0xc000172050)} ("Get http://127.0.0.1:23389/failpoint-env1: dial tcp 127.0.0.1:23389: connect: connection refused")

failed to parse "invalid" past "invalid"
failed to enable "runtime-test-2=invalid" (failpoint: could not parse terms)

----------------------------------------------------------------------
FAIL: runtime_test.go:21: runtimeSuite.TestRuntime

runtime_test.go:133:
    c.Assert(ok, IsTrue)
... obtained bool = false

OOPS: 1 passed, 2 FAILED
--- FAIL: TestFailpoint (1.00s)
failed to parse "invalid" past "invalid"
failed to enable "failpoint-name=invalid" (failpoint: could not parse terms)

----------------------------------------------------------------------
FAIL: http_test.go:47: httpSuite.TestServeHTTP

http_test.go:124:
    c.Assert(err, IsNil)
... value *url.Error = &url.Error{Op:"Get", URL:"http://127.0.0.1:23389/failpoint-env1", Err:(*net.OpError)(0xc000172d70)} ("Get http://127.0.0.1:23389/failpoint-env1: dial tcp 127.0.0.1:23389: connect: connection refused")

failed to parse "invalid" past "invalid"
failed to enable "runtime-test-2=invalid" (failpoint: could not parse terms)

----------------------------------------------------------------------
FAIL: runtime_test.go:21: runtimeSuite.TestRuntime

runtime_test.go:133:
    c.Assert(ok, IsTrue)
... obtained bool = false

OOPS: 1 passed, 2 FAILED
--- FAIL: TestHttp (1.00s)
failed to parse "invalid" past "invalid"
failed to enable "failpoint-name=invalid" (failpoint: could not parse terms)

----------------------------------------------------------------------
FAIL: http_test.go:47: httpSuite.TestServeHTTP

http_test.go:124:
    c.Assert(err, IsNil)
... value *url.Error = &url.Error{Op:"Get", URL:"http://127.0.0.1:23389/failpoint-env1", Err:(*net.OpError)(0xc000146500)} ("Get http://127.0.0.1:23389/failpoint-env1: dial tcp 127.0.0.1:23389: connect: connection refused")

failed to parse "invalid" past "invalid"
failed to enable "runtime-test-2=invalid" (failpoint: could not parse terms)

----------------------------------------------------------------------
FAIL: runtime_test.go:21: runtimeSuite.TestRuntime

runtime_test.go:133:
    c.Assert(ok, IsTrue)
... obtained bool = false

OOPS: 1 passed, 2 FAILED
--- FAIL: TestNewRestorer (1.01s)
FAIL
FAIL    github.com/pingcap/failpoint    3.030s
/V/d/g/s/g/p/failpoint (master|✔) [1] $
amyangfei commented 5 years ago

Some environment variables are required in unit/integration test, we recommend to use make test or make gotest. You can find more information in the Makefile.

nkbai commented 5 years ago
package testfail

import (
    "errors"
    "github.com/pingcap/failpoint"
    "testing"
)

type ttt struct {

}
func (t ttt) Call() error{
    failpoint.Inject("failpoint-test", func() {
        failpoint.Return(nil)
    })
    return errors.New("must fail")
}

func TestCall(t*testing.T){
    tt:=ttt{}
    failpoint.Enable("failpoint-test","return(nil)")
    err:=tt.Call()
    if err!=nil{
        t.Error(err)
    }
}
nkbai commented 5 years ago

how to make this test success? I cannot find any example ,tell me how to use this library.

lonng commented 5 years ago

@nkbai failpoint.Enable("failpoint-test","return(nil)") means the failpoint failpoint-test has been activited and the Call will return nil. I cannot understand your intentions.

nkbai commented 5 years ago

but this test is failed, and tt.Call return error ("must fail")

lonng commented 5 years ago

You must use failpoint-ctl enable to rewrite the source file.

lonng commented 5 years ago

@nkbai You can reference the TiDB project.

tisonkun commented 3 years ago

@nkbai for the first question, you can take a look at the target make test and it actually expands to

GO111MODULE=on GO_FAILPOINTS="failpoint-env1=return(10);failpoint-env2=return(true)" GO_FAILPOINTS_HTTP=":23389" CGO_ENABLED=1 GO111MODULE=on go test -p 4 -v ./...

... for using failpoint in your own project, yes you can take a look at how pingcap/tidb and tikv/client-go use it. The former uses a code generation way while the latter doesn't require code generation.

We should provide more document on its usage, though.

I'll close this issue as the document part is tracked by https://github.com/pingcap/failpoint/issues/29