diff --git a/go.sum b/go.sum index a2f15438c..9f5a5b4b0 100644 --- a/go.sum +++ b/go.sum @@ -840,6 +840,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= @@ -879,6 +880,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= diff --git a/pkg/remoting/getty/getty_client_test.go b/pkg/remoting/getty/getty_client_test.go new file mode 100644 index 000000000..5e9e62a16 --- /dev/null +++ b/pkg/remoting/getty/getty_client_test.go @@ -0,0 +1,160 @@ +/* + * 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 ( + "fmt" + "reflect" + "testing" + + "github.com/agiledragon/gomonkey" + getty "github.com/apache/dubbo-getty" + "github.com/seata/seata-go/pkg/protocol/message" + "github.com/stretchr/testify/assert" +) + +// TestGettyRemotingClient_SendSyncRequest unit test for SendSyncRequest function +func TestGettyRemotingClient_SendSyncRequest(t *testing.T) { + respMsg := message.GlobalBeginResponse{ + AbstractTransactionResponse: message.AbstractTransactionResponse{ + AbstractResultMessage: message.AbstractResultMessage{ + ResultCode: message.ResultCodeSuccess, + }, + }, + } + 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)) +} + +// TestGettyRemotingClient_SendAsyncResponse unit test for SendAsyncResponse function +func TestGettyRemotingClient_SendAsyncResponse(t *testing.T) { + 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) +} + +// 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) { + 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) + }) + } +} + +// 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) + }) + } +} diff --git a/pkg/remoting/getty/getty_remoting_test.go b/pkg/remoting/getty/getty_remoting_test.go new file mode 100644 index 000000000..d57ff38e4 --- /dev/null +++ b/pkg/remoting/getty/getty_remoting_test.go @@ -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 ( + "testing" + + "github.com/seata/seata-go/pkg/protocol/message" + "github.com/stretchr/testify/assert" +) + +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) + mergedWarpMessage := GetGettyRemotingInstance().GetMergedMessage(test.msgID) + assert.Empty(t, mergedWarpMessage) + } + }) + } +}