This is an automated email from the ASF dual-hosted git repository.

yixia pushed a commit to branch 2.3.0
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.3.0 by this push:
     new 408fecd77a optimize: fix context conflict ut (#7080)
408fecd77a is described below

commit 408fecd77ae9afaad589f5a3a0118861c0cb6aff
Author: wt_better <[email protected]>
AuthorDate: Fri Dec 27 11:28:07 2024 +0800

    optimize: fix context conflict ut (#7080)
---
 .../rm/tcc/interceptor/ProxyUtilsTccTest.java      | 56 +++++++++++----------
 .../rm/tcc/interceptor/ProxyUtilsTccTest.java      | 58 ++++++++++++----------
 2 files changed, 62 insertions(+), 52 deletions(-)

diff --git 
a/compatible/src/test/java/io/seata/rm/tcc/interceptor/ProxyUtilsTccTest.java 
b/compatible/src/test/java/io/seata/rm/tcc/interceptor/ProxyUtilsTccTest.java
index e886e4bf61..254f7b043e 100644
--- 
a/compatible/src/test/java/io/seata/rm/tcc/interceptor/ProxyUtilsTccTest.java
+++ 
b/compatible/src/test/java/io/seata/rm/tcc/interceptor/ProxyUtilsTccTest.java
@@ -27,9 +27,7 @@ import org.apache.seata.core.model.Resource;
 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 class ProxyUtilsTccTest {
 
     @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();
-    }
-
-
 }
diff --git 
a/tcc/src/test/java/org/apache/seata/rm/tcc/interceptor/ProxyUtilsTccTest.java 
b/tcc/src/test/java/org/apache/seata/rm/tcc/interceptor/ProxyUtilsTccTest.java
index 5149377c4d..277feb37cd 100644
--- 
a/tcc/src/test/java/org/apache/seata/rm/tcc/interceptor/ProxyUtilsTccTest.java
+++ 
b/tcc/src/test/java/org/apache/seata/rm/tcc/interceptor/ProxyUtilsTccTest.java
@@ -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.apache.seata.rm.tcc.TccParam;
 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 class ProxyUtilsTccTest {
 
     @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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to