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.*;
    23
    24  import java.io.IOException;
    25  import java.net.InetSocketAddress;
    26  import java.net.Socket;
    27  import java.util.concurrent.CountDownLatch;
    28  import java.util.concurrent.TimeUnit;
    29
    30  import org.apache.mina.api.AbstractIoFilter;
    31  import org.apache.mina.api.IdleStatus;
    32  import org.apache.mina.api.IoSession;
    33  import org.apache.mina.transport.nio.NioTcpServer;
    34  import org.junit.BeforeClass;
    35  import org.junit.Test;
    36
    37  /**
    38   * Run a TCP server and wait for idle events to be generated.
    39   *
    40   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    41   */
    42  public class IdleTcpServerTest {
    43
    44      private static final int CLIENT_COUNT = 3;
    45
    46      @BeforeClass
    47      public static void setup() {
    48          // BasicConfigurator.configure();
    49      }
    50
    51      @Test
    52      public void readIdleTest() throws IOException {
    53          final NioTcpServer server = new NioTcpServer();
    54
    55          final CountDownLatch idleLatch = new CountDownLatch(CLIENT_COUNT);
    56
    57          // 3 seconds idle time
    58          server.getSessionConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 2000);
    59
    60          // start the server
    61          server.bind(new InetSocketAddress(0));
    62
    63          final int boundPort = server.getServerSocketChannel().socket().getLocalPort();
    64          server.setFilters(new IdleHandler(idleLatch));
    65
    66          // fire the clients and let them idle
    67          final Socket[] clients = new Socket[CLIENT_COUNT];
    68
    69          for (int i = 0; i < CLIENT_COUNT; i++) {
    70              clients[i] = new Socket("127.0.0.1", boundPort);
    71          }
    72
    73          long start = System.currentTimeMillis();
    74          try {
    75              assertTrue("idle event missing ! ", idleLatch.await(4, TimeUnit.SECONDS));
    76              System.err.println((System.currentTimeMillis() - start));
    77              assertTrue(2000 <= (System.currentTimeMillis() - start));
    78          } catch (final InterruptedException e) {
    79              fail(e.getMessage());
    80          }
    81      }
    82
    83      private class IdleHandler extends AbstractIoFilter {
    84
    85          private final CountDownLatch latch;
    86
    87          public IdleHandler(final CountDownLatch latch) {
    88              this.latch = latch;
    89          }
    90
    91          @Override
    92          public void sessionIdle(final IoSession session, final IdleStatus status) {
    93              if (status == IdleStatus.READ_IDLE) {
    94                  // happy
    95                  latch.countDown();
    96                  session.close(true);
    97              }
    98          }
    99      }
    100 }