|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package io.seata; |
| 18 | + |
| 19 | +import io.seata.core.rpc.netty.Action1Impl; |
| 20 | +import io.seata.core.rpc.netty.ProtocolTestConstants; |
| 21 | +import io.seata.core.rpc.netty.RmClientTest; |
| 22 | +import io.seata.core.rpc.netty.RmRpcClient; |
| 23 | +import io.seata.core.rpc.netty.TmClientTest; |
| 24 | +import io.seata.core.rpc.netty.TmRpcClient; |
| 25 | +import io.seata.rm.DefaultResourceManager; |
| 26 | +import io.seata.core.exception.TransactionException; |
| 27 | +import io.seata.core.model.BranchType; |
| 28 | +import io.seata.core.model.GlobalStatus; |
| 29 | +import io.seata.core.model.TransactionManager; |
| 30 | +import org.apache.seata.mockserver.MockCoordinator; |
| 31 | +import org.apache.seata.mockserver.MockServer; |
| 32 | +import org.junit.jupiter.api.AfterAll; |
| 33 | +import org.junit.jupiter.api.Assertions; |
| 34 | +import org.junit.jupiter.api.BeforeAll; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.slf4j.Logger; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | + |
| 39 | +/** |
| 40 | + * the type MockServerTest |
| 41 | + */ |
| 42 | +public class MockTest { |
| 43 | + |
| 44 | + static String RESOURCE_ID = "mock-action-061"; |
| 45 | + Logger logger = LoggerFactory.getLogger(MockTest.class); |
| 46 | + |
| 47 | + @BeforeAll |
| 48 | + public static void before() { |
| 49 | + System.setProperty("server.servicePort", ProtocolTestConstants.MOCK_SERVER_PORT+""); |
| 50 | + MockServer.start(ProtocolTestConstants.MOCK_SERVER_PORT); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterAll |
| 54 | + public static void after() { |
| 55 | + MockServer.close(); |
| 56 | + TmRpcClient.getInstance().destroy(); |
| 57 | + RmRpcClient.getInstance().destroy(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testCommit() throws Exception { |
| 62 | + String xid = doTestCommit(0); |
| 63 | + Assertions.assertEquals(1, Action1Impl.getCommitTimes(xid)); |
| 64 | + Assertions.assertEquals(0, Action1Impl.getRollbackTimes(xid)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testCommitRetry() throws Exception { |
| 69 | + String xid = doTestCommit(2); |
| 70 | + Assertions.assertEquals(3, Action1Impl.getCommitTimes(xid)); |
| 71 | + Assertions.assertEquals(0, Action1Impl.getRollbackTimes(xid)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testRollback() throws Exception { |
| 76 | + String xid = doTestRollback(0); |
| 77 | + Assertions.assertEquals(0, Action1Impl.getCommitTimes(xid)); |
| 78 | + Assertions.assertEquals(1, Action1Impl.getRollbackTimes(xid)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testRollbackRetry() throws Exception { |
| 83 | + String xid = doTestRollback(2); |
| 84 | + Assertions.assertEquals(0, Action1Impl.getCommitTimes(xid)); |
| 85 | + Assertions.assertEquals(3, Action1Impl.getRollbackTimes(xid)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testTm() throws Exception { |
| 90 | + TmClientTest.testTm(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testRm() throws Exception { |
| 95 | + RmClientTest.testRm("testRM01"); |
| 96 | + } |
| 97 | + |
| 98 | + private String doTestCommit(int times) throws TransactionException, NoSuchMethodException { |
| 99 | + TransactionManager tm = TmClientTest.getTm(); |
| 100 | + DefaultResourceManager rm = RmClientTest.getRm(RESOURCE_ID); |
| 101 | + |
| 102 | + String xid = tm.begin(ProtocolTestConstants.APPLICATION_ID, ProtocolTestConstants.SERVICE_GROUP, "test-commit", 60000); |
| 103 | + logger.info("doTestCommit(0.6.1) xid:{}", xid); |
| 104 | + MockCoordinator.getInstance().setExpectedRetry(xid, times); |
| 105 | + Long branchId = rm.branchRegister(BranchType.TCC, RESOURCE_ID, "1", xid, "{\"mock\":\"mock\"}", "1"); |
| 106 | + logger.info("branch register(0.6.1) ok, branchId=" + branchId); |
| 107 | + GlobalStatus commit = tm.commit(xid); |
| 108 | + Assertions.assertEquals(GlobalStatus.Committed, commit); |
| 109 | + return xid; |
| 110 | + } |
| 111 | + |
| 112 | + private String doTestRollback(int times) throws TransactionException, NoSuchMethodException { |
| 113 | + TransactionManager tm = TmClientTest.getTm(); |
| 114 | + DefaultResourceManager rm = RmClientTest.getRm(RESOURCE_ID); |
| 115 | + |
| 116 | + String xid = tm.begin(ProtocolTestConstants.APPLICATION_ID, ProtocolTestConstants.SERVICE_GROUP, "test-rollback", 60000); |
| 117 | + logger.info("doTestRollback(0.6.1) xid:{}", xid); |
| 118 | + MockCoordinator.getInstance().setExpectedRetry(xid, times); |
| 119 | + Long branchId = rm.branchRegister(BranchType.TCC, RESOURCE_ID, "1", xid, "{\"mock\":\"mock\"}", "1"); |
| 120 | + logger.info("branch register(0.6.1) ok, branchId=" + branchId); |
| 121 | + GlobalStatus rollback = tm.rollback(xid); |
| 122 | + Assertions.assertEquals(GlobalStatus.Rollbacked, rollback); |
| 123 | + return xid; |
| 124 | + } |
| 125 | +} |
0 commit comments