Skip to content

Commit

Permalink
optimize: fix context conflict ut (#7080)
Browse files Browse the repository at this point in the history
  • Loading branch information
wt-better authored Dec 27, 2024
1 parent e19854c commit 408fecd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {

Expand Down Expand Up @@ -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();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 408fecd

Please sign in to comment.