1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.transport.nio;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.IOException;
27 import java.net.Socket;
28 import java.nio.ByteBuffer;
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31
32 import org.apache.mina.api.AbstractIoHandler;
33 import org.apache.mina.api.IdleStatus;
34 import org.apache.mina.api.IoHandler;
35 import org.apache.mina.api.IoSession;
36 import org.apache.mina.transport.nio.FixedSelectorLoopPool;
37 import org.apache.mina.transport.nio.NioTcpServer;
38 import org.apache.mina.transport.nio.SelectorLoopPool;
39 import org.junit.Test;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44
45
46
47
48
49 public class NioTcpServerHandlerTest {
50
51 private static final Logger LOG = LoggerFactory.getLogger(NioTcpServerHandlerTest.class);
52
53 private static final int CLIENT_COUNT = 100;
54
55 private static final int WAIT_TIME = 5000;
56
57 private final CountDownLatch msgSentLatch = new CountDownLatch(CLIENT_COUNT);
58
59 private final CountDownLatch msgReadLatch = new CountDownLatch(CLIENT_COUNT);
60
61 private final CountDownLatch openLatch = new CountDownLatch(CLIENT_COUNT);
62
63 private final CountDownLatch closedLatch = new CountDownLatch(CLIENT_COUNT);
64
65 private final CountDownLatch idleLatch = new CountDownLatch(CLIENT_COUNT);
66
67 @Test
68 public void generate_all_kind_of_server_event() throws IOException, InterruptedException {
69 final NioTcpServer server = new NioTcpServer();
70 server.setFilters();
71 server.getSessionConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 1000);
72 server.setIoHandler(new Handler());
73 server.bind(0);
74
75
76 Thread.sleep(100);
77
78 final int port = server.getServerSocketChannel().socket().getLocalPort();
79
80 final Socket[] clients = new Socket[CLIENT_COUNT];
81
82
83 for (int i = 0; i < CLIENT_COUNT; i++) {
84 clients[i] = new Socket("127.0.0.1", port);
85 }
86
87
88 assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
89
90
91 for (int i = 0; i < CLIENT_COUNT; i++) {
92 clients[i].getOutputStream().write(("test:" + i).getBytes());
93 clients[i].getOutputStream().flush();
94 }
95
96
97 assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
98
99
100 assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
101
102
103 final byte[] buffer = new byte[1024];
104
105 for (int i = 0; i < CLIENT_COUNT; i++) {
106 final int bytes = clients[i].getInputStream().read(buffer);
107 final String text = new String(buffer, 0, bytes);
108 assertEquals("test:" + i, text);
109 }
110
111
112 assertTrue(idleLatch.await(5 * WAIT_TIME, TimeUnit.MILLISECONDS));
113
114
115 assertEquals(CLIENT_COUNT, closedLatch.getCount());
116 for (int i = 0; i < CLIENT_COUNT; i++) {
117 clients[i].close();
118 }
119
120
121 assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
122
123 server.unbind();
124 }
125
126 @Test
127 public void generateAllKindOfServerEventOneSelector() throws IOException, InterruptedException {
128 SelectorLoopPool selectorLoopPool = new FixedSelectorLoopPool("Server", 1);
129 final NioTcpServer server = new NioTcpServer(selectorLoopPool.getSelectorLoop(), selectorLoopPool, null);
130 server.setFilters();
131 server.setIoHandler(new Handler());
132 server.bind(0);
133
134
135 Thread.sleep(100);
136
137 final int port = server.getServerSocketChannel().socket().getLocalPort();
138
139 final Socket[] clients = new Socket[CLIENT_COUNT];
140
141
142 for (int i = 0; i < CLIENT_COUNT; i++) {
143 clients[i] = new Socket("127.0.0.1", port);
144 }
145
146
147 assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
148
149
150 for (int i = 0; i < CLIENT_COUNT; i++) {
151 clients[i].getOutputStream().write(("test:" + i).getBytes());
152 clients[i].getOutputStream().flush();
153 }
154
155
156 assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
157
158
159 assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
160
161
162 final byte[] buffer = new byte[1024];
163
164 for (int i = 0; i < CLIENT_COUNT; i++) {
165 final int bytes = clients[i].getInputStream().read(buffer);
166 final String text = new String(buffer, 0, bytes);
167 assertEquals("test:" + i, text);
168 }
169
170
171 assertEquals(CLIENT_COUNT, closedLatch.getCount());
172 for (int i = 0; i < CLIENT_COUNT; i++) {
173 clients[i].close();
174 }
175
176
177 assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
178
179 server.unbind();
180 }
181
182 private class Handler extends AbstractIoHandler {
183
184 @Override
185 public void sessionOpened(final IoSession session) {
186 LOG.debug("** session open");
187 openLatch.countDown();
188 }
189
190 @Override
191 public void sessionClosed(final IoSession session) {
192 LOG.debug("** session closed");
193 closedLatch.countDown();
194 }
195
196 @Override
197 public void messageReceived(final IoSession session, final Object message) {
198 LOG.debug("** message received {}", message);
199 msgReadLatch.countDown();
200 if (message instanceof ByteBuffer) {
201 ByteBuffer msg = (ByteBuffer) message;
202 session.write(ByteBuffer.allocate(msg.remaining()).put(msg).flip());
203 } else {
204 fail("non bytebuffer received??");
205 }
206 }
207
208 @Override
209 public void messageSent(final IoSession session, final Object message) {
210 LOG.debug("** message sent {}", message);
211 msgSentLatch.countDown();
212 }
213
214 @Override
215 public void sessionIdle(IoSession session, IdleStatus status) {
216 if (status == IdleStatus.READ_IDLE) {
217 idleLatch.countDown();
218 }
219 }
220 }
221
222 }