Skip to content

Commit

Permalink
Merge pull request #408 from cocowh/fix_ConvertToReqMessage
Browse files Browse the repository at this point in the history
fix ConvertToReqMessage
  • Loading branch information
rayzhang0603 authored Nov 7, 2024
2 parents ee5a90c + 272a654 commit dee077b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
3 changes: 1 addition & 2 deletions protocol/motanProtocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"time"

motan "github.com/weibocom/motan-go/core"
"github.com/weibocom/motan-go/log"
vlog "github.com/weibocom/motan-go/log"
)

const (
Expand Down Expand Up @@ -689,7 +689,6 @@ func ConvertToReqMessage(request motan.Request, serialize motan.Serialization) (
if rc.Proxy {
req.Header.SetProxy(true)
}
req.Header.SetSerialize(serialize.GetSerialNum())
req.Metadata.Store(MPath, request.GetServiceName())
req.Metadata.Store(MMethod, request.GetMethod())
if request.GetAttachment(MProxyProtocol) == "" {
Expand Down
46 changes: 46 additions & 0 deletions protocol/motanProtocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,52 @@ func TestConvertToRequest(t *testing.T) {
}
}

func TestConvertToReqMessage(t *testing.T) {
// create mock request
request := core.AcquireMotanRequest()
request.RequestID = 12345
request.ServiceName = "testService"
request.Method = "testMethod"
request.MethodDesc = "testMethodDesc"
request.Attachment = core.NewStringMap(0)
request.Attachment.Store(MGroup, "testGroup")
request.Attachment.Store(MProxyProtocol, "")

// set RpcContext
rc := request.GetRPCContext(true)
rc.Serialized = false
rc.Oneway = true
rc.Proxy = true
rc.GzipSize = 1024

// use SimpleSerialization as the serialization param
serialization := &serialize.SimpleSerialization{}
// call ConvertToReqMessage
msg, err := ConvertToReqMessage(request, serialization)

// assert msg
assert.Nil(t, err)
assert.NotNil(t, msg)
assert.Equal(t, request.RequestID, msg.Header.RequestID)
assert.True(t, msg.Header.IsOneWay())
assert.True(t, msg.Header.IsProxy())
assert.Equal(t, serialization.GetSerialNum(), msg.Header.GetSerialize())
assert.Equal(t, "testService", msg.Metadata.LoadOrEmpty(MPath))
assert.Equal(t, "testMethod", msg.Metadata.LoadOrEmpty(MMethod))
assert.Equal(t, "motan2", msg.Metadata.LoadOrEmpty(MProxyProtocol))
assert.Equal(t, "testMethodDesc", msg.Metadata.LoadOrEmpty(MMethodDesc))
assert.Equal(t, "testGroup", msg.Metadata.LoadOrEmpty(MGroup))

// use rc serialize
rc.Serialized = true
rc.SerializeNum = serialize.BreezeNumber
msg, err = ConvertToReqMessage(request, serialization)

assert.Nil(t, err)
assert.NotNil(t, msg)
assert.Equal(t, serialize.BreezeNumber, msg.Header.GetSerialize())
}

func BenchmarkEncodeGzip(b *testing.B) {
DefaultGzipLevel = gzip.BestSpeed
bs := buildBytes(10 * 1024)
Expand Down

0 comments on commit dee077b

Please sign in to comment.