Open powerman opened 4 years ago
Can you share your TestGetStr
?
Seems duplicated of https://github.com/golang/go/issues/29264
@cuonglm Sure, but there is nothing interesting in it - I'm testing jsonrpc2 client using jsonrpc2 server implemented by gomock.
func TestGetStr(tt *testing.T) {
t := check.T(tt)
endpoint, mockServer := testNewServer(t)
c := New(Config{
Endpoint: endpoint,
PlatformKey: "key",
})
reqCtx := proto.Ctx{
PlatformKey: "key",
ServiceName: def.ProgName,
}
req := proto.IDGetStrReq{
Ctx: reqCtx,
Amount: 3,
}
resp := proto.IDGetStrResp{"rnd1", "rnd2"}
err42 := jsonrpc2.NewError(42, "answer")
mockServer.EXPECT().GetStr(req, gomock.Any()).SetArg(1, resp).Return(nil)
mockServer.EXPECT().GetStr(req, gomock.Any()).Return(err42)
res, err := c.GetStr(3)
t.Nil(err)
t.DeepEqual(res, resp)
res, err = c.GetStr(3)
t.Err(err, err42)
t.Nil(res)
}
func testNewServer(t *check.C) (string, *proto.MockServer) {
t.Helper()
ctrl := gomock.NewController(t)
mockServer := proto.NewMockServer(ctrl)
rpcAddr := netx.NewAddr("localhost", netx.UnusedTCPPort("localhost"))
ctx, cancel := context.WithCancel(ctx)
errc := make(chan error, 1)
go func() { errc <- serve.RPCName(ctx, rpcAddr, mockServer, "ID") }()
t.Cleanup(func() {
t.Helper()
cancel()
t.Nil(<-errc, "serve.RPC")
ctrl.Finish()
})
endpoint := fmt.Sprintf("http://%s/rpc", rpcAddr)
return endpoint, mockServer
}
@aclements
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I was running
go test
on my package, which in turn called https://godoc.org/github.com/powerman/check#C.Nil at value returned by https://godoc.org/github.com/powerman/rpc-codec/jsonrpc2#WrapError.The value itself was implemented this way https://github.com/powerman/rpc-codec/blob/v1.2.0/jsonrpc2/errors.go#L96-L116 and contains valid non-nil wrapped error inside.
Line https://github.com/powerman/rpc-codec/blob/v1.2.0/jsonrpc2/errors.go#L111 generates different panics at different
go test
runs, like these:Complete panic one
Complete panic two
What did you expect to see?
No panic. :)
What did you see instead?
Panic. :(
THE WORKAROUND
I was able to work around this issue by changing wrapped error to become a reference: https://github.com/powerman/rpc-codec/commit/c2902e88d7b3fa7ddd0c3b93ea95210b246a1aab But I don't think something was wrong with my code before that commit, so probably it's a Go bug.