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` and `9181`
  • Loading branch information
linghengqian committed Jul 28, 2024
1 parent 9bceb88 commit e25a2ab
Show file tree
Hide file tree
Showing 30 changed files with 121 additions and 121 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.
#

regCenter.serverLists=localhost:3181
regCenter.serverLists=127.0.0.1:3181
regCenter.namespace=elasticjob-spring-test
regCenter.baseSleepTimeMilliseconds=1000
regCenter.maxSleepTimeMilliseconds=3000
Expand Down
4 changes: 2 additions & 2 deletions spring/namespace/src/test/resources/conf/reg/conf.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
# limitations under the License.
#

regCenter1.serverLists=localhost:3181
regCenter1.serverLists=127.0.0.1:3181
regCenter1.namespace=regCenter1
regCenter1.baseSleepTimeMilliseconds=1000
regCenter1.maxSleepTimeMilliseconds=3000
regCenter1.maxRetries=3

regCenter2.serverLists=localhost:3181
regCenter2.serverLists=127.0.0.1:3181
regCenter2.namespace=regCenter2
regCenter2.baseSleepTimeMilliseconds=1000
regCenter2.maxSleepTimeMilliseconds=3000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
@Getter(AccessLevel.PROTECTED)
public abstract class BaseAnnotationE2ETest {

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

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

@Getter(AccessLevel.PROTECTED)
private static final CoordinatorRegistryCenter REGISTRY_CENTER = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIG);
private static CoordinatorRegistryCenter registryCenter;

private final ElasticJob elasticJob;

Expand All @@ -61,15 +61,15 @@ protected BaseAnnotationE2ETest(final TestType type, final ElasticJob elasticJob
jobConfiguration = JobAnnotationBuilder.generateJobConfiguration(elasticJob.getClass());
jobName = jobConfiguration.getJobName();
jobBootstrap = createJobBootstrap(type, elasticJob);
leaderService = new LeaderService(REGISTRY_CENTER, jobName);
leaderService = new LeaderService(registryCenter, jobName);
}

private JobBootstrap createJobBootstrap(final TestType type, final ElasticJob elasticJob) {
switch (type) {
case SCHEDULE:
return new ScheduleJobBootstrap(REGISTRY_CENTER, elasticJob);
return new ScheduleJobBootstrap(registryCenter, elasticJob);
case ONE_OFF:
return new OneOffJobBootstrap(REGISTRY_CENTER, elasticJob);
return new OneOffJobBootstrap(registryCenter, elasticJob);
default:
throw new RuntimeException(String.format("Cannot support `%s`", type));
}
Expand All @@ -78,8 +78,10 @@ private JobBootstrap createJobBootstrap(final TestType type, final ElasticJob el
@BeforeAll
static void init() {
EMBED_TESTING_SERVER.start();
ZOOKEEPER_CONFIG.setConnectionTimeoutMilliseconds(30000);
REGISTRY_CENTER.init();
zookeeperConfig = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), "zkRegTestCenter");
registryCenter = new ZookeeperRegistryCenter(zookeeperConfig);
zookeeperConfig.setConnectionTimeoutMilliseconds(30000);
registryCenter.init();
}

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ class OneOffEnabledJobE2ETest extends BaseAnnotationE2ETest {
void assertEnabledRegCenterInfo() {
assertThat(JobRegistry.getInstance().getCurrentShardingTotalCount(getJobName()), is(1));
assertThat(JobRegistry.getInstance().getJobInstance(getJobName()).getServerIp(), is(IpUtils.getIp()));
JobConfiguration jobConfig = YamlEngine.unmarshal(getREGISTRY_CENTER().get("/" + getJobName() + "/config"), JobConfigurationPOJO.class).toJobConfiguration();
JobConfiguration jobConfig = YamlEngine.unmarshal(getRegistryCenter().get("/" + getJobName() + "/config"), JobConfigurationPOJO.class).toJobConfiguration();
assertThat(jobConfig.getShardingTotalCount(), is(1));
assertNull(jobConfig.getCron());
assertThat(getREGISTRY_CENTER().get("/" + getJobName() + "/servers/" + JobRegistry.getInstance().getJobInstance(getJobName()).getServerIp()), is(ServerStatus.ENABLED.name()));
assertThat(getREGISTRY_CENTER().get("/" + getJobName() + "/leader/election/instance"), is(JobRegistry.getInstance().getJobInstance(getJobName()).getJobInstanceId()));
assertTrue(getREGISTRY_CENTER().isExisted("/" + getJobName() + "/instances/" + JobRegistry.getInstance().getJobInstance(getJobName()).getJobInstanceId()));
getREGISTRY_CENTER().remove("/" + getJobName() + "/leader/election");
assertThat(getRegistryCenter().get("/" + getJobName() + "/servers/" + JobRegistry.getInstance().getJobInstance(getJobName()).getServerIp()), is(ServerStatus.ENABLED.name()));
assertThat(getRegistryCenter().get("/" + getJobName() + "/leader/election/instance"), is(JobRegistry.getInstance().getJobInstance(getJobName()).getJobInstanceId()));
assertTrue(getRegistryCenter().isExisted("/" + getJobName() + "/instances/" + JobRegistry.getInstance().getJobInstance(getJobName()).getJobInstanceId()));
getRegistryCenter().remove("/" + getJobName() + "/leader/election");
assertTrue(getLeaderService().isLeaderUntilBlock());
}

@Test
void assertJobInit() {
Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> MatcherAssert.assertThat(((AnnotationUnShardingJob) getElasticJob()).isCompleted(), is(true)));
assertTrue(getREGISTRY_CENTER().isExisted("/" + getJobName() + "/sharding"));
assertTrue(getRegistryCenter().isExisted("/" + getJobName() + "/sharding"));
}

}
Loading

0 comments on commit e25a2ab

Please sign in to comment.