View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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   * This class test the resource management of {@link NioTcpClient}.
40   * 
41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
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       * Create an old IO server and use a bunch of MINA client on it. Test if the events occurs correctly in the
55       * different IoFilters.
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  }