Skip to content

Commit

Permalink
Prevent unit tests and E2E from using host ports 7181, 8181, `900…
Browse files Browse the repository at this point in the history
…0`, `9181` `9872` and `9875`
  • Loading branch information
linghengqian committed Jul 28, 2024
1 parent 9bceb88 commit 8440b12
Show file tree
Hide file tree
Showing 34 changed files with 142 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@

class OneOffJobBootstrapTest {

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 EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer();

private static final int SHARDING_TOTAL_COUNT = 3;

Expand All @@ -52,7 +50,8 @@ class OneOffJobBootstrapTest {
@BeforeAll
static void init() {
EMBED_TESTING_SERVER.start();
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), OneOffJobBootstrapTest.class.getSimpleName());
zkRegCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
zkRegCenter.init();
}

Expand Down
5 changes: 5 additions & 0 deletions ecosystem/error-handler/dingtalk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.apache.curator.test.InstanceSpec;
import org.apache.shardingsphere.elasticjob.error.handler.dingtalk.fixture.DingtalkInternalController;
import org.apache.shardingsphere.elasticjob.spi.executor.error.handler.JobErrorHandler;
import org.apache.shardingsphere.elasticjob.restful.NettyRestfulService;
Expand All @@ -40,7 +41,7 @@

class DingtalkJobErrorHandlerTest {

private static final int PORT = 9875;
private static final int PORT = InstanceSpec.getRandomPort();

private static final String HOST = "localhost";

Expand Down Expand Up @@ -75,7 +76,7 @@ static void close() {

@Test
void assertHandleExceptionWithNotifySuccessful() {
DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler(createConfigurationProperties("http://localhost:9875/send?access_token=mocked_token"));
DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler(createConfigurationProperties("http://localhost:" + PORT + "/send?access_token=mocked_token"));
Throwable cause = new RuntimeException("test");
actual.handleException("test_job", cause);
assertThat(appenderList.size(), is(1));
Expand All @@ -85,7 +86,7 @@ void assertHandleExceptionWithNotifySuccessful() {

@Test
void assertHandleExceptionWithWrongToken() {
DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler(createConfigurationProperties("http://localhost:9875/send?access_token=wrong_token"));
DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler(createConfigurationProperties("http://localhost:" + PORT + "/send?access_token=wrong_token"));
Throwable cause = new RuntimeException("test");
actual.handleException("test_job", cause);
assertThat(appenderList.size(), is(1));
Expand All @@ -95,7 +96,7 @@ void assertHandleExceptionWithWrongToken() {

@Test
void assertHandleExceptionWithUrlIsNotFound() {
DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler(createConfigurationProperties("http://localhost:9875/404"));
DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler(createConfigurationProperties("http://localhost:" + PORT + "/404"));
Throwable cause = new RuntimeException("test");
actual.handleException("test_job", cause);
assertThat(appenderList.size(), is(1));
Expand All @@ -115,7 +116,7 @@ void assertHandleExceptionWithWrongUrl() {

@Test
void assertHandleExceptionWithNoSign() {
DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler(createNoSignJobConfigurationProperties("http://localhost:9875/send?access_token=mocked_token"));
DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler(createNoSignJobConfigurationProperties("http://localhost:" + PORT + "/send?access_token=mocked_token"));
Throwable cause = new RuntimeException("test");
actual.handleException("test_job", cause);
assertThat(appenderList.size(), is(1));
Expand Down
5 changes: 5 additions & 0 deletions ecosystem/error-handler/wechat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.apache.curator.test.InstanceSpec;
import org.apache.shardingsphere.elasticjob.error.handler.wechat.fixture.WechatInternalController;
import org.apache.shardingsphere.elasticjob.spi.executor.error.handler.JobErrorHandler;
import org.apache.shardingsphere.elasticjob.restful.NettyRestfulService;
Expand All @@ -40,7 +41,7 @@

class WechatJobErrorHandlerTest {

private static final int PORT = 9872;
private static final int PORT = InstanceSpec.getRandomPort();

private static final String HOST = "localhost";

Expand Down Expand Up @@ -75,7 +76,7 @@ static void close() {

@Test
void assertHandleExceptionWithNotifySuccessful() {
WechatJobErrorHandler actual = getWechatJobErrorHandler(createConfigurationProperties("http://localhost:9872/send?key=mocked_key"));
WechatJobErrorHandler actual = getWechatJobErrorHandler(createConfigurationProperties("http://localhost:" + PORT + "/send?key=mocked_key"));
Throwable cause = new RuntimeException("test");
actual.handleException("test_job", cause);
assertThat(appenderList.size(), is(1));
Expand All @@ -85,7 +86,7 @@ void assertHandleExceptionWithNotifySuccessful() {

@Test
void assertHandleExceptionWithWrongToken() {
WechatJobErrorHandler actual = getWechatJobErrorHandler(createConfigurationProperties("http://localhost:9872/send?key=wrong_key"));
WechatJobErrorHandler actual = getWechatJobErrorHandler(createConfigurationProperties("http://localhost:" + PORT + "/send?key=wrong_key"));
Throwable cause = new RuntimeException("test");
actual.handleException("test_job", cause);
assertThat(appenderList.size(), is(1));
Expand All @@ -105,7 +106,7 @@ void assertHandleExceptionWithWrongUrl() {

@Test
void assertHandleExceptionWithUrlIsNotFound() {
WechatJobErrorHandler actual = getWechatJobErrorHandler(createConfigurationProperties("http://localhost:9872/404"));
WechatJobErrorHandler actual = getWechatJobErrorHandler(createConfigurationProperties("http://localhost:" + PORT + "/404"));
Throwable cause = new RuntimeException("test");
actual.handleException("test_job", cause);
assertThat(appenderList.size(), is(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class JobAPIFactoryTest {

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

@BeforeAll
static void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
@ExtendWith(MockitoExtension.class)
class JobOperateAPIImplTest {

static final int DUMP_PORT = 9000;

private JobOperateAPI jobOperateAPI;

// TODO We should not use `Mock.Strictness.LENIENT` here, but the default. This is a flaw in the unit test design.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

class RegistryCenterFactoryTest {

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

@BeforeAll
static void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
@ExtendWith(MockitoExtension.class)
class ZookeeperElectionServiceTest {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@

class ZookeeperRegistryCenterExecuteInLeaderTest {

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

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

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EMBED_TESTING_SERVER.start();
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(30000);
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), ZookeeperRegistryCenterExecuteInLeaderTest.class.getName());
zkRegCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
zookeeperConfiguration.setConnectionTimeoutMilliseconds(30000);
zkRegCenter.init();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,20 @@

class ZookeeperRegistryCenterForAuthTest {

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

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

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

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EMBED_TESTING_SERVER.start();
ZOOKEEPER_CONFIGURATION.setDigest("digest:password");
ZOOKEEPER_CONFIGURATION.setSessionTimeoutMilliseconds(5000);
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(5000);
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), NAME_SPACE);
zookeeperConfiguration.setDigest("digest:password");
zookeeperConfiguration.setSessionTimeoutMilliseconds(5000);
zookeeperConfiguration.setConnectionTimeoutMilliseconds(5000);
zkRegCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
zkRegCenter.init();
RegistryCenterEnvironmentPreparer.persist(zkRegCenter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@

class ZookeeperRegistryCenterMiscellaneousTest {

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

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

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EMBED_TESTING_SERVER.start();
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(30000);
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
zookeeperConfiguration = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), ZookeeperRegistryCenterMiscellaneousTest.class.getName());
zookeeperConfiguration.setConnectionTimeoutMilliseconds(30000);
zkRegCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
zkRegCenter.init();
zkRegCenter.addCacheData("/test");
}
Expand All @@ -64,7 +64,7 @@ void assertGetRawCache() {

@Test
void assertGetZkConfig() {
ZookeeperRegistryCenter zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
assertThat(zkRegCenter.getZkConfig(), is(ZOOKEEPER_CONFIGURATION));
ZookeeperRegistryCenter zkRegCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
assertThat(zkRegCenter.getZkConfig(), is(zookeeperConfiguration));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,16 @@

class ZookeeperRegistryCenterModifyTest {

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

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

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EMBED_TESTING_SERVER.start();
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(30000);
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), ZookeeperRegistryCenterModifyTest.class.getName());
zkRegCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
zookeeperConfiguration.setConnectionTimeoutMilliseconds(30000);
zkRegCenter.init();
RegistryCenterEnvironmentPreparer.persist(zkRegCenter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@

class ZookeeperRegistryCenterQueryWithCacheTest {

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

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

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EMBED_TESTING_SERVER.start();
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(30000);
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), ZookeeperRegistryCenterQueryWithCacheTest.class.getName());
zkRegCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
zookeeperConfiguration.setConnectionTimeoutMilliseconds(30000);
zkRegCenter.init();
RegistryCenterEnvironmentPreparer.persist(zkRegCenter);
zkRegCenter.addCacheData("/test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@

class ZookeeperRegistryCenterQueryWithoutCacheTest {

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

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

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EMBED_TESTING_SERVER.start();
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(30000);
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), ZookeeperRegistryCenterQueryWithoutCacheTest.class.getName());
zookeeperConfiguration.setConnectionTimeoutMilliseconds(30000);
zkRegCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
zkRegCenter.init();
RegistryCenterEnvironmentPreparer.persist(zkRegCenter);
zkRegCenter.addCacheData("/other");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@

class ZookeeperRegistryCenterTransactionTest {

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

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

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EMBED_TESTING_SERVER.start();
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(30000);
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), ZookeeperRegistryCenterTransactionTest.class.getName());
zkRegCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
zookeeperConfiguration.setConnectionTimeoutMilliseconds(30000);
zkRegCenter.init();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,16 @@

class ZookeeperRegistryCenterWatchTest {

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

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

private static ZookeeperRegistryCenter zkRegCenter;

@BeforeAll
static void setUp() {
EMBED_TESTING_SERVER.start();
zkRegCenter = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIGURATION);
ZOOKEEPER_CONFIGURATION.setConnectionTimeoutMilliseconds(30000);
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), ZookeeperRegistryCenterWatchTest.class.getName());
zkRegCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
zookeeperConfiguration.setConnectionTimeoutMilliseconds(30000);
zkRegCenter.init();
RegistryCenterEnvironmentPreparer.persist(zkRegCenter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ elasticjob:
type: RDB
excludeJobNames: [customTestJob]
regCenter:
serverLists: localhost:18181
serverLists: 127.0.0.1:18181
namespace: elasticjob-spring-boot-starter
jobs:
customTestJob:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ elasticjob:
dump:
port: 0
regCenter:
serverLists: localhost:18181
serverLists: 127.0.0.1:18181
namespace: elasticjob-spring-boot-starter
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@

elasticjob:
regCenter:
serverLists: localhost:18181
serverLists: 127.0.0.1:18181
namespace: elasticjob-spring-boot-starter
Loading

0 comments on commit 8440b12

Please sign in to comment.