From 62153d111f56a576a283c7694edee2f70d809b28 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 28 Oct 2023 23:42:33 +0800 Subject: [PATCH] Use EmbedTestingServer on lifecycle module --- lifecycle/pom.xml | 7 + .../AbstractEmbedZookeeperBaseTest.java | 138 ------------------ .../lifecycle/api/JobAPIFactoryTest.java | 24 ++- .../reg/RegistryCenterFactoryTest.java | 20 ++- 4 files changed, 37 insertions(+), 152 deletions(-) delete mode 100644 lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lifecycle/AbstractEmbedZookeeperBaseTest.java diff --git a/lifecycle/pom.xml b/lifecycle/pom.xml index c9b00630ed..e3166172b3 100644 --- a/lifecycle/pom.xml +++ b/lifecycle/pom.xml @@ -39,6 +39,13 @@ provided + + org.apache.shardingsphere.elasticjob + elasticjob-test-util + ${project.parent.version} + test + + commons-codec commons-codec 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 b2ab7d1bde..0000000000 --- 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 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 53eb758cbd..bf9e40f677 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 22c6eaaa09..48ba2e25be 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.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()); }