Skip to content

Commit

Permalink
Add test-util module (#2328)
Browse files Browse the repository at this point in the history
* Add test-util module

* Add test-util module

* Add test-util module

* Add test-util module
  • Loading branch information
terrymanu authored Oct 28, 2023
1 parent 4b580bd commit ada191d
Show file tree
Hide file tree
Showing 26 changed files with 198 additions and 354 deletions.
11 changes: 7 additions & 4 deletions kernel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-test-util</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand All @@ -91,10 +98,6 @@
<artifactId>quartz</artifactId>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

import lombok.SneakyThrows;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.kernel.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobScheduleController;
import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobScheduler;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -42,15 +42,17 @@

class OneOffJobBootstrapTest {

private static final ZookeeperConfiguration ZOOKEEPER_CONFIGURATION = new ZookeeperConfiguration(EmbedTestingServer.getConnectionString(), OneOffJobBootstrapTest.class.getSimpleName());
private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(7181);

private static final ZookeeperConfiguration ZOOKEEPER_CONFIGURATION = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), OneOffJobBootstrapTest.class.getSimpleName());

private static final int SHARDING_TOTAL_COUNT = 3;

private ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void init() {
EmbedTestingServer.start();
EMBED_TESTING_SERVER.start();
}

@BeforeEach
Expand All @@ -60,7 +62,7 @@ void setUp() {
}

@AfterEach
void teardown() {
void tearDown() {
zkRegCenter.close();
}

Expand Down Expand Up @@ -108,7 +110,7 @@ private Scheduler getScheduler(final OneOffJobBootstrap oneOffJobBootstrap) {
private void blockUtilFinish(final OneOffJobBootstrap oneOffJobBootstrap, final AtomicInteger counter) {
Scheduler scheduler = getScheduler(oneOffJobBootstrap);
while (0 == counter.get() || !scheduler.getCurrentlyExecutingJobs().isEmpty()) {
Thread.sleep(100);
Thread.sleep(100L);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@
import org.apache.shardingsphere.elasticjob.kernel.api.bootstrap.JobBootstrap;
import org.apache.shardingsphere.elasticjob.kernel.api.bootstrap.impl.OneOffJobBootstrap;
import org.apache.shardingsphere.elasticjob.kernel.api.bootstrap.impl.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.kernel.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.kernel.internal.election.LeaderService;
import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry;
import org.apache.shardingsphere.elasticjob.kernel.util.ReflectionUtils;
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.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;

@Getter(AccessLevel.PROTECTED)
public abstract class BaseIntegrateTest {

private static final ZookeeperConfiguration ZOOKEEPER_CONFIG = new ZookeeperConfiguration(EmbedTestingServer.getConnectionString(), "zkRegTestCenter");
private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(7181);

private static final ZookeeperConfiguration ZOOKEEPER_CONFIG = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), "zkRegTestCenter");

@Getter(AccessLevel.PROTECTED)
private static final CoordinatorRegistryCenter REGISTRY_CENTER = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIG);
Expand Down Expand Up @@ -75,7 +77,7 @@ private JobBootstrap createJobBootstrap(final TestType type, final ElasticJob el

@BeforeAll
static void init() {
EmbedTestingServer.start();
EMBED_TESTING_SERVER.start();
ZOOKEEPER_CONFIG.setConnectionTimeoutMilliseconds(30000);
REGISTRY_CENTER.init();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,24 @@
import org.apache.shardingsphere.elasticjob.kernel.api.bootstrap.JobBootstrap;
import org.apache.shardingsphere.elasticjob.kernel.api.bootstrap.impl.OneOffJobBootstrap;
import org.apache.shardingsphere.elasticjob.kernel.api.bootstrap.impl.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.kernel.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.kernel.internal.annotation.JobAnnotationBuilder;
import org.apache.shardingsphere.elasticjob.kernel.internal.election.LeaderService;
import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry;
import org.apache.shardingsphere.elasticjob.kernel.util.ReflectionUtils;
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.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;

@Getter(AccessLevel.PROTECTED)
public abstract class BaseAnnotationTest {

private static final ZookeeperConfiguration ZOOKEEPER_CONFIG = new ZookeeperConfiguration(EmbedTestingServer.getConnectionString(), "zkRegTestCenter");
private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(7181);

private static final ZookeeperConfiguration ZOOKEEPER_CONFIG = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), "zkRegTestCenter");

@Getter(AccessLevel.PROTECTED)
private static final CoordinatorRegistryCenter REGISTRY_CENTER = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIG);
Expand Down Expand Up @@ -75,7 +77,7 @@ private JobBootstrap createJobBootstrap(final TestType type, final ElasticJob el

@BeforeAll
static void init() {
EmbedTestingServer.start();
EMBED_TESTING_SERVER.start();
ZOOKEEPER_CONFIG.setConnectionTimeoutMilliseconds(30000);
REGISTRY_CENTER.init();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import org.apache.shardingsphere.elasticjob.api.ElasticJob;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.kernel.api.bootstrap.impl.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.kernel.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry;
import org.apache.shardingsphere.elasticjob.kernel.util.ReflectionUtils;
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.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -36,7 +36,9 @@ public abstract class BaseSnapshotServiceTest {

static final int DUMP_PORT = 9000;

private static final ZookeeperConfiguration ZOOKEEPER_CONFIG = new ZookeeperConfiguration(EmbedTestingServer.getConnectionString(), "zkRegTestCenter");
private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(7181);

private static final ZookeeperConfiguration ZOOKEEPER_CONFIG = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), "zkRegTestCenter");

@Getter(value = AccessLevel.PROTECTED)
private static final CoordinatorRegistryCenter REG_CENTER = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIG);
Expand All @@ -55,7 +57,7 @@ public BaseSnapshotServiceTest(final ElasticJob elasticJob) {

@BeforeAll
static void init() {
EmbedTestingServer.start();
EMBED_TESTING_SERVER.start();
ZOOKEEPER_CONFIG.setConnectionTimeoutMilliseconds(30000);
REG_CENTER.init();
}
Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
<module>kernel</module>
<module>lifecycle</module>
<module>restful</module>

<module>ecosystem</module>
<module>spring</module>

<module>test</module>

<module>distribution</module>
</modules>

Expand Down
12 changes: 7 additions & 5 deletions registry-center/provider/zookeeper-curator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-test-util</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
Expand All @@ -45,10 +52,5 @@
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.test.KillSession;
import org.apache.shardingsphere.elasticjob.reg.base.ElectionCandidate;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -42,6 +42,8 @@
@ExtendWith(MockitoExtension.class)
class ZookeeperElectionServiceTest {

private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(9181);

private static final String HOST_AND_PORT = "localhost:8899";

private static final String ELECTION_PATH = "/election";
Expand All @@ -51,18 +53,18 @@ class ZookeeperElectionServiceTest {

@BeforeAll
static void init() {
EmbedTestingServer.start();
EMBED_TESTING_SERVER.start();
}

@Test
void assertContend() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
CuratorFramework client = CuratorFrameworkFactory.newClient(EMBED_TESTING_SERVER.getConnectionString(), new RetryOneTime(2000));
client.start();
client.blockUntilConnected();
ZookeeperElectionService service = new ZookeeperElectionService(HOST_AND_PORT, client, ELECTION_PATH, electionCandidate);
service.start();
ElectionCandidate anotherElectionCandidate = mock(ElectionCandidate.class);
CuratorFramework anotherClient = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
CuratorFramework anotherClient = CuratorFrameworkFactory.newClient(EMBED_TESTING_SERVER.getConnectionString(), new RetryOneTime(2000));
ZookeeperElectionService anotherService = new ZookeeperElectionService("ANOTHER_CLIENT:8899", anotherClient, ELECTION_PATH, anotherElectionCandidate);
anotherClient.start();
anotherClient.blockUntilConnected();
Expand All @@ -80,7 +82,7 @@ void assertContend() throws Exception {
@SneakyThrows
private void blockUntilCondition(final Supplier<Boolean> condition) {
while (!condition.get()) {
Thread.sleep(100);
Thread.sleep(100L);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.elasticjob.reg.base.LeaderExecutionCallback;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -33,14 +33,16 @@

class ZookeeperRegistryCenterExecuteInLeaderTest {

private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(9181);

private static final ZookeeperConfiguration ZOOKEEPER_CONFIGURATION =
new ZookeeperConfiguration(EmbedTestingServer.getConnectionString(), ZookeeperRegistryCenterExecuteInLeaderTest.class.getName());
new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), ZookeeperRegistryCenterExecuteInLeaderTest.class.getName());

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EmbedTestingServer.start();
EMBED_TESTING_SERVER.start();
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(30000);
zkRegCenter.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.util.ZookeeperRegistryCenterTestUtil;
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
import org.apache.zookeeper.KeeperException.NoAuthException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -33,15 +33,17 @@

class ZookeeperRegistryCenterForAuthTest {

private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(9181);

private static final String NAME_SPACE = ZookeeperRegistryCenterForAuthTest.class.getName();

private static final ZookeeperConfiguration ZOOKEEPER_CONFIGURATION = new ZookeeperConfiguration(EmbedTestingServer.getConnectionString(), NAME_SPACE);
private static final ZookeeperConfiguration ZOOKEEPER_CONFIGURATION = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), NAME_SPACE);

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EmbedTestingServer.start();
EMBED_TESTING_SERVER.start();
ZOOKEEPER_CONFIGURATION.setDigest("digest:password");
ZOOKEEPER_CONFIGURATION.setSessionTimeoutMilliseconds(5000);
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(5000);
Expand All @@ -58,7 +60,7 @@ static void tearDown() {
@Test
void assertInitWithDigestSuccess() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString(EmbedTestingServer.getConnectionString())
.connectString(EMBED_TESTING_SERVER.getConnectionString())
.retryPolicy(new RetryOneTime(2000))
.authorization("digest", "digest:password".getBytes()).build();
client.start();
Expand All @@ -69,7 +71,7 @@ void assertInitWithDigestSuccess() throws Exception {
@Test
void assertInitWithDigestFailure() {
assertThrows(NoAuthException.class, () -> {
CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
CuratorFramework client = CuratorFrameworkFactory.newClient(EMBED_TESTING_SERVER.getConnectionString(), new RetryOneTime(2000));
client.start();
client.blockUntilConnected();
client.getData().forPath("/" + ZookeeperRegistryCenterForAuthTest.class.getName() + "/test/deep/nested");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.CuratorCache;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -30,14 +30,16 @@

class ZookeeperRegistryCenterMiscellaneousTest {

private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(9181);

private static final ZookeeperConfiguration ZOOKEEPER_CONFIGURATION =
new ZookeeperConfiguration(EmbedTestingServer.getConnectionString(), ZookeeperRegistryCenterMiscellaneousTest.class.getName());
new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), ZookeeperRegistryCenterMiscellaneousTest.class.getName());

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EmbedTestingServer.start();
EMBED_TESTING_SERVER.start();
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(30000);
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
zkRegCenter.init();
Expand Down
Loading

0 comments on commit ada191d

Please sign in to comment.