-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleTcpTest.java
152 lines (131 loc) · 5.65 KB
/
SimpleTcpTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package practice.netty.tcp;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.*;
import practice.netty.handler.duplex.Logger;
import practice.netty.handler.inbound.ActiveServerChannelUpdater;
import practice.netty.handler.inbound.ReceiveDataUpdater;
import practice.netty.handler.inbound.ServerResponseHandler;
import practice.netty.handler.outbound.OutboundDelayHandler;
import java.net.SocketAddress;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
@DisplayName("TCP 서버-클라이언트 간단한 통신 테스트")
public class SimpleTcpTest {
// 서버
ServerBootstrap serverBootstrap = new ServerBootstrap();
NioEventLoopGroup serverAcceptGroup = new NioEventLoopGroup();
NioEventLoopGroup serverServiceGroup = new NioEventLoopGroup();
ConcurrentHashMap<SocketAddress, Channel> activeServerChannelMap = new ConcurrentHashMap<>();
// 클라이언트
Bootstrap clientBootstrap = new Bootstrap();
NioEventLoopGroup clientEventLoopGroup = new NioEventLoopGroup();
BlockingQueue<String> clientResponseQueue = new LinkedBlockingQueue<>();
Channel clientChannel;
// 테스트 데이터
String fixedResponse = "RESP";
@BeforeEach
@SneakyThrows
public void beforeEach() {
serverSetupAndStart();
clientSetupAndConnect();
waitForServerServiceActive();
}
private void waitForServerServiceActive() throws InterruptedException {
Thread.sleep(100);
}
@AfterEach
@SneakyThrows
public void afterEach() {
clientEventLoopGroup.shutdownGracefully();
serverServiceGroup.shutdownGracefully();
serverAcceptGroup.shutdownGracefully();
}
@SneakyThrows
private void serverSetupAndStart() {
serverBootstrap.group(serverAcceptGroup, serverServiceGroup)
.channel(NioServerSocketChannel.class)
.localAddress("0.0.0.0", 12345)
.option(ChannelOption.SO_REUSEADDR, true)
.childHandler(new ChannelInitializer<>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline()
// Inbound
.addLast(new ActiveServerChannelUpdater(activeServerChannelMap))
.addLast(new FixedLengthFrameDecoder(4))
.addLast(new StringDecoder())
.addLast(new ServerResponseHandler(fixedResponse))
// Outbound
.addLast(new StringEncoder())
// Duplex
.addLast(new Logger("Server", true));
}
});
serverBootstrap.bind().sync();
}
@SneakyThrows
private void clientSetupAndConnect() {
clientBootstrap.group(clientEventLoopGroup)
.channel(NioSocketChannel.class)
.remoteAddress("127.0.0.1", 12345)
.handler(new ChannelInitializer<>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline()
// Inbound
.addLast(new FixedLengthFrameDecoder(4))
.addLast(new StringDecoder())
.addLast(new ReceiveDataUpdater(clientResponseQueue))
// Outbound
.addLast(new StringEncoder())
// Duplex
.addLast(new Logger("Client", true));
}
});
clientChannel = clientBootstrap.connect().sync().channel();
}
@Test
@SneakyThrows
@DisplayName("메시지 전송-응답 테스트")
void simpleResponseTest() {
// Given : 연결된 서버, 클라이언트
// When : 클라이언트에서 메시지 전송
clientChannel.writeAndFlush("ABCD");
// Then : 서버로부터 응답 수신
String response = clientResponseQueue.poll(100, TimeUnit.MILLISECONDS);
Assertions.assertEquals(fixedResponse, response);
}
@Test
@SneakyThrows
@DisplayName("메시지 N개 수신 테스트")
void multipleReceiveTest() {
// Given : 연결된 서버, 클라이언트
// When : 서버에서 10개 메시지 전송
Channel serverServiceChannel = activeServerChannelMap.get(clientChannel.localAddress());
for (int i = 0; i < 10; i++) {
serverServiceChannel.writeAndFlush(String.format("RES%d", i));
}
// Then : 클라이언트에서 10개 메시지 수신
for (int i = 0; i < 10; i++) {
String response = clientResponseQueue.poll(100, TimeUnit.MILLISECONDS);
Assertions.assertEquals(String.format("RES%d", i), response);
}
}
}