Closed shubham-dogra-s1 closed 3 months ago
Are you using xgo test
or xgo run
?
You can try to add --trap-stdlib
to force stdlib mock
using xgo test, tried with --trap-stdlib
same error
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
@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
@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 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.
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
How to reproduce?
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()")
})
}
}
@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
@xhd2015 It was cache issue working fine xgo exec --reset-instrument go version
. We can close the issue :)
xgo version: v1.0.42