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

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


The following commit(s) were added to refs/heads/master by this push:
     new 33273fbc0 Use EmbedTestingServer on lifecycle module (#2329)
33273fbc0 is described below

commit 33273fbc027af927b79d5b54fcab0a78bdaa82ff
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Oct 28 23:46:13 2023 +0800

    Use EmbedTestingServer on lifecycle module (#2329)
---
 lifecycle/pom.xml                                  |   7 ++
 .../lifecycle/AbstractEmbedZookeeperBaseTest.java  | 138 ---------------------
 .../lifecycle/api/JobAPIFactoryTest.java           |  24 ++--
 .../internal/reg/RegistryCenterFactoryTest.java    |  20 ++-
 4 files changed, 37 insertions(+), 152 deletions(-)

diff --git a/lifecycle/pom.xml b/lifecycle/pom.xml
index c9b00630e..e3166172b 100644
--- a/lifecycle/pom.xml
+++ b/lifecycle/pom.xml
@@ -39,6 +39,13 @@
             <scope>provided</scope>
         </dependency>
         
+        <dependency>
+            <groupId>org.apache.shardingsphere.elasticjob</groupId>
+            <artifactId>elasticjob-test-util</artifactId>
+            <version>${project.parent.version}</version>
+            <scope>test</scope>
+        </dependency>
+        
         <dependency>
             <groupId>commons-codec</groupId>
             <artifactId>commons-codec</artifactId>
diff --git 
a/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/AbstractEmbedZookeeperBaseTest.java
 
b/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/AbstractEmbedZookeeperBaseTest.java
deleted file mode 100644
index b2ab7d1bd..000000000
--- 
a/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/AbstractEmbedZookeeperBaseTest.java
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.elasticjob.lifecycle;
-
-import lombok.extern.slf4j.Slf4j;
-import org.apache.curator.framework.CuratorFramework;
-import org.apache.curator.framework.CuratorFrameworkFactory;
-import org.apache.curator.framework.imps.CuratorFrameworkState;
-import org.apache.curator.retry.ExponentialBackoffRetry;
-import org.apache.curator.test.TestingServer;
-import org.apache.zookeeper.KeeperException;
-import org.junit.jupiter.api.BeforeAll;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.concurrent.TimeUnit;
-
-@Slf4j
-public abstract class AbstractEmbedZookeeperBaseTest {
-    
-    private static final int PORT = 8181;
-    
-    private static volatile TestingServer testingServer;
-    
-    private static final Object INIT_LOCK = new Object();
-    
-    @BeforeAll
-    static void setUp() {
-        startEmbedTestingServer();
-    }
-    
-    /**
-     * Start embed zookeeper server.
-     */
-    private static void startEmbedTestingServer() {
-        if (null != testingServer) {
-            log.info("Embed zookeeper server already exists 1, on {}", 
testingServer.getConnectString());
-            return;
-        }
-        log.info("Starting embed zookeeper server...");
-        synchronized (INIT_LOCK) {
-            if (null != testingServer) {
-                log.info("Embed zookeeper server already exists 2, on {}", 
testingServer.getConnectString());
-                return;
-            }
-            start0();
-            waitTestingServerReady();
-        }
-    }
-    
-    private static void start0() {
-        try {
-            testingServer = new TestingServer(PORT, true);
-            // CHECKSTYLE:OFF
-        } catch (final Exception ex) {
-            // CHECKSTYLE:ON
-            if (!isIgnoredException(ex)) {
-                throw new RuntimeException(ex);
-            } else {
-                log.warn("Start embed zookeeper server got exception: {}", 
ex.getMessage());
-            }
-        } finally {
-            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
-                try {
-                    testingServer.close();
-                } catch (final IOException ignored) {
-                }
-                log.info("Close embed zookeeper server done");
-            }));
-        }
-    }
-    
-    private static void waitTestingServerReady() {
-        int maxRetries = 60;
-        try (CuratorFramework client = buildCuratorClient()) {
-            client.start();
-            int round = 0;
-            while (round < maxRetries) {
-                try {
-                    if (client.getZookeeperClient().isConnected()) {
-                        log.info("client is connected");
-                        break;
-                    }
-                    if (client.blockUntilConnected(500, 
TimeUnit.MILLISECONDS)) {
-                        CuratorFrameworkState state = client.getState();
-                        Collection<String> childrenKeys = 
client.getChildren().forPath("/");
-                        log.info("TestingServer connected, state={}, 
childrenKeys={}", state, childrenKeys);
-                        break;
-                    }
-                    // CHECKSTYLE:OFF
-                } catch (final Exception ignored) {
-                    // CHECKSTYLE:ON
-                }
-                ++round;
-            }
-        }
-    }
-    
-    private static CuratorFramework buildCuratorClient() {
-        CuratorFrameworkFactory.Builder builder = 
CuratorFrameworkFactory.builder();
-        int retryIntervalMilliseconds = 500;
-        int maxRetries = 3;
-        builder.connectString(getConnectionString())
-                .retryPolicy(new 
ExponentialBackoffRetry(retryIntervalMilliseconds, maxRetries, 
retryIntervalMilliseconds * maxRetries))
-                .namespace("test");
-        builder.sessionTimeoutMs(60 * 1000);
-        builder.connectionTimeoutMs(500);
-        return builder.build();
-    }
-    
-    private static boolean isIgnoredException(final Throwable cause) {
-        return cause instanceof KeeperException.ConnectionLossException || 
cause instanceof KeeperException.NoNodeException || cause instanceof 
KeeperException.NodeExistsException;
-    }
-    
-    /**
-     * Get the connection string.
-     *
-     * @return connection string
-     */
-    public static String getConnectionString() {
-        return "localhost:" + PORT;
-    }
-}
diff --git 
a/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/api/JobAPIFactoryTest.java
 
b/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/api/JobAPIFactoryTest.java
index 53eb758cb..bf9e40f67 100644
--- 
a/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/api/JobAPIFactoryTest.java
+++ 
b/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/api/JobAPIFactoryTest.java
@@ -17,41 +17,49 @@
 
 package org.apache.shardingsphere.elasticjob.lifecycle.api;
 
-import 
org.apache.shardingsphere.elasticjob.lifecycle.AbstractEmbedZookeeperBaseTest;
+import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
+import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.MatcherAssert.assertThat;
 
-class JobAPIFactoryTest extends AbstractEmbedZookeeperBaseTest {
+class JobAPIFactoryTest {
+    
+    private static final EmbedTestingServer EMBED_TESTING_SERVER = new 
EmbedTestingServer(8181);
+    
+    @BeforeAll
+    static void setUp() {
+        EMBED_TESTING_SERVER.start();
+    }
     
     @Test
     void assertCreateJobConfigAPI() {
-        
assertThat(JobAPIFactory.createJobConfigurationAPI(getConnectionString(), 
"namespace", null), instanceOf(JobConfigurationAPI.class));
+        
assertThat(JobAPIFactory.createJobConfigurationAPI(EMBED_TESTING_SERVER.getConnectionString(),
 "namespace", null), instanceOf(JobConfigurationAPI.class));
     }
     
     @Test
     void assertCreateJobOperateAPI() {
-        assertThat(JobAPIFactory.createJobOperateAPI(getConnectionString(), 
"namespace", null), instanceOf(JobOperateAPI.class));
+        
assertThat(JobAPIFactory.createJobOperateAPI(EMBED_TESTING_SERVER.getConnectionString(),
 "namespace", null), instanceOf(JobOperateAPI.class));
     }
     
     @Test
     void assertCreateServerOperateAPI() {
-        
assertThat(JobAPIFactory.createShardingOperateAPI(getConnectionString(), 
"namespace", null), instanceOf(ShardingOperateAPI.class));
+        
assertThat(JobAPIFactory.createShardingOperateAPI(EMBED_TESTING_SERVER.getConnectionString(),
 "namespace", null), instanceOf(ShardingOperateAPI.class));
     }
     
     @Test
     void assertCreateJobStatisticsAPI() {
-        assertThat(JobAPIFactory.createJobStatisticsAPI(getConnectionString(), 
"namespace", null), instanceOf(JobStatisticsAPI.class));
+        
assertThat(JobAPIFactory.createJobStatisticsAPI(EMBED_TESTING_SERVER.getConnectionString(),
 "namespace", null), instanceOf(JobStatisticsAPI.class));
     }
     
     @Test
     void assertCreateServerStatisticsAPI() {
-        
assertThat(JobAPIFactory.createServerStatisticsAPI(getConnectionString(), 
"namespace", null), instanceOf(ServerStatisticsAPI.class));
+        
assertThat(JobAPIFactory.createServerStatisticsAPI(EMBED_TESTING_SERVER.getConnectionString(),
 "namespace", null), instanceOf(ServerStatisticsAPI.class));
     }
     
     @Test
     void assertCreateShardingStatisticsAPI() {
-        
assertThat(JobAPIFactory.createShardingStatisticsAPI(getConnectionString(), 
"namespace", null), instanceOf(ShardingStatisticsAPI.class));
+        
assertThat(JobAPIFactory.createShardingStatisticsAPI(EMBED_TESTING_SERVER.getConnectionString(),
 "namespace", null), instanceOf(ShardingStatisticsAPI.class));
     }
 }
diff --git 
a/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/internal/reg/RegistryCenterFactoryTest.java
 
b/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/internal/reg/RegistryCenterFactoryTest.java
index 22c6eaaa0..48ba2e25b 100644
--- 
a/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/internal/reg/RegistryCenterFactoryTest.java
+++ 
b/lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/internal/reg/RegistryCenterFactoryTest.java
@@ -17,10 +17,11 @@
 
 package org.apache.shardingsphere.elasticjob.lifecycle.internal.reg;
 
-import 
org.apache.shardingsphere.elasticjob.lifecycle.AbstractEmbedZookeeperBaseTest;
 import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
 import 
org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
 import 
org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
+import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
+import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
 import java.lang.reflect.Method;
@@ -29,26 +30,33 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.jupiter.api.Assertions.assertNull;
 
-class RegistryCenterFactoryTest extends AbstractEmbedZookeeperBaseTest {
+class RegistryCenterFactoryTest {
+    
+    private static final EmbedTestingServer EMBED_TESTING_SERVER = new 
EmbedTestingServer(8181);
+    
+    @BeforeAll
+    static void setUp() {
+        EMBED_TESTING_SERVER.start();
+    }
     
     @Test
     void assertCreateCoordinatorRegistryCenterWithoutDigest() throws 
ReflectiveOperationException {
-        ZookeeperConfiguration zkConfig = 
getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(getConnectionString(),
 "namespace", null));
+        ZookeeperConfiguration zkConfig = 
getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(EMBED_TESTING_SERVER.getConnectionString(),
 "namespace", null));
         assertThat(zkConfig.getNamespace(), is("namespace"));
         assertNull(zkConfig.getDigest());
     }
     
     @Test
     void assertCreateCoordinatorRegistryCenterWithDigest() throws 
ReflectiveOperationException {
-        ZookeeperConfiguration zkConfig = 
getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(getConnectionString(),
 "namespace", "digest"));
+        ZookeeperConfiguration zkConfig = 
getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(EMBED_TESTING_SERVER.getConnectionString(),
 "namespace", "digest"));
         assertThat(zkConfig.getNamespace(), is("namespace"));
         assertThat(zkConfig.getDigest(), is("digest"));
     }
     
     @Test
     void assertCreateCoordinatorRegistryCenterFromCache() throws 
ReflectiveOperationException {
-        
RegistryCenterFactory.createCoordinatorRegistryCenter(getConnectionString(), 
"otherNamespace", null);
-        ZookeeperConfiguration zkConfig = 
getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(getConnectionString(),
 "otherNamespace", null));
+        
RegistryCenterFactory.createCoordinatorRegistryCenter(EMBED_TESTING_SERVER.getConnectionString(),
 "otherNamespace", null);
+        ZookeeperConfiguration zkConfig = 
getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(EMBED_TESTING_SERVER.getConnectionString(),
 "otherNamespace", null));
         assertThat(zkConfig.getNamespace(), is("otherNamespace"));
         assertNull(zkConfig.getDigest());
     }

Reply via email to