Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit testing for getty #203

Merged
merged 8 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -880,6 +881,8 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180530234432-1e491301e022/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
162 changes: 162 additions & 0 deletions pkg/remoting/getty/getty_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package getty

import (
betterwinsone marked this conversation as resolved.
Show resolved Hide resolved
"fmt"
"github.com/agiledragon/gomonkey"
getty "github.com/apache/dubbo-getty"
"github.com/seata/seata-go/pkg/protocol/message"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)

// TestGettyRemotingClient_SendSyncRequest unit test for SendSyncRequest function
func TestGettyRemotingClient_SendSyncRequest(t *testing.T) {
betterwinsone marked this conversation as resolved.
Show resolved Hide resolved
respMsg := message.GlobalBeginResponse{
AbstractTransactionResponse: message.AbstractTransactionResponse{
AbstractResultMessage: message.AbstractResultMessage{
ResultCode: message.ResultCodeSuccess,
},
},
}
mock := gomonkey.ApplyMethod(reflect.TypeOf(GetGettyRemotingInstance()), "SendSync",
func(_ *GettyRemoting, msg message.RpcMessage, s getty.Session, callback callbackMethod) (interface{},
error) {
return respMsg, nil
})
resp, err := GetGettyRemotingClient().SendSyncRequest("message")
assert.Empty(t, err)
assert.Equal(t, respMsg, resp.(message.GlobalBeginResponse))
mock.Reset()
betterwinsone marked this conversation as resolved.
Show resolved Hide resolved
}

// TestGettyRemotingClient_SendAsyncResponse unit test for SendAsyncResponse function
func TestGettyRemotingClient_SendAsyncResponse(t *testing.T) {
mock := gomonkey.ApplyMethod(reflect.TypeOf(GetGettyRemotingInstance()), "SendASync",
func(_ *GettyRemoting, msg message.RpcMessage, s getty.Session, callback callbackMethod) error {
return nil
})
err := GetGettyRemotingClient().SendAsyncResponse(1, "message")
assert.Empty(t, err)
mock.Reset()
betterwinsone marked this conversation as resolved.
Show resolved Hide resolved
}

// TestGettyRemotingClient_SendAsyncRequest unit test for SendAsyncRequest function
func TestGettyRemotingClient_SendAsyncRequest(t *testing.T) {
var tests = []struct {
name string
message interface{}
}{
{
name: "HeartBeatMessage",
message: message.HeartBeatMessage{},
},
{
name: "not HeartBeatMessage",
message: "message",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
mock := gomonkey.ApplyMethod(reflect.TypeOf(GetGettyRemotingInstance()), "SendASync",
func(_ *GettyRemoting, msg message.RpcMessage, s getty.Session, callback callbackMethod) error {
return nil
})
err := GetGettyRemotingClient().SendAsyncRequest(test.message)
assert.Empty(t, err)
mock.Reset()
})
}
}

// Test_syncCallback unit test for syncCallback function
func Test_syncCallback(t *testing.T) {
var tests = []struct {
name string
respMsg *message.MessageFuture
reqMsg message.RpcMessage
wantErr bool
}{
{
name: "timeout",
respMsg: message.NewMessageFuture(message.RpcMessage{
ID: 1,
}),
reqMsg: message.RpcMessage{
ID: 2,
},
wantErr: true,
},
{
name: "Done",
respMsg: message.NewMessageFuture(message.RpcMessage{
ID: 1,
}),
reqMsg: message.RpcMessage{
ID: 2,
},
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.wantErr {
response, err := GetGettyRemotingClient().syncCallback(test.reqMsg, test.respMsg)
assert.EqualError(t, err, fmt.Sprintf("wait response timeout, request: %#v", test.reqMsg))
assert.Empty(t, response)
} else {
go func() {
test.respMsg.Done <- struct{}{}
}()
response, err := GetGettyRemotingClient().syncCallback(test.reqMsg, test.respMsg)
assert.Empty(t, err)
assert.Empty(t, response)
}
})
}
}

// Test_asyncCallback unit test for asyncCallback function
func Test_asyncCallback(t *testing.T) {
var tests = []struct {
name string
respMsg *message.MessageFuture
reqMsg message.RpcMessage
wantErr bool
}{
{
name: "Done",
respMsg: message.NewMessageFuture(message.RpcMessage{
ID: 1,
}),
reqMsg: message.RpcMessage{
ID: 2,
},
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
response, err := GetGettyRemotingClient().asyncCallback(test.reqMsg, test.respMsg)
assert.Empty(t, err)
assert.Empty(t, response)
})
}
}
161 changes: 161 additions & 0 deletions pkg/remoting/getty/getty_remoting_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package getty

import (
"github.com/seata/seata-go/pkg/protocol/message"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGettyRemoting_GetMessageFuture(t *testing.T) {
var tests = []struct {
name string
msgID int32
messageFuture *message.MessageFuture
}{
{
name: "futures is null",
msgID: 1,
messageFuture: nil,
},
{
name: "futures not null",
msgID: 1,
messageFuture: &message.MessageFuture{
ID: 1,
Err: nil,
Response: nil,
Done: nil,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.messageFuture != nil {
GetGettyRemotingInstance().futures.Store(test.msgID, test.messageFuture)
messageFuture := GetGettyRemotingInstance().GetMessageFuture(test.msgID)
assert.Equal(t, *test.messageFuture, *messageFuture)
} else {
messageFuture := GetGettyRemotingInstance().GetMessageFuture(test.msgID)
assert.Empty(t, messageFuture)
}
})
}
}

func TestGettyRemoting_RemoveMessageFuture(t *testing.T) {
var tests = []struct {
name string
msgID int32
messageFuture *message.MessageFuture
}{
{
name: "test remove message future",
msgID: 1,
messageFuture: &message.MessageFuture{
ID: 1,
Err: nil,
Response: nil,
Done: nil,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
GetGettyRemotingInstance().futures.Store(test.msgID, test.messageFuture)
messageFuture := GetGettyRemotingInstance().GetMessageFuture(test.msgID)
assert.Equal(t, messageFuture, test.messageFuture)
GetGettyRemotingInstance().RemoveMessageFuture(test.msgID)
messageFuture = GetGettyRemotingInstance().GetMessageFuture(test.msgID)
assert.Empty(t, messageFuture)
})
}
}

func TestGettyRemoting_GetMergedMessage(t *testing.T) {
var tests = []struct {
name string
msgID int32
mergedWarpMessage *message.MergedWarpMessage
}{
{
name: "mergeMsgMap is null",
msgID: 1,
mergedWarpMessage: nil,
},
{
name: "mergeMsgMap not null",
msgID: 1,
mergedWarpMessage: &message.MergedWarpMessage{
Msgs: []message.MessageTypeAware{},
MsgIds: []int32{1, 2},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.mergedWarpMessage != nil {
GetGettyRemotingInstance().mergeMsgMap.Store(test.msgID, test.mergedWarpMessage)
mergedWarpMessage := GetGettyRemotingInstance().GetMergedMessage(test.msgID)
assert.Equal(t, *test.mergedWarpMessage, *mergedWarpMessage)
} else {
mergedWarpMessage := GetGettyRemotingInstance().GetMessageFuture(test.msgID)
assert.Empty(t, mergedWarpMessage)
}
})
}
}

func TestGettyRemoting_RemoveMergedMessageFuture(t *testing.T) {
var tests = []struct {
name string
msgID int32
mergedWarpMessage *message.MergedWarpMessage
}{
{
name: "mergeMsgMap is null",
msgID: 1,
mergedWarpMessage: nil,
},
{
name: "mergeMsgMap not null",
msgID: 1,
mergedWarpMessage: &message.MergedWarpMessage{
Msgs: []message.MessageTypeAware{},
MsgIds: []int32{1, 2},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.mergedWarpMessage != nil {
GetGettyRemotingInstance().mergeMsgMap.Store(test.msgID, test.mergedWarpMessage)
mergedWarpMessage := GetGettyRemotingInstance().GetMergedMessage(test.msgID)
assert.NotEmpty(t, mergedWarpMessage)
GetGettyRemotingInstance().RemoveMergedMessageFuture(test.msgID)
mergedWarpMessage = GetGettyRemotingInstance().GetMergedMessage(test.msgID)
assert.Empty(t, mergedWarpMessage)
} else {
GetGettyRemotingInstance().RemoveMergedMessageFuture(test.msgID)
betterwinsone marked this conversation as resolved.
Show resolved Hide resolved
mergedWarpMessage := GetGettyRemotingInstance().GetMergedMessage(test.msgID)
assert.Empty(t, mergedWarpMessage)
}
})
}
}