|
16 | 16 | */
|
17 | 17 | package io.seata.tm.api;
|
18 | 18 |
|
19 |
| -import java.lang.reflect.Field; |
20 |
| - |
21 | 19 | import io.netty.util.HashedWheelTimer;
|
22 | 20 | import io.seata.core.context.RootContext;
|
23 | 21 | import io.seata.tm.api.transaction.MyRuntimeException;
|
24 | 22 | import org.apache.seata.common.util.ReflectionUtil;
|
25 | 23 | import org.apache.seata.core.exception.TransactionException;
|
26 | 24 | import org.apache.seata.core.model.GlobalStatus;
|
27 | 25 | import org.apache.seata.core.model.TransactionManager;
|
28 |
| -import org.apache.seata.tm.TransactionManagerHolder; |
29 |
| -import org.jetbrains.annotations.NotNull; |
30 | 26 | import org.junit.jupiter.api.Assertions;
|
31 |
| -import org.junit.jupiter.api.BeforeAll; |
32 | 27 | import org.junit.jupiter.api.Test;
|
33 | 28 | import org.slf4j.Logger;
|
34 | 29 | import org.slf4j.LoggerFactory;
|
35 | 30 |
|
| 31 | +import java.lang.reflect.Field; |
| 32 | + |
36 | 33 | class DefaultFailureHandlerImplTest {
|
37 | 34 | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFailureHandlerImplTest.class);
|
38 | 35 |
|
@@ -71,67 +68,78 @@ public GlobalStatus globalReport(String xid, GlobalStatus globalStatus) throws T
|
71 | 68 |
|
72 | 69 | @Test
|
73 | 70 | void onBeginFailure() throws Exception {
|
74 |
| - RootContext.bind(DEFAULT_XID); |
75 |
| - DefaultGlobalTransaction tx = (DefaultGlobalTransaction) GlobalTransactionContext.getCurrentOrCreate(); |
76 |
| - ReflectionUtil.setFieldValue(tx.getInstance(), "transactionManager", getTransactionManager()); |
77 |
| - |
78 |
| - FailureHandler failureHandler = new DefaultFailureHandlerImpl(); |
79 |
| - failureHandler.onBeginFailure(tx, new MyRuntimeException("").getCause()); |
| 71 | + try { |
| 72 | + RootContext.bind(DEFAULT_XID); |
| 73 | + DefaultGlobalTransaction tx = (DefaultGlobalTransaction) GlobalTransactionContext.getCurrentOrCreate(); |
| 74 | + ReflectionUtil.setFieldValue(tx.getInstance(), "transactionManager", getTransactionManager()); |
| 75 | + |
| 76 | + FailureHandler failureHandler = new DefaultFailureHandlerImpl(); |
| 77 | + failureHandler.onBeginFailure(tx, new MyRuntimeException("").getCause()); |
| 78 | + } finally { |
| 79 | + RootContext.unbind(); |
| 80 | + } |
80 | 81 | }
|
81 | 82 |
|
82 | 83 | @Test
|
83 | 84 | void onCommitFailure() throws Exception {
|
84 | 85 |
|
85 |
| - RootContext.bind(DEFAULT_XID); |
86 |
| - DefaultGlobalTransaction tx = (DefaultGlobalTransaction) GlobalTransactionContext.getCurrentOrCreate(); |
87 |
| - ReflectionUtil.setFieldValue(tx.getInstance(), "transactionManager", getTransactionManager()); |
88 |
| - |
89 |
| - FailureHandler failureHandler = new DefaultFailureHandlerImpl(); |
90 |
| - failureHandler.onCommitFailure(tx, new MyRuntimeException("").getCause()); |
91 |
| - |
92 |
| - // get timer |
93 |
| - Class<?> c = Class.forName("io.seata.tm.api.DefaultFailureHandlerImpl"); |
94 |
| - Field field = c.getDeclaredField("TIMER"); |
95 |
| - field.setAccessible(true); |
96 |
| - HashedWheelTimer timer = (HashedWheelTimer) field.get(failureHandler); |
97 |
| - // assert timer pendingCount: first time is 1 |
98 |
| - Long pendingTimeout = timer.pendingTimeouts(); |
99 |
| - Assertions.assertEquals(pendingTimeout, 1L); |
100 |
| - //set globalStatus |
101 |
| - globalStatus = GlobalStatus.Committed; |
102 |
| - Thread.sleep(25 * 1000L); |
103 |
| - pendingTimeout = timer.pendingTimeouts(); |
104 |
| - LOGGER.info("pendingTimeout {}", pendingTimeout); |
105 |
| - //all timer is done |
106 |
| - Assertions.assertEquals(pendingTimeout, 0L); |
| 86 | + try { |
| 87 | + RootContext.bind(DEFAULT_XID); |
| 88 | + DefaultGlobalTransaction tx = (DefaultGlobalTransaction) GlobalTransactionContext.getCurrentOrCreate(); |
| 89 | + ReflectionUtil.setFieldValue(tx.getInstance(), "transactionManager", getTransactionManager()); |
| 90 | + |
| 91 | + FailureHandler failureHandler = new DefaultFailureHandlerImpl(); |
| 92 | + failureHandler.onCommitFailure(tx, new MyRuntimeException("").getCause()); |
| 93 | + |
| 94 | + // get timer |
| 95 | + Class<?> c = Class.forName("io.seata.tm.api.DefaultFailureHandlerImpl"); |
| 96 | + Field field = c.getDeclaredField("TIMER"); |
| 97 | + field.setAccessible(true); |
| 98 | + HashedWheelTimer timer = (HashedWheelTimer) field.get(failureHandler); |
| 99 | + // assert timer pendingCount: first time is 1 |
| 100 | + Long pendingTimeout = timer.pendingTimeouts(); |
| 101 | + Assertions.assertEquals(pendingTimeout, 1L); |
| 102 | + //set globalStatus |
| 103 | + globalStatus = GlobalStatus.Committed; |
| 104 | + Thread.sleep(25 * 1000L); |
| 105 | + pendingTimeout = timer.pendingTimeouts(); |
| 106 | + LOGGER.info("pendingTimeout {}", pendingTimeout); |
| 107 | + //all timer is done |
| 108 | + Assertions.assertEquals(pendingTimeout, 0L); |
| 109 | + } finally { |
| 110 | + RootContext.unbind(); |
| 111 | + } |
107 | 112 | }
|
108 | 113 |
|
109 | 114 | @Test
|
110 | 115 | void onRollbackFailure() throws Exception {
|
111 |
| - RootContext.bind(DEFAULT_XID); |
112 |
| - DefaultGlobalTransaction tx = (DefaultGlobalTransaction) GlobalTransactionContext.getCurrentOrCreate(); |
113 |
| - ReflectionUtil.setFieldValue(tx.getInstance(), "transactionManager", getTransactionManager()); |
114 |
| - |
115 |
| - FailureHandler failureHandler = new DefaultFailureHandlerImpl(); |
116 |
| - |
117 |
| - failureHandler.onRollbackFailure(tx, new MyRuntimeException("").getCause()); |
118 |
| - |
119 |
| - // get timer |
120 |
| - Class<?> c = Class.forName("io.seata.tm.api.DefaultFailureHandlerImpl"); |
121 |
| - Field field = c.getDeclaredField("TIMER"); |
122 |
| - field.setAccessible(true); |
123 |
| - HashedWheelTimer timer = (HashedWheelTimer) field.get(failureHandler); |
124 |
| - // assert timer pendingCount: first time is 1 |
125 |
| - Long pendingTimeout = timer.pendingTimeouts(); |
126 |
| - Assertions.assertEquals(pendingTimeout, 1L); |
127 |
| - //set globalStatus |
128 |
| - globalStatus = GlobalStatus.Rollbacked; |
129 |
| - Thread.sleep(25 * 1000L); |
130 |
| - pendingTimeout = timer.pendingTimeouts(); |
131 |
| - LOGGER.info("pendingTimeout {}", pendingTimeout); |
132 |
| - //all timer is done |
133 |
| - Assertions.assertEquals(pendingTimeout, 0L); |
134 |
| - |
| 116 | + try { |
| 117 | + RootContext.bind(DEFAULT_XID); |
| 118 | + DefaultGlobalTransaction tx = (DefaultGlobalTransaction) GlobalTransactionContext.getCurrentOrCreate(); |
| 119 | + ReflectionUtil.setFieldValue(tx.getInstance(), "transactionManager", getTransactionManager()); |
| 120 | + |
| 121 | + FailureHandler failureHandler = new DefaultFailureHandlerImpl(); |
| 122 | + |
| 123 | + failureHandler.onRollbackFailure(tx, new MyRuntimeException("").getCause()); |
| 124 | + |
| 125 | + // get timer |
| 126 | + Class<?> c = Class.forName("io.seata.tm.api.DefaultFailureHandlerImpl"); |
| 127 | + Field field = c.getDeclaredField("TIMER"); |
| 128 | + field.setAccessible(true); |
| 129 | + HashedWheelTimer timer = (HashedWheelTimer) field.get(failureHandler); |
| 130 | + // assert timer pendingCount: first time is 1 |
| 131 | + Long pendingTimeout = timer.pendingTimeouts(); |
| 132 | + Assertions.assertEquals(pendingTimeout, 1L); |
| 133 | + //set globalStatus |
| 134 | + globalStatus = GlobalStatus.Rollbacked; |
| 135 | + Thread.sleep(25 * 1000L); |
| 136 | + pendingTimeout = timer.pendingTimeouts(); |
| 137 | + LOGGER.info("pendingTimeout {}", pendingTimeout); |
| 138 | + //all timer is done |
| 139 | + Assertions.assertEquals(pendingTimeout, 0L); |
| 140 | + } finally { |
| 141 | + RootContext.unbind(); |
| 142 | + } |
135 | 143 |
|
136 | 144 | }
|
137 | 145 |
|
|
0 commit comments