Skip to content

Commit

Permalink
optimize: fix 2.3.0 UT and update NOTICE (#7172)
Browse files Browse the repository at this point in the history
  • Loading branch information
wt-better authored Feb 20, 2025
1 parent d2f3554 commit 35291d2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import io.netty.util.HashedWheelTimer;
import io.seata.core.context.RootContext;
import io.seata.tm.api.transaction.MyRuntimeException;
import org.apache.seata.common.util.ReflectionUtil;
import org.apache.seata.core.exception.TransactionException;
import org.apache.seata.core.model.GlobalStatus;
import org.apache.seata.core.model.TransactionManager;
import org.apache.seata.tm.TransactionManagerHolder;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -37,10 +39,8 @@ class DefaultFailureHandlerImplTest {
private static final String DEFAULT_XID = "1234567890";
private static GlobalStatus globalStatus = GlobalStatus.Begin;

@BeforeAll
public static void init() {

TransactionManagerHolder.set(new TransactionManager() {
private TransactionManager getTransactionManager() {
return new TransactionManager() {
@Override
public String begin(String applicationId, String transactionServiceGroup, String name, int timeout)
throws TransactionException {
Expand All @@ -66,22 +66,26 @@ public GlobalStatus getStatus(String xid) throws TransactionException {
public GlobalStatus globalReport(String xid, GlobalStatus globalStatus) throws TransactionException {
return globalStatus;
}
});
};
}

@Test
void onBeginFailure() {
void onBeginFailure() throws Exception {
RootContext.bind(DEFAULT_XID);
DefaultGlobalTransaction tx = (DefaultGlobalTransaction)GlobalTransactionContext.getCurrentOrCreate();
DefaultGlobalTransaction tx = (DefaultGlobalTransaction) GlobalTransactionContext.getCurrentOrCreate();
ReflectionUtil.setFieldValue(tx.getInstance(), "transactionManager", getTransactionManager());

FailureHandler failureHandler = new DefaultFailureHandlerImpl();
failureHandler.onBeginFailure(tx, new MyRuntimeException("").getCause());
}

@Test
void onCommitFailure() throws Exception{
void onCommitFailure() throws Exception {

RootContext.bind(DEFAULT_XID);
DefaultGlobalTransaction tx = (DefaultGlobalTransaction)GlobalTransactionContext.getCurrentOrCreate();
DefaultGlobalTransaction tx = (DefaultGlobalTransaction) GlobalTransactionContext.getCurrentOrCreate();
ReflectionUtil.setFieldValue(tx.getInstance(), "transactionManager", getTransactionManager());

FailureHandler failureHandler = new DefaultFailureHandlerImpl();
failureHandler.onCommitFailure(tx, new MyRuntimeException("").getCause());

Expand All @@ -92,22 +96,22 @@ void onCommitFailure() throws Exception{
HashedWheelTimer timer = (HashedWheelTimer) field.get(failureHandler);
// assert timer pendingCount: first time is 1
Long pendingTimeout = timer.pendingTimeouts();
Assertions.assertEquals(pendingTimeout,1L);
Assertions.assertEquals(pendingTimeout, 1L);
//set globalStatus
globalStatus= GlobalStatus.Committed;
Thread.sleep(25*1000L);
globalStatus = GlobalStatus.Committed;
Thread.sleep(25 * 1000L);
pendingTimeout = timer.pendingTimeouts();
LOGGER.info("pendingTimeout {}" ,pendingTimeout);
LOGGER.info("pendingTimeout {}", pendingTimeout);
//all timer is done
Assertions.assertEquals(pendingTimeout,0L);
Assertions.assertEquals(pendingTimeout, 0L);
}

@Test
void onRollbackFailure() throws Exception {


RootContext.bind(DEFAULT_XID);
DefaultGlobalTransaction tx = (DefaultGlobalTransaction)GlobalTransactionContext.getCurrentOrCreate();
DefaultGlobalTransaction tx = (DefaultGlobalTransaction) GlobalTransactionContext.getCurrentOrCreate();
ReflectionUtil.setFieldValue(tx.getInstance(), "transactionManager", getTransactionManager());

FailureHandler failureHandler = new DefaultFailureHandlerImpl();

failureHandler.onRollbackFailure(tx, new MyRuntimeException("").getCause());
Expand All @@ -119,14 +123,14 @@ void onRollbackFailure() throws Exception {
HashedWheelTimer timer = (HashedWheelTimer) field.get(failureHandler);
// assert timer pendingCount: first time is 1
Long pendingTimeout = timer.pendingTimeouts();
Assertions.assertEquals(pendingTimeout,1L);
Assertions.assertEquals(pendingTimeout, 1L);
//set globalStatus
globalStatus= GlobalStatus.Rollbacked;
Thread.sleep(25*1000L);
globalStatus = GlobalStatus.Rollbacked;
Thread.sleep(25 * 1000L);
pendingTimeout = timer.pendingTimeouts();
LOGGER.info("pendingTimeout {}" ,pendingTimeout);
LOGGER.info("pendingTimeout {}", pendingTimeout);
//all timer is done
Assertions.assertEquals(pendingTimeout,0L);
Assertions.assertEquals(pendingTimeout, 0L);


}
Expand Down
4 changes: 2 additions & 2 deletions distribution/NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ Please copy database driver dependencies, such as `mysql-connector-java.jar`, to
│   ├── tomcat-embed-core-9.0.98.jar
│   ├── tomcat-embed-el-9.0.98.jar
│   ├── tomcat-embed-websocket-9.0.98.jar
│   ├── xstream-1.4.20.jar
│   ├── xstream-1.4.21.jar
│   ├── zookeeper-3.7.2.jar
│   ├── zookeeper-jute-3.7.2.jar
│   └── zstd-jni-1.5.0-4.jar
Expand Down Expand Up @@ -778,7 +778,7 @@ Please copy database driver dependencies, such as `mysql-connector-java.jar`, to
│   ├── tomcat-embed-core-9.0.98.jar
│   ├── tomcat-embed-el-9.0.98.jar
│   ├── tomcat-embed-websocket-9.0.98.jar
│   ├── xstream-1.4.20.jar
│   ├── xstream-1.4.21.jar
│   ├── zookeeper-3.7.2.jar
│   ├── zookeeper-jute-3.7.2.jar
│   └── zstd-jni-1.5.0-4.jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@


import io.netty.util.HashedWheelTimer;
import org.apache.seata.common.util.ReflectionUtil;
import org.apache.seata.core.context.RootContext;
import org.apache.seata.core.exception.TransactionException;
import org.apache.seata.core.model.GlobalStatus;
import org.apache.seata.core.model.TransactionManager;
import org.apache.seata.tm.TransactionManagerHolder;
import org.apache.seata.tm.api.transaction.MyRuntimeException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,10 +38,8 @@ class DefaultFailureHandlerImplTest {
private static final String DEFAULT_XID = "1234567890";
private static GlobalStatus globalStatus = GlobalStatus.Begin;

@BeforeAll
public static void init() {

TransactionManagerHolder.set(new TransactionManager() {
private TransactionManager getTransactionManager() {
return new TransactionManager() {
@Override
public String begin(String applicationId, String transactionServiceGroup, String name, int timeout)
throws TransactionException {
Expand All @@ -68,22 +65,27 @@ public GlobalStatus getStatus(String xid) throws TransactionException {
public GlobalStatus globalReport(String xid, GlobalStatus globalStatus) throws TransactionException {
return globalStatus;
}
});
};
}

@Test
void onBeginFailure() {
void onBeginFailure() throws Exception {
RootContext.bind(DEFAULT_XID);
GlobalTransaction tx = GlobalTransactionContext.getCurrentOrCreate();
ReflectionUtil.setFieldValue(tx, "transactionManager", getTransactionManager());

FailureHandler failureHandler = new DefaultFailureHandlerImpl();
failureHandler.onBeginFailure(tx, new MyRuntimeException("").getCause());
}

@Test
void onCommitFailure() throws Exception{
void onCommitFailure() throws Exception {

RootContext.bind(DEFAULT_XID);
GlobalTransaction tx = GlobalTransactionContext.getCurrentOrCreate();
//TransactionManagerHolder.set has interaction, using globalTransaction instance level tm
ReflectionUtil.setFieldValue(tx, "transactionManager", getTransactionManager());

FailureHandler failureHandler = new DefaultFailureHandlerImpl();
failureHandler.onCommitFailure(tx, new MyRuntimeException("").getCause());

Expand All @@ -94,14 +96,14 @@ void onCommitFailure() throws Exception{
HashedWheelTimer timer = (HashedWheelTimer) field.get(failureHandler);
// assert timer pendingCount: first time is 1
Long pendingTimeout = timer.pendingTimeouts();
Assertions.assertEquals(pendingTimeout,1L);
Assertions.assertEquals(pendingTimeout, 1L);
//set globalStatus
globalStatus= GlobalStatus.Committed;
Thread.sleep(25*1000L);
globalStatus = GlobalStatus.Committed;
Thread.sleep(25 * 1000L);
pendingTimeout = timer.pendingTimeouts();
LOGGER.info("pendingTimeout {}" ,pendingTimeout);
LOGGER.info("pendingTimeout {}", pendingTimeout);
//all timer is done
Assertions.assertEquals(pendingTimeout,0L);
Assertions.assertEquals(pendingTimeout, 0L);
}

@Test
Expand All @@ -110,6 +112,8 @@ void onRollbackFailure() throws Exception {

RootContext.bind(DEFAULT_XID);
GlobalTransaction tx = GlobalTransactionContext.getCurrentOrCreate();
ReflectionUtil.setFieldValue(tx, "transactionManager", getTransactionManager());

FailureHandler failureHandler = new DefaultFailureHandlerImpl();
failureHandler.onRollbackFailure(tx, new MyRuntimeException("").getCause());

Expand All @@ -120,14 +124,14 @@ void onRollbackFailure() throws Exception {
HashedWheelTimer timer = (HashedWheelTimer) field.get(failureHandler);
// assert timer pendingCount: first time is 1
Long pendingTimeout = timer.pendingTimeouts();
Assertions.assertEquals(pendingTimeout,1L);
Assertions.assertEquals(pendingTimeout, 1L);
//set globalStatus
globalStatus= GlobalStatus.Rollbacked;
Thread.sleep(25*1000L);
globalStatus = GlobalStatus.Rollbacked;
Thread.sleep(25 * 1000L);
pendingTimeout = timer.pendingTimeouts();
LOGGER.info("pendingTimeout {}" ,pendingTimeout);
LOGGER.info("pendingTimeout {}", pendingTimeout);
//all timer is done
Assertions.assertEquals(pendingTimeout,0L);
Assertions.assertEquals(pendingTimeout, 0L);


}
Expand Down

0 comments on commit 35291d2

Please sign in to comment.