This is an automated email from the ASF dual-hosted git repository.

dmwangnima pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 8a87c62b9 fix: triple msgpack invocation (#2613)
8a87c62b9 is described below

commit 8a87c62b9fbcdb2b99d90581bd9b270fff03f9da
Author: Scout Wang <[email protected]>
AuthorDate: Sun Mar 10 11:17:59 2024 +0800

    fix: triple msgpack invocation (#2613)
    
    * fix: triple msgpack invocation
    
    * fix triple test
    
    * add msgpackCodec unit test
    
    * fix golang-ci lint
    
    * fix ineffectual assignment
---
 protocol/triple/triple_protocol/codec.go           | 21 +++++++++++--------
 protocol/triple/triple_protocol/codec_test.go      | 17 +++++++++++++++
 protocol/triple/triple_protocol/triple_ext_test.go |  4 ++--
 protocol/triple/triple_test.go                     | 24 ++++++++++++++++------
 4 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/protocol/triple/triple_protocol/codec.go 
b/protocol/triple/triple_protocol/codec.go
index 84ab75c65..3c60b9c07 100644
--- a/protocol/triple/triple_protocol/codec.go
+++ b/protocol/triple/triple_protocol/codec.go
@@ -16,14 +16,15 @@ package triple_protocol
 
 import (
        "bytes"
-       
"dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol/internal/interoperability"
        "encoding/json"
        "errors"
        "fmt"
-       hessian "github.com/apache/dubbo-go-hessian2"
-       perrors "github.com/pkg/errors"
        "reflect"
        "time"
+
+       
"dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol/internal/interoperability"
+       hessian "github.com/apache/dubbo-go-hessian2"
+       perrors "github.com/pkg/errors"
 )
 
 import (
@@ -192,10 +193,13 @@ func (c *protoWrapperCodec) Name() string {
 }
 
 func (c *protoWrapperCodec) Marshal(message interface{}) ([]byte, error) {
-       reqs, ok := message.([]interface{})
+       var reqs []interface{}
+       var ok bool
+       reqs, ok = message.([]interface{})
        if !ok {
-               return c.innerCodec.Marshal(message)
+               reqs = []interface{}{message}
        }
+
        reqsLen := len(reqs)
        reqsBytes := make([][]byte, reqsLen)
        reqsTypes := make([]string, reqsLen)
@@ -218,9 +222,11 @@ func (c *protoWrapperCodec) Marshal(message interface{}) 
([]byte, error) {
 }
 
 func (c *protoWrapperCodec) Unmarshal(binary []byte, message interface{}) 
error {
-       params, ok := message.([]interface{})
+       var params []interface{}
+       var ok bool
+       params, ok = message.([]interface{})
        if !ok {
-               return c.innerCodec.Unmarshal(binary, message)
+               params = []interface{}{message}
        }
 
        var wrapperReq interoperability.TripleRequestWrapper
@@ -269,7 +275,6 @@ func (c *hessian2Codec) Unmarshal(binary []byte, message 
interface{}) error {
        return reflectResponse(val, message)
 }
 
-// todo(DMwangnima): add unit tests
 type msgpackCodec struct{}
 
 func (c *msgpackCodec) Name() string {
diff --git a/protocol/triple/triple_protocol/codec_test.go 
b/protocol/triple/triple_protocol/codec_test.go
index 9a2e31f77..c12504bc9 100644
--- a/protocol/triple/triple_protocol/codec_test.go
+++ b/protocol/triple/triple_protocol/codec_test.go
@@ -116,3 +116,20 @@ func TestJSONCodec(t *testing.T) {
                assert.Sprintf(`error message should explain that "" is not a 
valid JSON object`),
        )
 }
+
+func TestMsgpackCodec(t *testing.T) {
+       t.Parallel()
+
+       want := &pingv1.PingRequest{
+               Number: 1234,
+               Text:   "5678",
+       }
+       codec := &msgpackCodec{}
+       binary, err := codec.Marshal(want)
+       assert.Nil(t, err)
+       var got pingv1.PingRequest
+       err = codec.Unmarshal(binary, &got)
+       assert.Nil(t, err)
+       assert.Equal(t, got.Number, want.Number)
+       assert.Equal(t, got.Text, want.Text)
+}
diff --git a/protocol/triple/triple_protocol/triple_ext_test.go 
b/protocol/triple/triple_protocol/triple_ext_test.go
index d4b82543d..cb305f844 100644
--- a/protocol/triple/triple_protocol/triple_ext_test.go
+++ b/protocol/triple/triple_protocol/triple_ext_test.go
@@ -320,7 +320,7 @@ func TestServer(t *testing.T) {
                                return
                        }
                        if sendErr := stream.Send(&pingv1.CumSumRequest{Number: 
42}); sendErr != nil {
-                               assert.ErrorIs(t, err, io.EOF)
+                               assert.ErrorIs(t, sendErr, io.EOF)
                                assert.Equal(t, triple.CodeOf(err), 
triple.CodeUnknown)
                        }
                        // We didn't send the headers the server expects, so we 
should now get an
@@ -2008,7 +2008,7 @@ func TestBidiOverHTTP1(t *testing.T) {
        stream, err := client.CumSum(context.Background())
        assert.Nil(t, err)
        if sendErr := stream.Send(&pingv1.CumSumRequest{Number: 2}); sendErr != 
nil {
-               assert.ErrorIs(t, err, io.EOF)
+               assert.ErrorIs(t, sendErr, io.EOF)
        }
        err = stream.Receive(&pingv1.CumSumResponse{})
        assert.NotNil(t, err)
diff --git a/protocol/triple/triple_test.go b/protocol/triple/triple_test.go
index 19c9f6d95..31568dac2 100644
--- a/protocol/triple/triple_test.go
+++ b/protocol/triple/triple_test.go
@@ -241,11 +241,12 @@ func TestInvoke(t *testing.T) {
                return reply.Interface()
        }
 
-       invokeTripleCodeFunc := func(t *testing.T, invoker protocol.Invoker, 
identifier string) {
+       invokeTripleCodeFunc := func(t *testing.T, invoker protocol.Invoker, 
identifier string, isIDL bool) {
                tests := []struct {
                        methodName string
                        callType   string
                        rawParams  []interface{}
+                       needIDL    bool
                        validate   func(t *testing.T, rawParams []interface{}, 
res protocol.Result)
                }{
                        {
@@ -268,6 +269,7 @@ func TestInvoke(t *testing.T) {
                        {
                                methodName: "GreetClientStream",
                                callType:   constant.CallClientStream,
+                               needIDL:    true,
                                validate: func(t *testing.T, params 
[]interface{}, res protocol.Result) {
                                        assert.Nil(t, res.Error())
                                        streamRaw, ok := 
res.Result().(*triple_protocol.ClientStreamForClient)
@@ -295,6 +297,7 @@ func TestInvoke(t *testing.T) {
                                                Name: "dubbo",
                                        },
                                },
+                               needIDL: true,
                                validate: func(t *testing.T, params 
[]interface{}, res protocol.Result) {
                                        assert.Nil(t, res.Error())
                                        req := 
params[0].(*greet.GreetServerStreamRequest)
@@ -314,6 +317,7 @@ func TestInvoke(t *testing.T) {
                        {
                                methodName: "GreetStream",
                                callType:   constant.CallBidiStream,
+                               needIDL:    true,
                                validate: func(t *testing.T, params 
[]interface{}, res protocol.Result) {
                                        assert.Nil(t, res.Error())
                                        streamRaw, ok := 
res.Result().(*triple_protocol.BidiStreamForClient)
@@ -333,6 +337,9 @@ func TestInvoke(t *testing.T) {
                }
 
                for _, test := range tests {
+                       if test.needIDL && !isIDL {
+                               continue
+                       }
                        t.Run(test.methodName, func(t *testing.T) {
                                inv := tripleInvocationInit(test.methodName, 
test.rawParams, test.callType)
                                res := invoker.Invoke(context.Background(), inv)
@@ -436,27 +443,32 @@ func TestInvoke(t *testing.T) {
        t.Run("triple2triple", func(t *testing.T) {
                invoker, err := tripleInvokerInit(localAddr, triplePort, 
customTripleInterfaceName, "", "", 
greettriple.GreetService_ClientInfo.MethodNames, "", 
&greettriple.GreetService_ClientInfo)
                assert.Nil(t, err)
-               invokeTripleCodeFunc(t, invoker, "")
+               invokeTripleCodeFunc(t, invoker, "", true)
        })
        t.Run("triple2triple_JsonSerialization", func(t *testing.T) {
                invoker, err := tripleInvokerInit(localAddr, triplePort, 
customTripleInterfaceName, "", "", 
greettriple.GreetService_ClientInfo.MethodNames, constant.JSONSerialization, 
&greettriple.GreetService_ClientInfo)
                assert.Nil(t, err)
-               invokeTripleCodeFunc(t, invoker, "")
+               invokeTripleCodeFunc(t, invoker, "", true)
+       })
+       t.Run("triple2triple_MsgpackSerialization", func(t *testing.T) {
+               invoker, err := tripleInvokerInit(localAddr, triplePort, 
customTripleInterfaceName, "", "", 
greettriple.GreetService_ClientInfo.MethodNames, constant.MsgpackSerialization, 
&greettriple.GreetService_ClientInfo)
+               assert.Nil(t, err)
+               invokeTripleCodeFunc(t, invoker, "", false)
        })
        t.Run("triple2triple_Group1Version1", func(t *testing.T) {
                invoker, err := tripleInvokerInit(localAddr, triplePort, 
customTripleInterfaceName, group, version, 
greettriple.GreetService_ClientInfo.MethodNames, "", 
&greettriple.GreetService_ClientInfo)
                assert.Nil(t, err)
-               invokeTripleCodeFunc(t, invoker, api.GroupVersionIdentifier)
+               invokeTripleCodeFunc(t, invoker, api.GroupVersionIdentifier, 
true)
        })
        t.Run("triple2dubbo3", func(t *testing.T) {
                invoker, err := tripleInvokerInit(localAddr, dubbo3Port, 
customDubbo3InterfaceName, "", "", 
greettriple.GreetService_ClientInfo.MethodNames, "", 
&greettriple.GreetService_ClientInfo)
                assert.Nil(t, err)
-               invokeTripleCodeFunc(t, invoker, "")
+               invokeTripleCodeFunc(t, invoker, "", true)
        })
        t.Run("triple2dubbo3_Group1Version1", func(t *testing.T) {
                invoker, err := tripleInvokerInit(localAddr, dubbo3Port, 
customDubbo3InterfaceName, group, version, 
greettriple.GreetService_ClientInfo.MethodNames, "", 
&greettriple.GreetService_ClientInfo)
                assert.Nil(t, err)
-               invokeTripleCodeFunc(t, invoker, 
dubbo3_api.GroupVersionIdentifier)
+               invokeTripleCodeFunc(t, invoker, 
dubbo3_api.GroupVersionIdentifier, true)
        })
        t.Run("dubbo32triple", func(t *testing.T) {
                svc := new(dubbo3_greet.GreetServiceClientImpl)

Reply via email to