forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add TCC three-phase hooks (apache#6731)
- Loading branch information
chengliefeng
committed
Aug 29, 2024
1 parent
59e82ce
commit 381c3c3
Showing
1 changed file
with
138 additions
and
0 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
...tion-tx-api/src/test/java/org/apache/seata/integration/tx/api/fence/hook/TccHookTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package org.apache.seata.integration.tx.api.fence.hook; | ||
|
||
|
||
import org.apache.seata.common.exception.ShouldNeverHappenException; | ||
import org.apache.seata.rm.tcc.api.BusinessActionContext; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class TccHookTest { | ||
private TccHook tccHook; | ||
private String xid; | ||
private Long branchId; | ||
private String actionName; | ||
private BusinessActionContext context; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
tccHook = mock(TccHook.class); | ||
xid = "test-xid"; | ||
branchId = 12345L; | ||
actionName = "testAction"; | ||
context = new BusinessActionContext(); | ||
TccHookManager.clear(); | ||
TccHookManager.registerHook(tccHook); | ||
} | ||
|
||
@Test | ||
public void testBeforeTccPrepare() { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.beforeTccPrepare(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).beforeTccPrepare(xid, branchId, actionName, context); | ||
} | ||
|
||
@Test | ||
public void testAfterTccPrepare() { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.afterTccPrepare(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).afterTccPrepare(xid, branchId, actionName, context); | ||
} | ||
|
||
@Test | ||
public void testBeforeTccCommit() { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.beforeTccCommit(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).beforeTccCommit(xid, branchId, actionName, context); | ||
} | ||
|
||
@Test | ||
public void testAfterTccCommit() { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.afterTccCommit(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).afterTccCommit(xid, branchId, actionName, context); | ||
} | ||
|
||
@Test | ||
public void testBeforeTccRollback() { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.beforeTccRollback(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).beforeTccRollback(xid, branchId, actionName, context); | ||
} | ||
|
||
@Test | ||
public void testAfterTccRollback() { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.afterTccRollback(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).afterTccRollback(xid, branchId, actionName, context); | ||
} | ||
|
||
@Test | ||
public void testTccPrepareFinally() { | ||
Assertions.assertThrowsExactly(TccHookTestException.class, () -> { | ||
try { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.beforeTccPrepare(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).beforeTccPrepare(xid, branchId, actionName, context); | ||
throw new TccHookTestException("tcc hook test exception!"); | ||
} finally { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.afterTccPrepare(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).afterTccPrepare(xid, branchId, actionName, context); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void testTccCommitFinally() { | ||
Assertions.assertThrowsExactly(TccHookTestException.class, () -> { | ||
try { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.beforeTccCommit(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).beforeTccCommit(xid, branchId, actionName, context); | ||
throw new TccHookTestException("tcc hook test exception!"); | ||
} finally { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.afterTccCommit(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).afterTccCommit(xid, branchId, actionName, context); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void testTccRollbackFinally() { | ||
Assertions.assertThrowsExactly(TccHookTestException.class, () -> { | ||
try { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.beforeTccRollback(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).beforeTccRollback(xid, branchId, actionName, context); | ||
throw new TccHookTestException("tcc hook test exception!"); | ||
} finally { | ||
for (TccHook hook : TccHookManager.getHooks()) { | ||
hook.afterTccRollback(xid, branchId, actionName, context); | ||
} | ||
verify(tccHook).afterTccRollback(xid, branchId, actionName, context); | ||
} | ||
}); | ||
} | ||
|
||
public static class TccHookTestException extends RuntimeException { | ||
public TccHookTestException(String message) { | ||
super(message); | ||
} | ||
} | ||
} |