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

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new d4b515d4655 Fixes `@EnabledInNativeImage` failure in some unit tests 
(#30031)
d4b515d4655 is described below

commit d4b515d46558f7019924958d045addf03ecfb2e5
Author: Ling Hengqian <[email protected]>
AuthorDate: Wed Feb 7 09:33:11 2024 +0800

    Fixes `@EnabledInNativeImage` failure in some unit tests (#30031)
---
 .../test/natived/jdbc/databases/PostgresTest.java  |  2 +-
 .../test/natived/jdbc/databases/SQLServerTest.java |  2 +-
 .../test/natived/jdbc/mode/cluster/EtcdTest.java   | 42 +++++++++++-----------
 3 files changed, 24 insertions(+), 22 deletions(-)

diff --git 
a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/PostgresTest.java
 
b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/PostgresTest.java
index 9275d359c4e..4860c4aea33 100644
--- 
a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/PostgresTest.java
+++ 
b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/PostgresTest.java
@@ -26,11 +26,11 @@ import org.junit.jupiter.api.condition.EnabledInNativeImage;
 import javax.sql.DataSource;
 import java.sql.SQLException;
 
-@EnabledInNativeImage
 class PostgresTest {
     
     private TestShardingService testShardingService;
     
+    @EnabledInNativeImage
     @Test
     void assertShardingInLocalTransactions() throws SQLException {
         HikariConfig config = new HikariConfig();
diff --git 
a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/SQLServerTest.java
 
b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/SQLServerTest.java
index 62cfe54e448..a86bb139833 100644
--- 
a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/SQLServerTest.java
+++ 
b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/SQLServerTest.java
@@ -26,11 +26,11 @@ import org.junit.jupiter.api.condition.EnabledInNativeImage;
 import javax.sql.DataSource;
 import java.sql.SQLException;
 
-@EnabledInNativeImage
 class SQLServerTest {
     
     private TestShardingService testShardingService;
     
+    @EnabledInNativeImage
     @Test
     void assertShardingInLocalTransactions() throws SQLException {
         HikariConfig config = new HikariConfig();
diff --git 
a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/mode/cluster/EtcdTest.java
 
b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/mode/cluster/EtcdTest.java
index 078b82f4b63..f6a15b9014c 100644
--- 
a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/mode/cluster/EtcdTest.java
+++ 
b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/mode/cluster/EtcdTest.java
@@ -19,7 +19,8 @@ package 
org.apache.shardingsphere.test.natived.jdbc.mode.cluster;
 
 import com.zaxxer.hikari.HikariConfig;
 import com.zaxxer.hikari.HikariDataSource;
-import io.etcd.jetcd.test.EtcdClusterExtension;
+import io.etcd.jetcd.launcher.Etcd;
+import io.etcd.jetcd.launcher.EtcdCluster;
 import org.apache.hc.client5.http.classic.methods.HttpGet;
 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
 import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
@@ -30,27 +31,20 @@ import 
org.apache.shardingsphere.test.natived.jdbc.commons.TestShardingService;
 import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.condition.EnabledInNativeImage;
-import org.junit.jupiter.api.extension.RegisterExtension;
 
 import javax.sql.DataSource;
 import java.io.IOException;
 import java.net.URI;
 import java.sql.SQLException;
 import java.time.Duration;
+import java.util.List;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.nullValue;
 
-@EnabledInNativeImage
 class EtcdTest {
     
-    @RegisterExtension
-    public static final EtcdClusterExtension CLUSTER = 
EtcdClusterExtension.builder()
-            .withNodes(1)
-            .withMountDirectory(false)
-            .build();
-    
     private static final String SYSTEM_PROP_KEY_PREFIX = 
"fixture.test-native.yaml.mode.cluster.etcd.";
     
     private TestShardingService testShardingService;
@@ -63,16 +57,24 @@ class EtcdTest {
      * @see 
org.apache.shardingsphere.mode.repository.cluster.etcd.EtcdRepository
      */
     @Test
+    @EnabledInNativeImage
     void assertShardingInLocalTransactions() throws SQLException {
-        DataSource dataSource = createDataSource();
-        testShardingService = new TestShardingService(dataSource);
-        initEnvironment();
-        
Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(() 
-> {
-            dataSource.getConnection().close();
-            return true;
-        });
-        testShardingService.processSuccess();
-        testShardingService.cleanEnvironment();
+        try (
+                EtcdCluster etcd = Etcd.builder()
+                        .withNodes(1)
+                        .withMountedDataDirectory(false)
+                        .build()) {
+            etcd.start();
+            DataSource dataSource = createDataSource(etcd.clientEndpoints());
+            testShardingService = new TestShardingService(dataSource);
+            initEnvironment();
+            
Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(() 
-> {
+                dataSource.getConnection().close();
+                return true;
+            });
+            testShardingService.processSuccess();
+            testShardingService.cleanEnvironment();
+        }
     }
     
     private void initEnvironment() throws SQLException {
@@ -84,8 +86,8 @@ class EtcdTest {
         testShardingService.getAddressRepository().truncateTable();
     }
     
-    private DataSource createDataSource() {
-        URI clientEndpoint = CLUSTER.clientEndpoints().get(0);
+    private DataSource createDataSource(final List<URI> clientEndpoints) {
+        URI clientEndpoint = clientEndpoints.get(0);
         
Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(() 
-> verifyEtcdClusterRunning(clientEndpoint));
         HikariConfig config = new HikariConfig();
         
config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver");

Reply via email to