Skip to content

Commit c329277

Browse files
authored
Prevent unit tests from using host ports 18181 and 9898 (#2482)
1 parent ec2da64 commit c329277

File tree

10 files changed

+148
-38
lines changed

10 files changed

+148
-38
lines changed

spring/boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/spring/boot/job/ElasticJobSpringBootScannerTest.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,68 @@
1717

1818
package org.apache.shardingsphere.elasticjob.spring.boot.job;
1919

20+
import org.apache.curator.CuratorZookeeperClient;
21+
import org.apache.curator.retry.ExponentialBackoffRetry;
22+
import org.apache.curator.test.TestingServer;
2023
import org.apache.shardingsphere.elasticjob.bootstrap.type.ScheduleJobBootstrap;
2124
import org.apache.shardingsphere.elasticjob.spring.boot.job.fixture.job.impl.AnnotationCustomJob;
2225
import org.apache.shardingsphere.elasticjob.spring.core.scanner.ElasticJobScan;
23-
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
2426
import org.awaitility.Awaitility;
27+
import org.junit.jupiter.api.AfterAll;
2528
import org.junit.jupiter.api.BeforeAll;
2629
import org.junit.jupiter.api.Test;
2730
import org.springframework.beans.factory.annotation.Autowired;
2831
import org.springframework.boot.test.context.SpringBootTest;
2932
import org.springframework.context.ApplicationContext;
33+
import org.springframework.test.annotation.DirtiesContext;
3034
import org.springframework.test.context.ActiveProfiles;
35+
import org.springframework.test.context.DynamicPropertyRegistry;
36+
import org.springframework.test.context.DynamicPropertySource;
3137

38+
import java.io.IOException;
39+
import java.time.Duration;
3240
import java.util.concurrent.TimeUnit;
3341

34-
import static org.hamcrest.CoreMatchers.is;
35-
import static org.hamcrest.MatcherAssert.assertThat;
3642
import static org.junit.jupiter.api.Assertions.assertNotNull;
3743
import static org.junit.jupiter.api.Assertions.assertTrue;
3844

45+
@DirtiesContext
3946
@SpringBootTest(properties = "spring.main.banner-mode=off")
4047
@ActiveProfiles("elasticjob")
4148
@ElasticJobScan(basePackages = "org.apache.shardingsphere.elasticjob.spring.boot.job.fixture.job.impl")
4249
class ElasticJobSpringBootScannerTest {
4350

51+
private static TestingServer testingServer;
52+
4453
@Autowired
4554
private ApplicationContext applicationContext;
4655

56+
@DynamicPropertySource
57+
static void elasticjobProperties(final DynamicPropertyRegistry registry) {
58+
registry.add("elasticjob.regCenter.serverLists", () -> testingServer.getConnectString());
59+
}
60+
4761
@BeforeAll
48-
static void init() {
49-
new EmbedTestingServer(18181).start();
62+
static void init() throws Exception {
63+
testingServer = new TestingServer();
64+
try (
65+
CuratorZookeeperClient client = new CuratorZookeeperClient(testingServer.getConnectString(),
66+
60000, 500, null,
67+
new ExponentialBackoffRetry(500, 3, 1500))) {
68+
client.start();
69+
Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(client::isConnected);
70+
}
5071
AnnotationCustomJob.reset();
5172
}
5273

74+
@AfterAll
75+
static void afterAll() throws IOException {
76+
testingServer.close();
77+
}
78+
5379
@Test
5480
void assertDefaultBeanNameWithTypeJob() {
55-
Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> assertThat(AnnotationCustomJob.isCompleted(), is(true)));
81+
Awaitility.await().atMost(1L, TimeUnit.MINUTES).until(AnnotationCustomJob::isCompleted);
5682
assertTrue(AnnotationCustomJob.isCompleted());
5783
assertNotNull(applicationContext);
5884
assertNotNull(applicationContext.getBean("annotationCustomJobSchedule", ScheduleJobBootstrap.class));

spring/boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/spring/boot/job/ElasticJobSpringBootTest.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,38 @@
1717

1818
package org.apache.shardingsphere.elasticjob.spring.boot.job;
1919

20+
import org.apache.curator.CuratorZookeeperClient;
21+
import org.apache.curator.retry.ExponentialBackoffRetry;
22+
import org.apache.curator.test.TestingServer;
2023
import org.apache.shardingsphere.elasticjob.api.ElasticJob;
2124
import org.apache.shardingsphere.elasticjob.api.JobExtraConfiguration;
2225
import org.apache.shardingsphere.elasticjob.bootstrap.JobBootstrap;
2326
import org.apache.shardingsphere.elasticjob.bootstrap.type.OneOffJobBootstrap;
2427
import org.apache.shardingsphere.elasticjob.bootstrap.type.ScheduleJobBootstrap;
2528
import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobScheduler;
29+
import org.apache.shardingsphere.elasticjob.kernel.tracing.config.TracingConfiguration;
2630
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
2731
import org.apache.shardingsphere.elasticjob.spring.boot.job.fixture.job.impl.CustomTestJob;
2832
import org.apache.shardingsphere.elasticjob.spring.boot.reg.ZookeeperProperties;
2933
import org.apache.shardingsphere.elasticjob.spring.boot.tracing.TracingProperties;
30-
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
3134
import org.apache.shardingsphere.elasticjob.test.util.ReflectionUtils;
32-
import org.apache.shardingsphere.elasticjob.kernel.tracing.config.TracingConfiguration;
3335
import org.awaitility.Awaitility;
36+
import org.junit.jupiter.api.AfterAll;
3437
import org.junit.jupiter.api.BeforeAll;
3538
import org.junit.jupiter.api.Test;
3639
import org.springframework.beans.factory.annotation.Autowired;
3740
import org.springframework.boot.autoconfigure.SpringBootApplication;
3841
import org.springframework.boot.test.context.SpringBootTest;
3942
import org.springframework.context.ApplicationContext;
43+
import org.springframework.test.annotation.DirtiesContext;
4044
import org.springframework.test.context.ActiveProfiles;
45+
import org.springframework.test.context.DynamicPropertyRegistry;
46+
import org.springframework.test.context.DynamicPropertySource;
4147

4248
import javax.sql.DataSource;
49+
import java.io.IOException;
4350
import java.sql.SQLException;
51+
import java.time.Duration;
4452
import java.util.Arrays;
4553
import java.util.Collection;
4654
import java.util.HashSet;
@@ -51,30 +59,49 @@
5159
import static org.hamcrest.CoreMatchers.is;
5260
import static org.hamcrest.MatcherAssert.assertThat;
5361
import static org.junit.jupiter.api.Assertions.assertFalse;
62+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
5463
import static org.junit.jupiter.api.Assertions.assertNotNull;
5564
import static org.junit.jupiter.api.Assertions.assertNull;
5665
import static org.junit.jupiter.api.Assertions.assertTrue;
5766

67+
@DirtiesContext
5868
@SpringBootTest(properties = "spring.main.banner-mode=off")
5969
@SpringBootApplication
6070
@ActiveProfiles("elasticjob")
6171
class ElasticJobSpringBootTest {
6272

63-
private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(18181);
73+
private static TestingServer testingServer;
6474

6575
@Autowired
6676
private ApplicationContext applicationContext;
6777

78+
@DynamicPropertySource
79+
static void elasticjobProperties(final DynamicPropertyRegistry registry) {
80+
registry.add("elasticjob.regCenter.serverLists", () -> testingServer.getConnectString());
81+
}
82+
6883
@BeforeAll
69-
static void init() {
70-
EMBED_TESTING_SERVER.start();
84+
static void init() throws Exception {
85+
testingServer = new TestingServer();
86+
try (
87+
CuratorZookeeperClient client = new CuratorZookeeperClient(testingServer.getConnectString(),
88+
60000, 500, null,
89+
new ExponentialBackoffRetry(500, 3, 1500))) {
90+
client.start();
91+
Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(client::isConnected);
92+
}
93+
}
94+
95+
@AfterAll
96+
static void afterAll() throws IOException {
97+
testingServer.close();
7198
}
7299

73100
@Test
74101
void assertZookeeperProperties() {
75102
assertNotNull(applicationContext);
76103
ZookeeperProperties actual = applicationContext.getBean(ZookeeperProperties.class);
77-
assertThat(actual.getServerLists(), is(EMBED_TESTING_SERVER.getConnectionString()));
104+
assertThat(actual.getServerLists(), is(testingServer.getConnectString()));
78105
assertThat(actual.getNamespace(), is("elasticjob-spring-boot-starter"));
79106
}
80107

@@ -93,7 +120,7 @@ void assertTracingConfigurationCreation() throws SQLException {
93120
TracingConfiguration<?> tracingConfig = applicationContext.getBean(TracingConfiguration.class);
94121
assertNotNull(tracingConfig);
95122
assertThat(tracingConfig.getType(), is("RDB"));
96-
assertTrue(tracingConfig.getTracingStorageConfiguration().getStorage() instanceof DataSource);
123+
assertInstanceOf(DataSource.class, tracingConfig.getTracingStorageConfiguration().getStorage());
97124
DataSource dataSource = (DataSource) tracingConfig.getTracingStorageConfiguration().getStorage();
98125
assertNotNull(dataSource.getConnection());
99126
}

spring/boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/spring/boot/reg/snapshot/ElasticJobSnapshotServiceConfigurationTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,25 @@
1717

1818
package org.apache.shardingsphere.elasticjob.spring.boot.reg.snapshot;
1919

20+
import org.apache.curator.CuratorZookeeperClient;
21+
import org.apache.curator.retry.ExponentialBackoffRetry;
22+
import org.apache.curator.test.InstanceSpec;
23+
import org.apache.curator.test.TestingServer;
2024
import org.apache.shardingsphere.elasticjob.kernel.internal.snapshot.SnapshotService;
21-
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
25+
import org.awaitility.Awaitility;
26+
import org.junit.jupiter.api.AfterAll;
2227
import org.junit.jupiter.api.BeforeAll;
2328
import org.junit.jupiter.api.Test;
2429
import org.springframework.beans.factory.annotation.Autowired;
2530
import org.springframework.boot.autoconfigure.SpringBootApplication;
2631
import org.springframework.boot.test.context.SpringBootTest;
2732
import org.springframework.context.ApplicationContext;
2833
import org.springframework.test.context.ActiveProfiles;
34+
import org.springframework.test.context.DynamicPropertyRegistry;
35+
import org.springframework.test.context.DynamicPropertySource;
36+
37+
import java.io.IOException;
38+
import java.time.Duration;
2939

3040
import static org.junit.jupiter.api.Assertions.assertNotNull;
3141

@@ -34,12 +44,32 @@
3444
@ActiveProfiles("snapshot")
3545
class ElasticJobSnapshotServiceConfigurationTest {
3646

47+
private static TestingServer testingServer;
48+
3749
@Autowired
3850
private ApplicationContext applicationContext;
3951

52+
@DynamicPropertySource
53+
static void elasticjobProperties(final DynamicPropertyRegistry registry) {
54+
registry.add("elasticjob.regCenter.serverLists", () -> testingServer.getConnectString());
55+
registry.add("elasticjob.dump.port", InstanceSpec::getRandomPort);
56+
}
57+
4058
@BeforeAll
41-
static void init() {
42-
new EmbedTestingServer(18181).start();
59+
static void init() throws Exception {
60+
testingServer = new TestingServer();
61+
try (
62+
CuratorZookeeperClient client = new CuratorZookeeperClient(testingServer.getConnectString(),
63+
60000, 500, null,
64+
new ExponentialBackoffRetry(500, 3, 1500))) {
65+
client.start();
66+
Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(client::isConnected);
67+
}
68+
}
69+
70+
@AfterAll
71+
static void afterAll() throws IOException {
72+
testingServer.close();
4373
}
4474

4575
@Test

spring/boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/spring/boot/tracing/TracingConfigurationTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717

1818
package org.apache.shardingsphere.elasticjob.spring.boot.tracing;
1919

20-
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
20+
import org.apache.curator.CuratorZookeeperClient;
21+
import org.apache.curator.retry.ExponentialBackoffRetry;
22+
import org.apache.curator.test.TestingServer;
2123
import org.apache.shardingsphere.elasticjob.kernel.tracing.config.TracingConfiguration;
24+
import org.awaitility.Awaitility;
25+
import org.junit.jupiter.api.AfterAll;
2226
import org.junit.jupiter.api.BeforeAll;
2327
import org.junit.jupiter.api.Test;
2428
import org.springframework.beans.factory.ObjectProvider;
@@ -28,8 +32,12 @@
2832
import org.springframework.context.ApplicationContext;
2933
import org.springframework.core.ResolvableType;
3034
import org.springframework.test.context.ActiveProfiles;
35+
import org.springframework.test.context.DynamicPropertyRegistry;
36+
import org.springframework.test.context.DynamicPropertySource;
3137

3238
import javax.sql.DataSource;
39+
import java.io.IOException;
40+
import java.time.Duration;
3341

3442
import static org.junit.jupiter.api.Assertions.assertFalse;
3543
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -40,12 +48,31 @@
4048
@ActiveProfiles("tracing")
4149
class TracingConfigurationTest {
4250

51+
private static TestingServer testingServer;
52+
4353
@Autowired
4454
private ApplicationContext applicationContext;
4555

56+
@DynamicPropertySource
57+
static void elasticjobProperties(final DynamicPropertyRegistry registry) {
58+
registry.add("elasticjob.regCenter.serverLists", () -> testingServer.getConnectString());
59+
}
60+
4661
@BeforeAll
47-
static void init() {
48-
new EmbedTestingServer(18181).start();
62+
static void init() throws Exception {
63+
testingServer = new TestingServer();
64+
try (
65+
CuratorZookeeperClient client = new CuratorZookeeperClient(testingServer.getConnectString(),
66+
60000, 500, null,
67+
new ExponentialBackoffRetry(500, 3, 1500))) {
68+
client.start();
69+
Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(client::isConnected);
70+
}
71+
}
72+
73+
@AfterAll
74+
static void afterAll() throws IOException {
75+
testingServer.close();
4976
}
5077

5178
@Test

spring/boot-starter/src/test/resources/application-elasticjob.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616
#
1717

18+
# `elasticjob.regCenter.serverLists` is dynamically defined in `org.springframework.test.context.DynamicPropertySource`
1819
spring:
1920
datasource:
2021
url: jdbc:h2:mem:job_event_storage
@@ -27,7 +28,6 @@ elasticjob:
2728
type: RDB
2829
excludeJobNames: [customTestJob]
2930
regCenter:
30-
serverLists: 127.0.0.1:18181
3131
namespace: elasticjob-spring-boot-starter
3232
jobs:
3333
customTestJob:

spring/boot-starter/src/test/resources/application-snapshot.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
# limitations under the License.
1616
#
1717

18+
# `elasticjob.regCenter.serverLists` is dynamically defined in `org.springframework.test.context.DynamicPropertySource`
19+
# `elasticjob.dump.port` is dynamically defined in `org.springframework.test.context.DynamicPropertySource`
1820
elasticjob:
19-
dump:
20-
port: 0
2121
regCenter:
22-
serverLists: 127.0.0.1:18181
2322
namespace: elasticjob-spring-boot-starter

spring/boot-starter/src/test/resources/application-tracing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616
#
1717

18+
# `elasticjob.regCenter.serverLists` is dynamically defined in `org.springframework.test.context.DynamicPropertySource`
1819
elasticjob:
1920
regCenter:
20-
serverLists: 127.0.0.1:18181
2121
namespace: elasticjob-spring-boot-starter

test/e2e/src/test/java/org/apache/shardingsphere/elasticjob/test/e2e/snapshot/SnapshotServiceDisableE2ETest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.shardingsphere.elasticjob.test.e2e.snapshot;
1919

20+
import org.apache.curator.test.InstanceSpec;
2021
import org.apache.shardingsphere.elasticjob.kernel.internal.snapshot.SnapshotService;
2122
import org.apache.shardingsphere.elasticjob.test.e2e.raw.fixture.job.E2EFixtureJobImpl;
2223
import org.apache.shardingsphere.elasticjob.test.util.ReflectionUtils;
@@ -46,8 +47,9 @@ void assertPortInvalid() {
4647

4748
@Test
4849
void assertListenException() throws IOException {
49-
ServerSocket serverSocket = new ServerSocket(9898);
50-
SnapshotService snapshotService = new SnapshotService(getRegCenter(), 9898);
50+
int randomPort = InstanceSpec.getRandomPort();
51+
ServerSocket serverSocket = new ServerSocket(randomPort);
52+
SnapshotService snapshotService = new SnapshotService(getRegCenter(), randomPort);
5153
snapshotService.listen();
5254
serverSocket.close();
5355
assertNull(ReflectionUtils.getFieldValue(snapshotService, "serverSocket"));

test/native/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
<properties>
3030
<maven.deploy.skip>true</maven.deploy.skip>
3131
<!--TODO Blocked by https://github.com/apache/shardingsphere-elasticjob/issues/2425 -->
32-
<spring-boot-dependencies.version>3.3.5</spring-boot-dependencies.version>
33-
<slf4j.version>2.0.16</slf4j.version>
34-
<logback.version>1.5.11</logback.version>
32+
<spring-boot-dependencies.version>3.5.5</spring-boot-dependencies.version>
33+
<slf4j.version>2.0.17</slf4j.version>
34+
<logback.version>1.5.18</logback.version>
3535
</properties>
3636

3737
<dependencies>

0 commit comments

Comments
 (0)