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 27, 2024
1 parent 9bceb88 commit 8725849
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 79 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 @@ -53,7 +53,7 @@ public final class SnapshotService {
private volatile boolean closed;

public SnapshotService(final CoordinatorRegistryCenter regCenter, final int port) {
Preconditions.checkArgument(port >= 0 && port <= 0xFFFF, "Port value out of range: " + port);
Preconditions.checkArgument(port >= -1 && port <= 0xFFFF, "Port value out of range: " + port);
this.regCenter = regCenter;
this.port = port;
}
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 @@ -39,7 +39,7 @@
@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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@Getter(AccessLevel.PROTECTED)
public abstract class BaseE2ETest {

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");

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

public abstract class BaseSnapshotServiceE2ETest {

static final int DUMP_PORT = 9000;

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

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

private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer();

@Getter(value = AccessLevel.PROTECTED)
private static final CoordinatorRegistryCenter REG_CENTER = new ZookeeperRegistryCenter(ZOOKEEPER_CONFIG);
private static CoordinatorRegistryCenter regCenter;

@Getter(value = AccessLevel.PROTECTED)
private static SnapshotService snapshotService = new SnapshotService(REG_CENTER, DUMP_PORT);
private static SnapshotService snapshotService = new SnapshotService(regCenter, DUMP_PORT);

private final ScheduleJobBootstrap bootstrap;

@Getter(value = AccessLevel.PROTECTED)
private final String jobName = System.nanoTime() + "_test_job";

public BaseSnapshotServiceE2ETest(final ElasticJob elasticJob) {
bootstrap = new ScheduleJobBootstrap(REG_CENTER, elasticJob, JobConfiguration.newBuilder(jobName, 3).cron("0/1 * * * * ?").overwrite(true).build());
bootstrap = new ScheduleJobBootstrap(regCenter, elasticJob, JobConfiguration.newBuilder(jobName, 3).cron("0/1 * * * * ?").overwrite(true).build());
}

@BeforeAll
static void init() {
EMBED_TESTING_SERVER.start();
ZOOKEEPER_CONFIG.setConnectionTimeoutMilliseconds(30000);
REG_CENTER.init();
ZookeeperConfiguration zookeeperConfig = new ZookeeperConfiguration(EMBED_TESTING_SERVER.getConnectionString(), "zkRegTestCenter");
regCenter = new ZookeeperRegistryCenter(zookeeperConfig);
zookeeperConfig.setConnectionTimeoutMilliseconds(30000);
regCenter.init();
}

@BeforeEach
void setUp() {
REG_CENTER.init();
regCenter.init();
bootstrap.schedule();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ class SnapshotServiceDisableE2ETest extends BaseSnapshotServiceE2ETest {

@Test
void assertMonitorWithDumpCommand() {
assertThrows(IOException.class, () -> SocketUtils.sendCommand(SnapshotService.DUMP_COMMAND, DUMP_PORT - 1));
assertThrows(IOException.class, () -> SocketUtils.sendCommand(SnapshotService.DUMP_COMMAND, DUMP_PORT + 1));
}

@Test
void assertPortInvalid() {
assertThrows(IllegalArgumentException.class, () -> new SnapshotService(getREG_CENTER(), -1).listen());
assertThrows(IllegalArgumentException.class, () -> new SnapshotService(getRegCenter(), -1).listen());
}

@Test
void assertListenException() throws IOException {
ServerSocket serverSocket = new ServerSocket(9898);
SnapshotService snapshotService = new SnapshotService(getREG_CENTER(), 9898);
SnapshotService snapshotService = new SnapshotService(getRegCenter(), 9898);
snapshotService.listen();
serverSocket.close();
assertNull(ReflectionUtils.getFieldValue(snapshotService, "serverSocket"));
Expand Down
Loading

0 comments on commit 8725849

Please sign in to comment.