From 82bcf9943ad4fc7f990f540069db0dc66ddc93dd Mon Sep 17 00:00:00 2001 From: XavierNiu Date: Fri, 4 Jun 2021 20:08:01 +0800 Subject: [PATCH 1/3] unittests for DubboCodec --- protocol/dubbo/dubbo_codec_test.go | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 protocol/dubbo/dubbo_codec_test.go diff --git a/protocol/dubbo/dubbo_codec_test.go b/protocol/dubbo/dubbo_codec_test.go new file mode 100644 index 0000000000..05fa55c2c1 --- /dev/null +++ b/protocol/dubbo/dubbo_codec_test.go @@ -0,0 +1,55 @@ +package dubbo + +import ( + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/protocol" + "dubbo.apache.org/dubbo-go/v3/remoting" + "github.com/stretchr/testify/assert" + "testing" +) + +var defaultDubboCodec = &DubboCodec{} + +func TestDubboCodec(t *testing.T) { + t.Run("url", testUrl) + t.Run("metadataInfo", testMetadataInfo) +} + +func testUrl(t *testing.T) { + // mock response + rsp := remoting.NewResponse(1, "") + // mock URL + url, err := common.NewURL("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider") + assert.NoError(t, err) + // mock RPCResult + rpcResult := protocol.RPCResult{Rest: url} + rsp.Result = rpcResult + // encode response + buf, err := defaultDubboCodec.EncodeResponse(rsp) + assert.NoError(t, err) + // decode response + // TODO: check the rest of return values + _, _, err = defaultDubboCodec.Decode(buf.Bytes()) + assert.NoError(t, err) +} + +func testMetadataInfo(t *testing.T) { + // mock response + rsp := remoting.NewResponse(1, "") + url, err := common.NewURL("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider") + assert.NoError(t, err) + serviceInfoMap := map[string]*common.ServiceInfo{ + "serviceInfo1": common.NewServiceInfoWithURL(url), + } + metadataInfo := common.NewMetadataInfo("test", "", serviceInfoMap) + // mock RPCResult + rpcResult := protocol.RPCResult{Rest: metadataInfo} + rsp.Result = rpcResult + // encode response + buf, err := defaultDubboCodec.EncodeResponse(rsp) + assert.NoError(t, err) + // decode response + // TODO: check the rest of return values + _, _, err = defaultDubboCodec.Decode(buf.Bytes()) + assert.NoError(t, err) +} From 2e8e1abd036d949df081d5e1992279e7f01f379c Mon Sep 17 00:00:00 2001 From: XavierNiu Date: Fri, 4 Jun 2021 23:22:47 +0800 Subject: [PATCH 2/3] fix serializing url bug and add error handling --- common/url.go | 6 +++ protocol/dubbo/dubbo_codec_test.go | 55 --------------------- protocol/dubbo/hessian2/hessian_response.go | 37 ++++++++++---- 3 files changed, 34 insertions(+), 64 deletions(-) delete mode 100644 protocol/dubbo/dubbo_codec_test.go diff --git a/common/url.go b/common/url.go index 07bda8eef1..fe716770c7 100644 --- a/common/url.go +++ b/common/url.go @@ -125,6 +125,12 @@ type URL struct { SubURL *URL } + +// JavaClassName POJO for URL +func (c *URL) JavaClassName() string { + return "org.apache.dubbo.common.URL" +} + // Option accepts URL // Option will define a function of handling URL type Option func(*URL) diff --git a/protocol/dubbo/dubbo_codec_test.go b/protocol/dubbo/dubbo_codec_test.go deleted file mode 100644 index 05fa55c2c1..0000000000 --- a/protocol/dubbo/dubbo_codec_test.go +++ /dev/null @@ -1,55 +0,0 @@ -package dubbo - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/protocol" - "dubbo.apache.org/dubbo-go/v3/remoting" - "github.com/stretchr/testify/assert" - "testing" -) - -var defaultDubboCodec = &DubboCodec{} - -func TestDubboCodec(t *testing.T) { - t.Run("url", testUrl) - t.Run("metadataInfo", testMetadataInfo) -} - -func testUrl(t *testing.T) { - // mock response - rsp := remoting.NewResponse(1, "") - // mock URL - url, err := common.NewURL("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider") - assert.NoError(t, err) - // mock RPCResult - rpcResult := protocol.RPCResult{Rest: url} - rsp.Result = rpcResult - // encode response - buf, err := defaultDubboCodec.EncodeResponse(rsp) - assert.NoError(t, err) - // decode response - // TODO: check the rest of return values - _, _, err = defaultDubboCodec.Decode(buf.Bytes()) - assert.NoError(t, err) -} - -func testMetadataInfo(t *testing.T) { - // mock response - rsp := remoting.NewResponse(1, "") - url, err := common.NewURL("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider") - assert.NoError(t, err) - serviceInfoMap := map[string]*common.ServiceInfo{ - "serviceInfo1": common.NewServiceInfoWithURL(url), - } - metadataInfo := common.NewMetadataInfo("test", "", serviceInfoMap) - // mock RPCResult - rpcResult := protocol.RPCResult{Rest: metadataInfo} - rsp.Result = rpcResult - // encode response - buf, err := defaultDubboCodec.EncodeResponse(rsp) - assert.NoError(t, err) - // decode response - // TODO: check the rest of return values - _, _, err = defaultDubboCodec.Decode(buf.Bytes()) - assert.NoError(t, err) -} diff --git a/protocol/dubbo/hessian2/hessian_response.go b/protocol/dubbo/hessian2/hessian_response.go index b0070a6436..47dc866cf0 100644 --- a/protocol/dubbo/hessian2/hessian_response.go +++ b/protocol/dubbo/hessian2/hessian_response.go @@ -111,30 +111,49 @@ func packResponse(header DubboHeader, ret interface{}) ([]byte, error) { } if response.Exception != nil { // throw error - _ = encoder.Encode(resWithException) + err := encoder.Encode(resWithException) + if err != nil { + return nil, perrors.Errorf("encoding response failed: %v", err) + } if t, ok := response.Exception.(java_exception.Throwabler); ok { - _ = encoder.Encode(t) + err = encoder.Encode(t) } else { - _ = encoder.Encode(java_exception.NewThrowable(response.Exception.Error())) + err = encoder.Encode(java_exception.NewThrowable(response.Exception.Error())) + } + if err != nil { + return nil, perrors.Errorf("encoding exception failed: %v", err) } } else { if response.RspObj == nil { - _ = encoder.Encode(resNullValue) + if err := encoder.Encode(resNullValue); err != nil { + return nil, perrors.Errorf("encoding null value failed: %v", err) + } } else { - _ = encoder.Encode(resValue) - _ = encoder.Encode(response.RspObj) // result + if err := encoder.Encode(resValue); err != nil { + return nil, perrors.Errorf("encoding response value failed: %v", err) + } + if err := encoder.Encode(response.RspObj); err != nil { + return nil, perrors.Errorf("encoding response failed: %v", err) + } } } + // attachments if atta { - _ = encoder.Encode(response.Attachments) // attachments + if err := encoder.Encode(response.Attachments); err != nil { + return nil, perrors.Errorf("encoding response attachements failed: %v", err) + } } } } else { + var err error if response.Exception != nil { // throw error - _ = encoder.Encode(response.Exception.Error()) + err = encoder.Encode(response.Exception.Error()) } else { - _ = encoder.Encode(response.RspObj) + err = encoder.Encode(response.RspObj) + } + if err != nil { + return nil, perrors.Errorf("encoding error failed: %v", err) } } From 0865622882f540111b275a72a714373707a21d9b Mon Sep 17 00:00:00 2001 From: XavierNiu Date: Sat, 5 Jun 2021 00:22:29 +0800 Subject: [PATCH 3/3] gofmt --- common/url.go | 1 - 1 file changed, 1 deletion(-) diff --git a/common/url.go b/common/url.go index fe716770c7..65d1748e2c 100644 --- a/common/url.go +++ b/common/url.go @@ -125,7 +125,6 @@ type URL struct { SubURL *URL } - // JavaClassName POJO for URL func (c *URL) JavaClassName() string { return "org.apache.dubbo.common.URL"