xhd2015 / xgo

All-in-one go testing library
MIT License
332 stars 19 forks source link

Failed to setup mock #225

Closed shubham-dogra-s1 closed 3 months ago

shubham-dogra-s1 commented 3 months ago

xgo version: v1.0.42


mock.Patch((*net.Dialer).Dial, func(_ *net.Dialer, network, address string) (net.Conn, error) {
                    return nil, errors.New("dial error")
                })

    --- FAIL: TestDialRequest_Do/1:_error_while_dialing_request (0.00s)
panic: failed to setup mock for: net.(*Dialer).Dial [recovered]
        panic: failed to setup mock for: net.(*Dialer).Dial
xhd2015 commented 3 months ago

Are you using xgo test or xgo run?

xhd2015 commented 3 months ago

You can try to add --trap-stdlib to force stdlib mock

shubham-dogra-s1 commented 3 months ago

using xgo test, tried with --trap-stdlib same error

xhd2015 commented 3 months ago

Hi @shubham-dogra-s1 thanks for your feedback.

It is confirmed that this is a bug due to this line: https://github.com/xhd2015/xgo/blob/e64e4df234291f76735d1d3a086d40862e6e2264/patch/ctxt/stdlib.go#L100-L103

The funcName is (*net.Dialer).Dial, which is not in upper case, so types.IsExported("(*net.Dialer).Dial") returns false.

However, it is obvious that (*net.Dialer).Dial is an exported method.

I have issued a PR to fix this: https://github.com/xhd2015/xgo/pull/226

xhd2015 commented 3 months ago

@shubham-dogra-s1 fixed in https://github.com/xhd2015/xgo/releases/tag/v1.0.43

upgrade:

# update xgo
go install github.com/xhd2015/xgo/cmd/xgo@v1.0.43

# update dependency
go get github.com/xhd2015/xgo/runtime@v1.0.43
shubham-dogra-s1 commented 3 months ago

@xhd2015 Thanks for the quick fix! Its working fine with xgo test, but when i run in debug mode with xgo e, failing with the same error

xhd2015 commented 3 months ago

@xhd2015 Thanks for the quick fix! Its working fine with xgo test, but when i run in debug mode with xgo e, failing with the same error

@shubham-dogra-s1 Do you have a screenshot? And some logs can also help.

shubham-dogra-s1 commented 3 months ago

Here are the logs @xhd2015 . let me know if you anything else

--- FAIL: TestDialRequest_Do (13.78s)
    --- FAIL: TestDialRequest_Do/1:_error_while_dialing_request (13.78s)
panic: failed to setup mock for: net.(*Dialer).Dial [recovered]
        panic: failed to setup mock for: net.(*Dialer).Dial

goroutine 6 [running]:
testing.tRunner.func1.2({0x10a40f8c0, 0x1400024b5d0})
        /Users/shubham/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/testing/testing.go:1642 +0x3c8
testing.tRunner.func1()
        /Users/shubham/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/testing/testing.go:1645 +0x56c
panic({0x10a40f8c0?, 0x1400024b5d0?})
        /Users/shubham/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/runtime/panic.go:770 +0xf0
github.com/xhd2015/xgo/runtime/mock.getFunc({0x10a4aeb60, 0x10a8bf7c8})
        /Users/shubham/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.43/mock/mock.go:54 +0x1c0
github.com/xhd2015/xgo/runtime/mock.Patch({0x10a4aeb60, 0x10a8bf7c8}, {0x10a4aeb60, 0x10a8bf398})
        /Users/shubham/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.43/mock/patch.go:53 +0x2b8
project/internal/clients.TestDialRequest_Do.func1()
        /Users/shubham/GolandProjects/project/internal/clients/network_test.go:87 +0xfc
project/internal/clients.TestDialRequest_Do.func2(0x14001dff860)
        /Users/shubham/GolandProjects/project/internal/clients/network_test.go:96 +0x1a8
testing.tRunner(0x14001dff860, 0x1400024a3e0)
        /Users/shubham/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/testing/testing.go:1709 +0x400
created by testing.(*T).Run in goroutine 5
        /Users/shubham/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/testing/testing.go:1762 +0x66c
test end
shubham-dogra-s1 commented 3 months ago

How to reproduce?

  1. Start xgo explorer
  2. Start debugger
  3. Add debug point at tt.mock

Env: 1: xgo version: 1.0.43 2: go version: go1.22.0 darwin/arm64


func DialRequestFunc() (net.Conn, error) {
    dialer := &net.Dialer{Timeout: 10}
    return dialer.Dial("tcp", fmt.Sprintf("%s:%s", "example.com", "80"))
}

func TestDialRequestFunc(t *testing.T) {
    tests := []struct {
        name    string
        want    net.Conn
        wantErr assert.ErrorAssertionFunc
        mock    func()
    }{
        {
            name: "1: error ",
            mock: func() {
                mock.Patch((*net.Dialer).Dial, func(_ *net.Dialer, network, address string) (net.Conn, error) {
                    fmt.Println("here")
                    return nil, errors.New("dial error")
                })
            },
            wantErr: assert.Error,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            tt.mock()
            got, err := DialRequestFunc()
            fmt.Println(err)
            if !tt.wantErr(t, err, fmt.Sprintf("TestDialRequest()")) {
                return
            }
            assert.Equalf(t, tt.want, got, "TestDialRequest()")
        })
    }
}
xhd2015 commented 3 months ago

@shubham-dogra-s1 This may be caused by build cache. You can run xgo exec --reset-instrument go version and try again to see if the problem still happens

shubham-dogra-s1 commented 3 months ago

@xhd2015 It was cache issue working fine xgo exec --reset-instrument go version. We can close the issue :)