Skip to content

Commit 0fcddcd

Browse files
authored
Merge pull request #11 from meethigher/issue-8
添加issue8单元测试
2 parents 05b055c + f81ed09 commit 0fcddcd

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package top.meethigher.proxy.tcp.tunnel;
2+
3+
import io.vertx.core.Vertx;
4+
import io.vertx.core.net.NetClient;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import java.util.concurrent.CountDownLatch;
11+
import java.util.concurrent.TimeUnit;
12+
13+
public class Issue8Test {
14+
15+
private static final Logger log = LoggerFactory.getLogger(Issue8Test.class);
16+
17+
@Test
18+
public void test() throws Exception {
19+
20+
/**
21+
* IDEA 多配置“批量启动”——使用 Run/Debug Configuration 的“Compound”功能
22+
* 步骤如下:
23+
* 打开Run/Debug Configurations窗口(快捷键 Ctrl+Alt+R / 右上角 Edit Configurations)。
24+
*
25+
* 新建三个 Application 类型配置,分别设置好 ClassA、ClassB、ClassC 的 main。
26+
*
27+
* 再新建一个Compound类型配置。
28+
*
29+
* 在 Compound 配置的“Run/Debug Configurations”里,添加刚刚那三个配置,顺序拖拽即可调整。
30+
*
31+
* 选中 Compound 配置,点绿色启动按钮,一次性批量顺序启动三(或更多)个实例。
32+
*
33+
* 注意:Compound 是并发同时,不是顺序启动。IDEA 会按照你添加的顺序一个一个起。
34+
*/
35+
36+
// step1: run top.meethigher.proxy.tcp.tunnel.issue8.Issue8BackendServer.main
37+
38+
// step2: run top.meethigher.proxy.tcp.tunnel.issue8.Issue8TunnelServer.main
39+
40+
// step3: run top.meethigher.proxy.tcp.tunnel.issue8.Issue8TunnelClient.main
41+
42+
Vertx vertx = Vertx.vertx();
43+
NetClient netClient = vertx.createNetClient();
44+
int total = 500;
45+
CountDownLatch latch = new CountDownLatch(total);
46+
for (int i = 0; i < total; i++) {
47+
final String id = String.valueOf(i + 1);
48+
netClient.connect(2222, "127.0.0.1").onSuccess(socket -> {
49+
socket.pause();
50+
socket.handler(buf -> {
51+
log.info("{} received: {}", id, buf);
52+
latch.countDown();
53+
});
54+
socket.resume();
55+
});
56+
}
57+
58+
latch.await(5, TimeUnit.SECONDS);
59+
Assert.assertEquals(total, total - latch.getCount());
60+
}
61+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package top.meethigher.proxy.tcp.tunnel.issue8;
2+
3+
import io.vertx.core.Vertx;
4+
import io.vertx.core.buffer.Buffer;
5+
import io.vertx.core.net.NetServer;
6+
7+
public class Issue8BackendServer {
8+
public static void main(String[] args) {
9+
Vertx vertx = Vertx.vertx();
10+
NetServer netServer = vertx.createNetServer();
11+
netServer.connectHandler(socket -> {
12+
socket.remoteAddress();
13+
socket.write(Buffer.buffer("SSH-2.0-OpenSSH_8.7")).onComplete(ar -> {
14+
System.out.println(socket.remoteAddress().toString() + " write " + ar.succeeded());
15+
});
16+
}).listen(23).onComplete(ar -> {
17+
if (ar.succeeded()) {
18+
System.out.println("Server started on port " + ar.result().actualPort());
19+
} else {
20+
System.err.println("Server failed to start");
21+
ar.cause().printStackTrace();
22+
System.exit(1);
23+
}
24+
});
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package top.meethigher.proxy.tcp.tunnel.issue8;
2+
3+
import io.vertx.core.Vertx;
4+
import io.vertx.core.net.NetClient;
5+
import io.vertx.core.net.NetClientOptions;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import top.meethigher.proxy.tcp.tunnel.ReverseTcpProxyTunnelClient;
9+
10+
import java.util.concurrent.TimeUnit;
11+
12+
public class Issue8TunnelClient {
13+
14+
private static final Vertx vertx = Vertx.vertx();
15+
private static final Logger log = LoggerFactory.getLogger(Issue8TunnelClient.class);
16+
17+
public static void main(String[] args) {
18+
NetClient netClient = vertx.createNetClient(new NetClientOptions()
19+
.setIdleTimeout(999999999)
20+
.setIdleTimeoutUnit(TimeUnit.MILLISECONDS));
21+
ReverseTcpProxyTunnelClient.create(vertx, netClient)
22+
.dataProxyPort(2222)
23+
.dataProxyHost("127.0.0.1")
24+
.dataProxyName("ssh")
25+
.backendHost("127.0.0.1")
26+
.backendPort(23)
27+
.connect("127.0.0.1", 44444);
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package top.meethigher.proxy.tcp.tunnel.issue8;
2+
3+
import io.vertx.core.Vertx;
4+
import io.vertx.core.net.NetServer;
5+
import io.vertx.core.net.NetServerOptions;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import top.meethigher.proxy.tcp.tunnel.ReverseTcpProxyTunnelServer;
9+
10+
import java.util.concurrent.TimeUnit;
11+
12+
public class Issue8TunnelServer {
13+
14+
private static final Vertx vertx = Vertx.vertx();
15+
private static final Logger log = LoggerFactory.getLogger(Issue8TunnelServer.class);
16+
17+
public static void main(String[] args) {
18+
NetServer netServer = vertx.createNetServer(
19+
new NetServerOptions()
20+
.setIdleTimeout(999999999)
21+
.setIdleTimeoutUnit(TimeUnit.MILLISECONDS));
22+
ReverseTcpProxyTunnelServer.create(vertx, netServer)
23+
.heartbeatDelay(888888888)
24+
.judgeDelay(50)
25+
.port(44444)
26+
.start();
27+
}
28+
}

0 commit comments

Comments
 (0)