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.assertTrue;
23
24 import java.io.IOException;
25 import java.net.InetSocketAddress;
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.TimeUnit;
29
30 import org.apache.mina.api.AbstractIoHandler;
31 import org.apache.mina.api.IoSession;
32 import org.apache.mina.transport.nio.NioTcpClient;
33 import org.apache.mina.transport.nio.NioTcpServer;
34 import org.junit.Test;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43 public class NioTcpClientReleaseTest {
44
45 private static final Logger LOG = LoggerFactory.getLogger(NioTcpClientReleaseTest.class);
46
47 private static final int CLIENT_COUNT = 10;
48
49 private static final int WAIT_TIME = 30000;
50
51 private final CountDownLatch closedLatch = new CountDownLatch(CLIENT_COUNT);
52
53
54
55
56
57 @Test
58 public void checkSessionsAreClosedWhenClientIsDisconnected() throws IOException, InterruptedException,
59 ExecutionException {
60
61 NioTcpServer server = new NioTcpServer();
62 server.setIoHandler(new Handler());
63 server.bind(0);
64
65 NioTcpClient client = new NioTcpClient();
66 client.setIoHandler(new AbstractIoHandler() {
67 });
68 for (int i = 0; i < CLIENT_COUNT; ++i) {
69 client.connect(new InetSocketAddress(server.getServerSocketChannel().socket().getLocalPort())).get();
70 }
71 client.disconnect();
72 assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
73 }
74
75 private class Handler extends AbstractIoHandler {
76
77 @Override
78 public void sessionClosed(final IoSession session) {
79 LOG.info("** session closed");
80 closedLatch.countDown();
81 }
82 }
83 }