Skip to content

Commit

Permalink
fix ConvertToReqMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhua3 committed Nov 6, 2024
1 parent ee5a90c commit 63d02d4
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) {
// 创建一个模拟的请求
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, "")

// 设置RPC上下文
rc := request.GetRPCContext(true)
rc.Serialized = false
rc.Oneway = true
rc.Proxy = true
rc.GzipSize = 1024

// 使用SimpleSerialization进行序列化
serialization := &serialize.SimpleSerialization{}

// 调用ConvertToReqMessage
msg, err := ConvertToReqMessage(request, serialization)

// 断言
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))

// 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 63d02d4

Please sign in to comment.