-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize: fix context conflict ut (#7080)
- Loading branch information
Showing
2 changed files
with
62 additions
and
52 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -27,9 +27,7 @@ | |
import org.apache.seata.core.model.ResourceManager; | ||
import org.apache.seata.integration.tx.api.util.ProxyUtil; | ||
import org.apache.seata.rm.DefaultResourceManager; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collections; | ||
|
@@ -43,10 +41,6 @@ public class ProxyUtilsTccTest { | |
|
||
private final AtomicReference<String> branchReference = new AtomicReference<String>(); | ||
|
||
@BeforeEach | ||
public void before() { | ||
RootContext.bind(DEFAULT_XID); | ||
} | ||
|
||
private final ResourceManager resourceManager = new ResourceManager() { | ||
|
||
|
@@ -109,40 +103,48 @@ public GlobalStatus getGlobalStatus(BranchType branchType, String xid) { | |
|
||
@Test | ||
public void testTcc() { | ||
TccParam tccParam = new TccParam(1, "[email protected]"); | ||
List<String> listB = Collections.singletonList("b"); | ||
try { | ||
//given | ||
RootContext.bind(DEFAULT_XID); | ||
|
||
TccParam tccParam = new TccParam(1, "[email protected]"); | ||
List<String> listB = Collections.singletonList("b"); | ||
|
||
DefaultResourceManager.mockResourceManager(BranchType.TCC, resourceManager); | ||
DefaultResourceManager.mockResourceManager(BranchType.TCC, resourceManager); | ||
|
||
String result = tccActionProxy.prepare(null, 0, listB, tccParam); | ||
String result = tccActionProxy.prepare(null, 0, listB, tccParam); | ||
|
||
//then | ||
Assertions.assertEquals("a", result); | ||
Assertions.assertNotNull(result); | ||
Assertions.assertEquals("tccActionForCompatibleTest", branchReference.get()); | ||
//then | ||
Assertions.assertEquals("a", result); | ||
Assertions.assertNotNull(result); | ||
Assertions.assertEquals("tccActionForCompatibleTest", branchReference.get()); | ||
} finally { | ||
RootContext.unbind(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testTccThrowRawException() { | ||
TccParam tccParam = new TccParam(1, "[email protected]"); | ||
List<String> listB = Collections.singletonList("b"); | ||
try { | ||
//given | ||
RootContext.bind(DEFAULT_XID); | ||
|
||
TccParam tccParam = new TccParam(1, "[email protected]"); | ||
List<String> listB = Collections.singletonList("b"); | ||
|
||
DefaultResourceManager.mockResourceManager(BranchType.TCC, resourceManager); | ||
DefaultResourceManager.mockResourceManager(BranchType.TCC, resourceManager); | ||
|
||
//when | ||
//then | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> tccActionProxy.prepareWithException(null, 0, listB, tccParam)); | ||
//when | ||
//then | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> tccActionProxy.prepareWithException(null, 0, listB, tccParam)); | ||
} finally { | ||
RootContext.unbind(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testTccImplementOtherMethod(){ | ||
public void testTccImplementOtherMethod() { | ||
Assertions.assertTrue(tccActionProxy.otherMethod()); | ||
} | ||
|
||
@AfterEach | ||
public void clear() { | ||
RootContext.unbind(); | ||
} | ||
|
||
|
||
} |
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 |
---|---|---|
|
@@ -16,11 +16,6 @@ | |
*/ | ||
package org.apache.seata.rm.tcc.interceptor; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.apache.seata.core.context.RootContext; | ||
import org.apache.seata.core.exception.TransactionException; | ||
import org.apache.seata.core.model.BranchStatus; | ||
|
@@ -35,12 +30,17 @@ | |
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
|
||
public class ProxyUtilsTccTest { | ||
|
||
private final String DEFAULT_XID = "default_xid"; | ||
|
||
AtomicReference<String> branchReference = new AtomicReference<String>(); | ||
AtomicReference<String> branchReference = new AtomicReference<>(); | ||
|
||
|
||
ResourceManager resourceManager = new ResourceManager() { | ||
|
@@ -104,36 +104,44 @@ public GlobalStatus getGlobalStatus(BranchType branchType, String xid) { | |
|
||
@Test | ||
public void testTcc() { | ||
//given | ||
RootContext.bind(DEFAULT_XID); | ||
try { | ||
//given | ||
RootContext.bind(DEFAULT_XID); | ||
|
||
TccParam tccParam = new TccParam(1, "[email protected]"); | ||
List<String> listB = Arrays.asList("b"); | ||
TccParam tccParam = new TccParam(1, "[email protected]"); | ||
List<String> listB = Arrays.asList("b"); | ||
|
||
DefaultResourceManager.mockResourceManager(BranchType.TCC, resourceManager); | ||
DefaultResourceManager.mockResourceManager(BranchType.TCC, resourceManager); | ||
|
||
//when | ||
String result = tccActionProxy.prepare(null, 0, listB, tccParam); | ||
//when | ||
String result = tccActionProxy.prepare(null, 0, listB, tccParam); | ||
|
||
//then | ||
Assertions.assertEquals("a", result); | ||
Assertions.assertNotNull(result); | ||
Assertions.assertEquals("normalTccActionForTest", branchReference.get()); | ||
//then | ||
Assertions.assertEquals("a", result); | ||
Assertions.assertNotNull(result); | ||
Assertions.assertEquals("normalTccActionForTest", branchReference.get()); | ||
} finally { | ||
RootContext.unbind(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testTccThrowRawException() { | ||
//given | ||
RootContext.bind(DEFAULT_XID); | ||
try { | ||
//given | ||
RootContext.bind(DEFAULT_XID); | ||
|
||
TccParam tccParam = new TccParam(1, "[email protected]"); | ||
List<String> listB = Arrays.asList("b"); | ||
TccParam tccParam = new TccParam(1, "[email protected]"); | ||
List<String> listB = Arrays.asList("b"); | ||
|
||
DefaultResourceManager.mockResourceManager(BranchType.TCC, resourceManager); | ||
DefaultResourceManager.mockResourceManager(BranchType.TCC, resourceManager); | ||
|
||
//when | ||
//then | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> tccActionProxy.prepareWithException(null, 0, listB, tccParam)); | ||
//when | ||
//then | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> tccActionProxy.prepareWithException(null, 0, listB, tccParam)); | ||
} finally { | ||
RootContext.unbind(); | ||
} | ||
} | ||
|
||
@Test | ||
|