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

test : Complete test cases for the (current-version) protocol by MessageType #6207

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 1999-2019 Seata.io Group.
Bughue marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed 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 io.seata.core.rpc.netty;

import io.netty.channel.Channel;
import io.seata.core.rpc.netty.current.ProtocolTestConstants;

import java.util.concurrent.ConcurrentMap;

/**
* Channel Manager Test Helper
*
* @author minghua.xie
* @date 2023/12/25
funky-eyes marked this conversation as resolved.
Show resolved Hide resolved
**/
public class ChannelManagerTestHelper {
public static ConcurrentMap<String, Channel> getChannelConcurrentMap(AbstractNettyRemotingClient remotingClient) {
return getChannelManager(remotingClient).getChannels();
}

public static Channel getChannel(TmNettyRemotingClient client) {
return getChannelManager(client)
.acquireChannel(ProtocolTestConstants.SERVER_ADDRESS);
}
private static NettyClientChannelManager getChannelManager(AbstractNettyRemotingClient remotingClient) {
return remotingClient.getClientChannelManager();
}
}
40 changes: 40 additions & 0 deletions test/src/test/java/io/seata/core/rpc/netty/current/Action1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.rpc.netty.current;

import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;

import java.util.Map;

@LocalTCC
public interface Action1 {

@TwoPhaseBusinessAction(name = "mock-action", commitMethod = "commitTcc", rollbackMethod = "cancel"
// , useTCCFence = true
)
String insert(@BusinessActionContextParameter Long reqId,
@BusinessActionContextParameter(paramName = "params") Map<String, String> params
);


boolean commitTcc(BusinessActionContext actionContext);


boolean cancel(BusinessActionContext actionContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.rpc.netty.current;


import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
public class Action1Impl implements Action1 {

@Override
public String insert(Long reqId,Map<String, String> params) {
System.out.println("prepare");
return "prepare";
}


@Override
public boolean commitTcc(BusinessActionContext actionContext) {
System.out.println("commitTcc");
return true;
}

@Override
public boolean cancel(BusinessActionContext actionContext) {
System.out.println("cancel");
return true;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.rpc.netty.current;

/**
* Mock Constants
*
* @author minghua.xie
* @date 2023/11/21
**/
public class ProtocolTestConstants {
public static final String APPLICATION_ID = "my_app_test";
public static final String SERVICE_GROUP = "default_tx_group";
public static final String SERVER_ADDRESS = "0.0.0.0:8091";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.rpc.netty.current;

import io.netty.channel.Channel;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.core.model.BranchStatus;
import io.seata.core.model.BranchType;
import io.seata.core.protocol.HeartbeatMessage;
import io.seata.core.rpc.netty.RmNettyRemotingClient;
import io.seata.integration.tx.api.interceptor.parser.DefaultResourceRegisterParser;
import io.seata.rm.DefaultResourceManager;
import io.seata.rm.RMClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentMap;

import static io.seata.core.rpc.netty.ChannelManagerTestHelper.getChannelConcurrentMap;

/**
* rm client test
*
* @author minghua.xie
* @date 2023/11/21
**/
public class RmClientTest {
protected static final Logger LOGGER = LoggerFactory.getLogger(RmClientTest.class);
public static void main(String[] args) throws TransactionException {
RMClient.init(ProtocolTestConstants.APPLICATION_ID, ProtocolTestConstants.SERVICE_GROUP);
DefaultResourceManager rm = DefaultResourceManager.get();

//register:TYPE_REG_RM = 103 , TYPE_REG_RM_RESULT = 104
String rid = "mock-action";
Action1 target = new Action1Impl();
DefaultResourceRegisterParser.get().registerResource(target, rid);
LOGGER.info("registerResource ok");

//branchRegister:TYPE_BRANCH_REGISTER = 11 , TYPE_BRANCH_REGISTER_RESULT = 12
Long branchId = rm.branchRegister(BranchType.AT, rid, "1", "1", "1", "1");
LOGGER.info("branchRegister ok, branchId=" + branchId);


// branchReport:TYPE_BRANCH_STATUS_REPORT = 13 , TYPE_BRANCH_STATUS_REPORT_RESULT = 14
// TYPE_SEATA_MERGE = 59 , TYPE_SEATA_MERGE_RESULT = 60
rm.branchReport(BranchType.AT, "1", branchId, BranchStatus.PhaseTwo_Committed, "");
LOGGER.info("branchReport ok");

//lockQuery:TYPE_GLOBAL_LOCK_QUERY = 21 , TYPE_GLOBAL_LOCK_QUERY_RESULT = 22
RootContext.bind("1");
boolean b = rm.lockQuery(BranchType.AT, rid, "1", "1");
LOGGER.info("lockQuery ok, result=" + b);

RmNettyRemotingClient remotingClient = RmNettyRemotingClient.getInstance();
ConcurrentMap<String, Channel> channels = getChannelConcurrentMap(remotingClient);
channels.forEach(
(key, value) -> RmNettyRemotingClient.getInstance().sendAsyncRequest(value, HeartbeatMessage.PING));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.rpc.netty.current;


import io.netty.channel.Channel;
import io.seata.core.model.GlobalStatus;
import io.seata.core.model.TransactionManager;
import io.seata.core.rpc.netty.TmNettyRemotingClient;
import io.seata.tm.DefaultTransactionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.seata.core.rpc.netty.ChannelManagerTestHelper.getChannel;

/**
* TmClient Test
*
* @author minghua.xie
* @date 2023/11/10
**/
public class TmClientTest {

protected static final Logger LOGGER = LoggerFactory.getLogger(TmClientTest.class);
public static void main(String[] args) throws Exception {

TmNettyRemotingClient tmNettyRemotingClient = TmNettyRemotingClient.getInstance(
ProtocolTestConstants.APPLICATION_ID, ProtocolTestConstants.SERVICE_GROUP);
tmNettyRemotingClient.init();
TransactionManager tm = new DefaultTransactionManager();

//register:TYPE_REG_CLT = 101 , TYPE_REG_CLT_RESULT = 102
TmNettyRemotingClient client = TmNettyRemotingClient.getInstance();
Channel channel = getChannel(client);
LOGGER.info("TM register ok:channel=" + channel);

//globalBegin:TYPE_GLOBAL_BEGIN = 1 , TYPE_GLOBAL_BEGIN_RESULT = 2
String xid = tm.begin(ProtocolTestConstants.APPLICATION_ID,
ProtocolTestConstants.SERVICE_GROUP, "test", 60000);
LOGGER.info("globalBegin ok:xid=" + xid);

// if (xid == null) {
// xid = "6";
// }

//globalCommit:TYPE_GLOBAL_COMMIT = 7 , TYPE_GLOBAL_COMMIT_RESULT = 8
GlobalStatus commit = tm.commit(xid);
LOGGER.info("globalCommit ok:" + commit);

//globalRollback:TYPE_GLOBAL_ROLLBACK = 9 , TYPE_GLOBAL_ROLLBACK_RESULT = 10
GlobalStatus rollback = tm.rollback(xid);
LOGGER.info("globalRollback ok:" + rollback);


//getStatus:TYPE_GLOBAL_STATUS = 15 , TYPE_GLOBAL_STATUS_RESULT = 16
GlobalStatus status = tm.getStatus(xid);
LOGGER.info("getStatus ok:" + status);

//globalReport:TYPE_GLOBAL_REPORT = 17 , TYPE_GLOBAL_REPORT_RESULT = 18
GlobalStatus globalReport = tm.globalReport(xid, GlobalStatus.Committed);
LOGGER.info("globalReport ok:" + globalReport);

}



}
Loading