Copilot commented on code in PR #1164: URL: https://github.com/apache/incubator-seata-go/pull/1164#discussion_r4028757862
########## pkg/remoting/processor/client/rm_branch_end_processor.go: ########## @@ -0,0 +1,80 @@ +/* + * 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 client + +import ( + "errors" + + "seata.apache.org/seata-go/v2/pkg/protocol/branch" + "seata.apache.org/seata-go/v2/pkg/protocol/message" + "seata.apache.org/seata-go/v2/pkg/remoting/grpc/pb" + "seata.apache.org/seata-go/v2/pkg/rm" +) + +// branchEndResult is the protocol-independent result of a branch operation. +type branchEndResult struct { + status branch.BranchStatus + resultCode message.ResultCode + errMsg string +} + +func newBranchEndResult(status branch.BranchStatus, bizErr error) branchEndResult { + result := branchEndResult{status: status, resultCode: message.ResultCodeSuccess} + if bizErr != nil { + result.resultCode = message.ResultCodeFailed + result.errMsg = bizErr.Error() + } + return result +} + +func branchEndResultCodeProto(resultCode message.ResultCode) pb.ResultCodeProto { + if resultCode == message.ResultCodeFailed { + return pb.ResultCodeProto_Failed + } + return pb.ResultCodeProto_Success +} + +func branchEndSendResponse( + sendResponse func(int32, interface{}) error, + fallback func(int32, interface{}) error, +) func(int32, interface{}) error { + if sendResponse != nil { + return sendResponse + } + return fallback +} + +func branchEndProcessError(bizErr, sendErr error) error { + if sendErr == nil { + return nil + } + if bizErr == nil { + return sendErr + } + return errors.Join(bizErr, sendErr) Review Comment: `errors.Join` requires Go 1.20+. If this repository/module still targets Go <1.20, this will not compile. Please confirm the `go` version in `go.mod` is >= 1.20, or replace `errors.Join` with an alternative compatible approach (e.g., wrapping one error with the other). ########## pkg/remoting/processor/client/rm_branch_commit_processor_test.go: ########## @@ -19,93 +19,253 @@ package client import ( "context" + "errors" + "sync" "testing" - "seata.apache.org/seata-go/v2/pkg/rm/tcc" - - model2 "seata.apache.org/seata-go/v2/pkg/protocol/branch" - "seata.apache.org/seata-go/v2/pkg/protocol/codec" + "github.com/stretchr/testify/require" + "seata.apache.org/seata-go/v2/pkg/protocol" + "seata.apache.org/seata-go/v2/pkg/protocol/branch" "seata.apache.org/seata-go/v2/pkg/protocol/message" "seata.apache.org/seata-go/v2/pkg/remoting/config" "seata.apache.org/seata-go/v2/pkg/remoting/grpc/pb" "seata.apache.org/seata-go/v2/pkg/rm" ) -func TestRmBranchCommitProcessor(t *testing.T) { - // testcases +type testResourceManager struct { + branchType branch.BranchType + commitStatus branch.BranchStatus + commitErr error + rollbackStatus branch.BranchStatus + rollbackErr error +} + +func (m *testResourceManager) BranchCommit(context.Context, rm.BranchResource) (branch.BranchStatus, error) { + return m.commitStatus, m.commitErr +} +func (m *testResourceManager) BranchRollback(context.Context, rm.BranchResource) (branch.BranchStatus, error) { + return m.rollbackStatus, m.rollbackErr +} +func (*testResourceManager) BranchRegister(context.Context, rm.BranchRegisterParam) (int64, error) { + return 0, nil +} +func (*testResourceManager) BranchReport(context.Context, rm.BranchReportParam) error { return nil } +func (*testResourceManager) LockQuery(context.Context, rm.LockQueryParam) (bool, error) { + return false, nil +} +func (*testResourceManager) RegisterResource(rm.Resource) error { return nil } +func (*testResourceManager) UnregisterResource(rm.Resource) error { return nil } +func (*testResourceManager) GetCachedResources() *sync.Map { return &sync.Map{} } +func (m *testResourceManager) GetBranchType() branch.BranchType { return m.branchType } + +func TestBranchEndSendResponse(t *testing.T) { + injectedCalled := false + fallbackCalled := false + injected := func(int32, interface{}) error { + injectedCalled = true + return nil + } + fallback := func(int32, interface{}) error { + fallbackCalled = true + return nil + } + + require.NoError(t, branchEndSendResponse(injected, fallback)(1, "response")) + require.True(t, injectedCalled) + require.False(t, fallbackCalled) + + injectedCalled = false + require.NoError(t, branchEndSendResponse(nil, fallback)(2, "response")) + require.False(t, injectedCalled) + require.True(t, fallbackCalled) +} + +func TestBranchEndResult(t *testing.T) { + bizErr := errors.New("operation failed") + failed := newBranchEndResult(branch.BranchStatusPhasetwoCommitFailedRetryable, bizErr) + require.Equal(t, message.ResultCodeFailed, failed.resultCode) + require.Equal(t, pb.ResultCodeProto_Failed, branchEndResultCodeProto(failed.resultCode)) + require.Equal(t, bizErr.Error(), failed.errMsg) + + success := newBranchEndResult(branch.BranchStatusPhasetwoCommitted, nil) + require.Equal(t, message.ResultCodeSuccess, success.resultCode) + require.Equal(t, pb.ResultCodeProto_Success, branchEndResultCodeProto(success.resultCode)) + require.Empty(t, success.errMsg) + + sendErr := errors.New("send failed") + require.NoError(t, branchEndProcessError(bizErr, nil)) + require.ErrorIs(t, branchEndProcessError(nil, sendErr), sendErr) + combined := branchEndProcessError(bizErr, sendErr) + require.ErrorIs(t, combined, bizErr) + require.ErrorIs(t, combined, sendErr) + + injectedManager := &testResourceManager{} + require.Same(t, injectedManager, branchEndResourceManager( + func(branch.BranchType) rm.ResourceManager { return injectedManager }, + branch.BranchTypeTCC, + )) + + fallbackType := branch.BranchType(99) + fallbackManager := &testResourceManager{branchType: fallbackType} + rmCache := rm.GetRmCacheInstance() + rmCache.RegisterResourceManager(fallbackManager) + t.Cleanup(func() { rmCache.UnregisterResourceManager(fallbackType) }) + require.Same(t, fallbackManager, branchEndResourceManager(nil, fallbackType)) +} + +func TestRmBranchCommitProcessor_SendsFailureResponse(t *testing.T) { + bizErr := errors.New("commit failed") + manager := &testResourceManager{commitStatus: branch.BranchStatusPhasetwoCommitFailedRetryable, commitErr: bizErr} + + t.Run("getty", func(t *testing.T) { + var sent interface{} + processor := rmBranchCommitProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(_ int32, response interface{}) error { sent = response; return nil }, + } + err := processor.handleGettyBranchCommit(context.Background(), message.RpcMessage{ID: 1, Body: message.BranchCommitRequest{ + AbstractBranchEndRequest: message.AbstractBranchEndRequest{Xid: "xid", BranchId: 7, BranchType: branch.BranchTypeTCC, ResourceId: "resource"}, + }}) + require.NoError(t, err) + got := sent.(message.BranchCommitResponse) + require.Equal(t, message.ResultCodeFailed, got.ResultCode) + require.Equal(t, bizErr.Error(), got.Msg) + require.Equal(t, manager.commitStatus, got.BranchStatus) + require.Equal(t, "xid", got.Xid) + require.Equal(t, int64(7), got.BranchId) + }) + + t.Run("grpc", func(t *testing.T) { + var sent interface{} + processor := rmBranchCommitProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(_ int32, response interface{}) error { sent = response; return nil }, + } + err := processor.handleGrpcBranchCommit(context.Background(), message.RpcMessage{ID: 1, Body: &pb.BranchCommitRequestProto{ + AbstractBranchEndRequest: &pb.AbstractBranchEndRequestProto{Xid: "xid", BranchId: 7, BranchType: pb.BranchTypeProto_TCC, ResourceId: "resource"}, + }}) + require.NoError(t, err) + got := sent.(*pb.BranchCommitResponseProto) + result := got.AbstractBranchEndResponse.AbstractTransactionResponse.AbstractResultMessage + require.Equal(t, pb.ResultCodeProto_Failed, result.ResultCode) + require.Equal(t, bizErr.Error(), result.Msg) + require.Equal(t, pb.BranchStatusProto(manager.commitStatus), got.AbstractBranchEndResponse.BranchStatus) + require.Equal(t, "xid", got.AbstractBranchEndResponse.Xid) + require.Equal(t, int64(7), got.AbstractBranchEndResponse.BranchId) + }) +} + +func TestRmBranchCommitProcessor_ObservesBusinessAndSendErrors(t *testing.T) { + bizErr := errors.New("commit failed") + sendErr := errors.New("send failed") + manager := &testResourceManager{commitStatus: branch.BranchStatusPhasetwoCommitFailedRetryable, commitErr: bizErr} + + t.Run("getty", func(t *testing.T) { + processor := rmBranchCommitProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(int32, interface{}) error { return sendErr }, + } + err := processor.handleGettyBranchCommit(context.Background(), message.RpcMessage{ID: 1, Body: message.BranchCommitRequest{ + AbstractBranchEndRequest: message.AbstractBranchEndRequest{Xid: "xid", BranchId: 7, BranchType: branch.BranchTypeTCC, ResourceId: "resource"}, + }}) + require.ErrorIs(t, err, bizErr) + require.ErrorIs(t, err, sendErr) + }) + + t.Run("grpc", func(t *testing.T) { + processor := rmBranchCommitProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(int32, interface{}) error { return sendErr }, + } + err := processor.handleGrpcBranchCommit(context.Background(), message.RpcMessage{ID: 1, Body: &pb.BranchCommitRequestProto{ + AbstractBranchEndRequest: &pb.AbstractBranchEndRequestProto{Xid: "xid", BranchId: 7, BranchType: pb.BranchTypeProto_TCC, ResourceId: "resource"}, + }}) + require.ErrorIs(t, err, bizErr) + require.ErrorIs(t, err, sendErr) + }) +} + +func TestRmBranchCommitProcessor_SendsSuccessResponse(t *testing.T) { + manager := &testResourceManager{commitStatus: branch.BranchStatusPhasetwoCommitted} + + t.Run("getty", func(t *testing.T) { + var sent interface{} + processor := rmBranchCommitProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(_ int32, response interface{}) error { sent = response; return nil }, + } + err := processor.handleGettyBranchCommit(context.Background(), message.RpcMessage{ID: 1, Body: message.BranchCommitRequest{ + AbstractBranchEndRequest: message.AbstractBranchEndRequest{Xid: "xid", BranchId: 7, BranchType: branch.BranchTypeTCC, ResourceId: "resource"}, + }}) + require.NoError(t, err) + got := sent.(message.BranchCommitResponse) + require.Equal(t, message.ResultCodeSuccess, got.ResultCode) + require.Empty(t, got.Msg) + require.Equal(t, manager.commitStatus, got.BranchStatus) + require.Equal(t, "xid", got.Xid) + require.Equal(t, int64(7), got.BranchId) + }) + + t.Run("grpc", func(t *testing.T) { + var sent interface{} + processor := rmBranchCommitProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(_ int32, response interface{}) error { sent = response; return nil }, + } + err := processor.handleGrpcBranchCommit(context.Background(), message.RpcMessage{ID: 1, Body: &pb.BranchCommitRequestProto{ + AbstractBranchEndRequest: &pb.AbstractBranchEndRequestProto{Xid: "xid", BranchId: 7, BranchType: pb.BranchTypeProto_TCC, ResourceId: "resource"}, + }}) + require.NoError(t, err) + got := sent.(*pb.BranchCommitResponseProto) + result := got.AbstractBranchEndResponse.AbstractTransactionResponse.AbstractResultMessage + require.Equal(t, pb.ResultCodeProto_Success, result.ResultCode) + require.Empty(t, result.Msg) + require.Equal(t, pb.BranchStatusProto(manager.commitStatus), got.AbstractBranchEndResponse.BranchStatus) + require.Equal(t, "xid", got.AbstractBranchEndResponse.Xid) + require.Equal(t, int64(7), got.AbstractBranchEndResponse.BranchId) + }) +} + +func TestRmBranchCommitProcessor_ProcessRoutesByProtocol(t *testing.T) { + previous := config.GetTransportConfig() + t.Cleanup(func() { config.InitTransportConfig(previous) }) + + manager := &testResourceManager{commitStatus: branch.BranchStatusPhasetwoCommitted} tests := []struct { - name string // testcase name - protocol string // protocol:seata/grpc - rpcMsg message.RpcMessage // rpcMessage case - wantErr bool // want testcase err or not + name string + protocol protocol.Protocol + body interface{} + wantType interface{} }{ { - name: "rbc-testcase1-failure", - protocol: "seata", - rpcMsg: message.RpcMessage{ - ID: 123, - Type: message.RequestType(message.MessageTypeBranchCommit), - Codec: byte(codec.CodecTypeSeata), - Compressor: byte(1), - HeadMap: map[string]string{ - "name": " Jack", - "age": "12", - "address": "Beijing", - }, - Body: message.BranchCommitRequest{ - AbstractBranchEndRequest: message.AbstractBranchEndRequest{ - Xid: "123344", - BranchId: 56678, - BranchType: model2.BranchTypeTCC, - ResourceId: "1232323", - ApplicationData: []byte("TestExtraData"), - }, - }, - }, - - wantErr: true, // need dail to server, so err accured + name: "seata", + protocol: protocol.ProtocolSEATA, + body: message.BranchCommitRequest{AbstractBranchEndRequest: message.AbstractBranchEndRequest{ + Xid: "xid", BranchId: 7, BranchType: branch.BranchTypeTCC, ResourceId: "resource", + }}, + wantType: message.BranchCommitResponse{}, }, { - name: "rbc-testcase2-failure", - protocol: "grpc", - rpcMsg: message.RpcMessage{ - ID: 123, - Type: message.RequestType(message.MessageTypeBranchCommit), - HeadMap: map[string]string{ - "name": " Jack", - "age": "12", - "address": "Beijing", - }, - Body: &pb.BranchCommitRequestProto{ - AbstractBranchEndRequest: &pb.AbstractBranchEndRequestProto{ - Xid: "123345", - BranchId: 56679, - BranchType: pb.BranchTypeProto_TCC, - ResourceId: "1232324", - ApplicationData: "TestExtraData", - }, - }, - }, - - wantErr: true, // need dail to server, so err accured + name: "grpc", + protocol: protocol.ProtocolGRPC, + body: &pb.BranchCommitRequestProto{AbstractBranchEndRequest: &pb.AbstractBranchEndRequestProto{ + Xid: "xid", BranchId: 7, BranchType: pb.BranchTypeProto_TCC, ResourceId: "resource", + }}, + wantType: (*pb.BranchCommitResponseProto)(nil), }, } - var ctx context.Context - var rbcProcessor rmBranchCommitProcessor - - // run tests - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - config.InitTransportConfig(&config.TransportConfig{Protocol: tc.protocol}) - - rm.GetRmCacheInstance().RegisterResourceManager(tcc.GetTCCResourceManagerInstance()) - - err := rbcProcessor.Process(ctx, tc.rpcMsg) - if (err != nil) != tc.wantErr { - t.Errorf("rmBranchCommitProcessor wantErr: %v, got: %v", tc.wantErr, err) - return + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + config.InitTransportConfig(&config.TransportConfig{Protocol: tt.protocol.String()}) Review Comment: `protocol.Protocol` may not implement a `String()` method (e.g., if it is a `type Protocol string`), which would cause this test to fail to compile. Consider passing the protocol value directly (if it is already a string-like type) or using an explicit conversion such as `string(tt.protocol)` instead of calling `.String()`. ########## pkg/remoting/processor/client/rm_branch_rollback_processor_test.go: ########## @@ -19,93 +19,171 @@ package client import ( "context" + "errors" "testing" - "seata.apache.org/seata-go/v2/pkg/rm/tcc" - - model2 "seata.apache.org/seata-go/v2/pkg/protocol/branch" - "seata.apache.org/seata-go/v2/pkg/protocol/codec" + "github.com/stretchr/testify/require" + "seata.apache.org/seata-go/v2/pkg/protocol" + "seata.apache.org/seata-go/v2/pkg/protocol/branch" "seata.apache.org/seata-go/v2/pkg/protocol/message" "seata.apache.org/seata-go/v2/pkg/remoting/config" "seata.apache.org/seata-go/v2/pkg/remoting/grpc/pb" "seata.apache.org/seata-go/v2/pkg/rm" ) -func TestRmBranchRollbackProcessor(t *testing.T) { - // testcases +func TestRmBranchRollbackProcessor_SendsFailureResponse(t *testing.T) { + bizErr := errors.New("rollback failed") + manager := &testResourceManager{rollbackStatus: branch.BranchStatusPhasetwoRollbackFailedRetryable, rollbackErr: bizErr} + + t.Run("getty", func(t *testing.T) { + var sent interface{} + processor := rmBranchRollbackProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(_ int32, response interface{}) error { sent = response; return nil }, + } + err := processor.handleGettyBranchRollback(context.Background(), message.RpcMessage{ID: 1, Body: message.BranchRollbackRequest{ + AbstractBranchEndRequest: message.AbstractBranchEndRequest{Xid: "xid", BranchId: 7, BranchType: branch.BranchTypeTCC, ResourceId: "resource"}, + }}) + require.NoError(t, err) + got := sent.(message.BranchRollbackResponse) + require.Equal(t, message.ResultCodeFailed, got.ResultCode) + require.Equal(t, bizErr.Error(), got.Msg) + require.Equal(t, manager.rollbackStatus, got.BranchStatus) + require.Equal(t, "xid", got.Xid) + require.Equal(t, int64(7), got.BranchId) + }) + + t.Run("grpc", func(t *testing.T) { + var sent interface{} + processor := rmBranchRollbackProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(_ int32, response interface{}) error { sent = response; return nil }, + } + err := processor.handleGrpcBranchRollback(context.Background(), message.RpcMessage{ID: 1, Body: &pb.BranchRollbackRequestProto{ + AbstractBranchEndRequest: &pb.AbstractBranchEndRequestProto{Xid: "xid", BranchId: 7, BranchType: pb.BranchTypeProto_TCC, ResourceId: "resource"}, + }}) + require.NoError(t, err) + got := sent.(*pb.BranchRollbackResponseProto) + result := got.AbstractBranchEndResponse.AbstractTransactionResponse.AbstractResultMessage + require.Equal(t, pb.ResultCodeProto_Failed, result.ResultCode) + require.Equal(t, bizErr.Error(), result.Msg) + require.Equal(t, pb.BranchStatusProto(manager.rollbackStatus), got.AbstractBranchEndResponse.BranchStatus) + require.Equal(t, "xid", got.AbstractBranchEndResponse.Xid) + require.Equal(t, int64(7), got.AbstractBranchEndResponse.BranchId) + }) +} + +func TestRmBranchRollbackProcessor_ObservesBusinessAndSendErrors(t *testing.T) { + bizErr := errors.New("rollback failed") + sendErr := errors.New("send failed") + manager := &testResourceManager{rollbackStatus: branch.BranchStatusPhasetwoRollbackFailedRetryable, rollbackErr: bizErr} + + t.Run("getty", func(t *testing.T) { + processor := rmBranchRollbackProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(int32, interface{}) error { return sendErr }, + } + err := processor.handleGettyBranchRollback(context.Background(), message.RpcMessage{ID: 1, Body: message.BranchRollbackRequest{ + AbstractBranchEndRequest: message.AbstractBranchEndRequest{Xid: "xid", BranchId: 7, BranchType: branch.BranchTypeTCC, ResourceId: "resource"}, + }}) + require.ErrorIs(t, err, bizErr) + require.ErrorIs(t, err, sendErr) + }) + + t.Run("grpc", func(t *testing.T) { + processor := rmBranchRollbackProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(int32, interface{}) error { return sendErr }, + } + err := processor.handleGrpcBranchRollback(context.Background(), message.RpcMessage{ID: 1, Body: &pb.BranchRollbackRequestProto{ + AbstractBranchEndRequest: &pb.AbstractBranchEndRequestProto{Xid: "xid", BranchId: 7, BranchType: pb.BranchTypeProto_TCC, ResourceId: "resource"}, + }}) + require.ErrorIs(t, err, bizErr) + require.ErrorIs(t, err, sendErr) + }) +} + +func TestRmBranchRollbackProcessor_SendsSuccessResponse(t *testing.T) { + manager := &testResourceManager{rollbackStatus: branch.BranchStatusPhasetwoRollbacked} + + t.Run("getty", func(t *testing.T) { + var sent interface{} + processor := rmBranchRollbackProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(_ int32, response interface{}) error { sent = response; return nil }, + } + err := processor.handleGettyBranchRollback(context.Background(), message.RpcMessage{ID: 1, Body: message.BranchRollbackRequest{ + AbstractBranchEndRequest: message.AbstractBranchEndRequest{Xid: "xid", BranchId: 7, BranchType: branch.BranchTypeTCC, ResourceId: "resource"}, + }}) + require.NoError(t, err) + got := sent.(message.BranchRollbackResponse) + require.Equal(t, message.ResultCodeSuccess, got.ResultCode) + require.Empty(t, got.Msg) + require.Equal(t, manager.rollbackStatus, got.BranchStatus) + require.Equal(t, "xid", got.Xid) + require.Equal(t, int64(7), got.BranchId) + }) + + t.Run("grpc", func(t *testing.T) { + var sent interface{} + processor := rmBranchRollbackProcessor{ + getResourceManager: func(branch.BranchType) rm.ResourceManager { return manager }, + sendResponse: func(_ int32, response interface{}) error { sent = response; return nil }, + } + err := processor.handleGrpcBranchRollback(context.Background(), message.RpcMessage{ID: 1, Body: &pb.BranchRollbackRequestProto{ + AbstractBranchEndRequest: &pb.AbstractBranchEndRequestProto{Xid: "xid", BranchId: 7, BranchType: pb.BranchTypeProto_TCC, ResourceId: "resource"}, + }}) + require.NoError(t, err) + got := sent.(*pb.BranchRollbackResponseProto) + result := got.AbstractBranchEndResponse.AbstractTransactionResponse.AbstractResultMessage + require.Equal(t, pb.ResultCodeProto_Success, result.ResultCode) + require.Empty(t, result.Msg) + require.Equal(t, pb.BranchStatusProto(manager.rollbackStatus), got.AbstractBranchEndResponse.BranchStatus) + require.Equal(t, "xid", got.AbstractBranchEndResponse.Xid) + require.Equal(t, int64(7), got.AbstractBranchEndResponse.BranchId) + }) +} + +func TestRmBranchRollbackProcessor_ProcessRoutesByProtocol(t *testing.T) { + previous := config.GetTransportConfig() + t.Cleanup(func() { config.InitTransportConfig(previous) }) + + manager := &testResourceManager{rollbackStatus: branch.BranchStatusPhasetwoRollbacked} tests := []struct { - name string // testcase name - protocol string // protocol:seata/grpc - rpcMsg message.RpcMessage // rpcMessage case - wantErr bool // want testcase err or not + name string + protocol protocol.Protocol + body interface{} + wantType interface{} }{ { - name: "rbr-testcase1-failure", - protocol: "seata", - rpcMsg: message.RpcMessage{ - ID: 223, - Type: message.RequestType(message.MessageTypeBranchRollback), - Codec: byte(codec.CodecTypeSeata), - Compressor: byte(1), - HeadMap: map[string]string{ - "name": " Jack", - "age": "12", - "address": "Beijing", - }, - Body: message.BranchRollbackRequest{ - AbstractBranchEndRequest: message.AbstractBranchEndRequest{ - Xid: "123345", - BranchId: 56679, - BranchType: model2.BranchTypeTCC, - ResourceId: "1232324", - ApplicationData: []byte("TestExtraData"), - }, - }, - }, - - wantErr: true, // need dail to server, so err accured + name: "seata", + protocol: protocol.ProtocolSEATA, + body: message.BranchRollbackRequest{AbstractBranchEndRequest: message.AbstractBranchEndRequest{ + Xid: "xid", BranchId: 7, BranchType: branch.BranchTypeTCC, ResourceId: "resource", + }}, + wantType: message.BranchRollbackResponse{}, }, { - name: "rbr-testcase2-failure", - protocol: "grpc", - rpcMsg: message.RpcMessage{ - ID: 223, - Type: message.RequestType(message.MessageTypeBranchRollback), - HeadMap: map[string]string{ - "name": " Jack", - "age": "12", - "address": "Beijing", - }, - Body: &pb.BranchRollbackRequestProto{ - AbstractBranchEndRequest: &pb.AbstractBranchEndRequestProto{ - Xid: "123345", - BranchId: 56679, - BranchType: pb.BranchTypeProto_TCC, - ResourceId: "1232324", - ApplicationData: "TestExtraData", - }, - }, - }, - - wantErr: true, // need dail to server, so err accured + name: "grpc", + protocol: protocol.ProtocolGRPC, + body: &pb.BranchRollbackRequestProto{AbstractBranchEndRequest: &pb.AbstractBranchEndRequestProto{ + Xid: "xid", BranchId: 7, BranchType: pb.BranchTypeProto_TCC, ResourceId: "resource", + }}, + wantType: (*pb.BranchRollbackResponseProto)(nil), }, } - var ctx context.Context - var rbrProcessor rmBranchRollbackProcessor - - // run tests - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - config.InitTransportConfig(&config.TransportConfig{Protocol: tc.protocol}) - - rm.GetRmCacheInstance().RegisterResourceManager(tcc.GetTCCResourceManagerInstance()) - - err := rbrProcessor.Process(ctx, tc.rpcMsg) - if (err != nil) != tc.wantErr { - t.Errorf("rmBranchRollbackProcessor wantErr: %v, got: %v", tc.wantErr, err) - return + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + config.InitTransportConfig(&config.TransportConfig{Protocol: tt.protocol.String()}) Review Comment: `protocol.Protocol` may not implement a `String()` method (e.g., if it is a `type Protocol string`), which would cause this test to fail to compile. Consider passing the protocol value directly (if it is already a string-like type) or using an explicit conversion such as `string(tt.protocol)` instead of calling `.String()`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
