Skip to content

Commit 7e1bc04

Browse files
author
ZhangJian He
authored
test: add mdtp connect test (#8)
Signed-off-by: ZhangJian He <[email protected]>
1 parent 9ca37ed commit 7e1bc04

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

.github/workflows/java_unit_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
java-version: '17'
1919
distribution: 'temurin'
2020
- name: unit tests
21-
run: mvn -B clean test
21+
run: mvn -B clean test; mvn -B validate
2222
- name: Upload coverage to Codecov
2323
uses: codecov/codecov-action@v4
2424
env:

mdtp-server/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<artifactId>mdtp-common</artifactId>
1818
<version>${project.version}</version>
1919
</dependency>
20+
<dependency>
21+
<groupId>io.github.protocol-laboratory</groupId>
22+
<artifactId>mdtp-client</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
2025
<dependency>
2126
<groupId>io.netty</groupId>
2227
<artifactId>netty-handler</artifactId>

mdtp-server/src/main/java/io/github/protocol/mdtp/server/MdtpServer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.protocol.mdtp.server;
22

33
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.Channel;
45
import io.netty.channel.ChannelFuture;
56
import io.netty.channel.ChannelInitializer;
67
import io.netty.channel.EventLoopGroup;
@@ -21,6 +22,8 @@ public class MdtpServer implements Closeable {
2122

2223
private EventLoopGroup ioGroup;
2324

25+
private Channel channel;
26+
2427
public MdtpServer(MdtpServerConfig config) {
2528
this.config = config;
2629
}
@@ -44,13 +47,21 @@ protected void initChannel(SocketChannel socketChannel) throws Exception {
4447
});
4548
ChannelFuture channelFuture = serverBootstrap.bind().sync();
4649
if (channelFuture.isSuccess()) {
50+
this.channel = channelFuture.channel();
4751
log.info("mdtp server started");
4852
} else {
4953
log.error("mdtp server start failed", channelFuture.cause());
5054
throw new Exception("mdtp server start failed", channelFuture.cause());
5155
}
5256
}
5357

58+
public int listenPort() {
59+
if (this.channel != null && this.channel.localAddress() instanceof InetSocketAddress) {
60+
return ((InetSocketAddress) this.channel.localAddress()).getPort();
61+
}
62+
return -1;
63+
}
64+
5465
@Override
5566
public void close() throws IOException {
5667
if (this.acceptorGroup != null) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.protocol.mdtp.server;
2+
3+
import io.github.protocol.mdtp.client.MdtpClient;
4+
import io.github.protocol.mdtp.client.MdtpClientConfig;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class MdtpConnectTest {
8+
@Test
9+
public void mdtpConnect() throws Exception {
10+
MdtpServerConfig mdtpServerConfig = new MdtpServerConfig().host("localhost").port(0);
11+
MdtpServer mdtpServer = new MdtpServer(mdtpServerConfig);
12+
mdtpServer.start();
13+
MdtpClientConfig mdtpClientConfig = new MdtpClientConfig().host("localhost").port(mdtpServer.listenPort());
14+
MdtpClient mdtpClient = new MdtpClient(mdtpClientConfig);
15+
mdtpClient.start();
16+
mdtpClient.close();
17+
mdtpServer.close();
18+
}
19+
}

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<maven.compiler.target>17</maven.compiler.target>
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<src.dir>src/main/java</src.dir>
24+
<jacoco.overall.exec>${maven.multiModuleProjectDirectory}/target/jacoco/jacoco.exec</jacoco.overall.exec>
2425
<!-- dependency -->
2526
<junit.version>5.11.0</junit.version>
2627
<log4j.version>2.20.0</log4j.version>
@@ -99,16 +100,23 @@
99100
<groupId>org.jacoco</groupId>
100101
<artifactId>jacoco-maven-plugin</artifactId>
101102
<version>${jacoco-maven-plugin.version}</version>
103+
<configuration>
104+
<destFile>${jacoco.overall.exec}</destFile>
105+
<dataFile>${jacoco.overall.exec}</dataFile>
106+
</configuration>
102107
<executions>
103108
<execution>
104109
<id>prepare-agent</id>
110+
<configuration>
111+
<append>true</append>
112+
</configuration>
105113
<goals>
106114
<goal>prepare-agent</goal>
107115
</goals>
108116
</execution>
109117
<execution>
110118
<id>report</id>
111-
<phase>test</phase>
119+
<phase>validate</phase>
112120
<goals>
113121
<goal>report</goal>
114122
</goals>

0 commit comments

Comments
 (0)