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   *
    21   */
    22  package org.apache.mina.service.idlecheker;
    23
    24  import static org.junit.Assert.assertEquals;
    25  import static org.mockito.Mockito.mock;
    26
    27  import java.net.SocketAddress;
    28
    29  import org.apache.mina.api.IdleStatus;
    30  import org.apache.mina.api.IoFuture;
    31  import org.apache.mina.api.IoService;
    32  import org.apache.mina.api.IoSessionConfig;
    33  import org.apache.mina.service.idlechecker.IdleChecker;
    34  import org.apache.mina.service.idlechecker.IndexedIdleChecker;
    35  import org.apache.mina.session.AbstractIoSession;
    36  import org.apache.mina.session.AbstractIoSessionConfig;
    37  import org.apache.mina.session.WriteRequest;
    38  import org.junit.Test;
    39
    40  /**
    41   * Unit test for {@link IndexedIdleChecker}.
    42   *
    43   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    44   */
    45  public class IndexedIdleChekerTest {
    46
    47      private final IndexedIdleChecker idleChecker = new IndexedIdleChecker();
    48
    49      private final long now = System.currentTimeMillis();
    50
    51      @Test
    52      public void process_on_empty_index() {
    53          assertEquals(0, idleChecker.processIdleSession(now));
    54      }
    55
    56      @Test
    57      public void dont_send_premature_events() {
    58          IoService service = mock(IoService.class);
    59          DummySession session = new DummySession(service, idleChecker);
    60
    61          session.getConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 2000L);
    62
    63          idleChecker.sessionRead(session, now);
    64
    65          // should be idle in 2 second
    66          assertEquals(0, idleChecker.processIdleSession(now));
    67          assertEquals(0, session.readIdleCount);
    68          assertEquals(0, session.writeIdleCount);
    69          assertEquals(1, idleChecker.processIdleSession(now + 3001));
    70
    71      }
    72
    73      @Test
    74      public void read_event() {
    75          IoService service = mock(IoService.class);
    76          DummySession session = new DummySession(service, idleChecker);
    77
    78          session.getConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 1000L);
    79
    80          idleChecker.sessionRead(session, now);
    81
    82          // should be idle in 1 second
    83          assertEquals(0, idleChecker.processIdleSession(now));
    84          assertEquals(0, session.readIdleCount);
    85          assertEquals(0, session.writeIdleCount);
    86          assertEquals(1, idleChecker.processIdleSession(now + 2000));
    87          assertEquals(1, session.readIdleCount);
    88
    89      }
    90
    91      @Test
    92      public void write_event() {
    93          IoService service = mock(IoService.class);
    94          DummySession session = new DummySession(service, idleChecker);
    95
    96          session.getConfig().setIdleTimeInMillis(IdleStatus.WRITE_IDLE, 1000L);
    97
    98          idleChecker.sessionWritten(session, now);
    99
    100         // should be idle in 1 second
    101         assertEquals(1, idleChecker.processIdleSession(now + 12000));
    102         assertEquals(0, session.readIdleCount);
    103         assertEquals(1, session.writeIdleCount);
    104     }
    105
    106     private class DummySession extends AbstractIoSession {
    107
    108         int readIdleCount = 0;
    109
    110         int writeIdleCount = 0;
    111
    112         private DummySession(IoService service, IdleChecker checker) {
    113             super(service, checker);
    114         }
    115
    116         @Override
    117         public IoFuture<Void> close(boolean immediately) {
    118             return null;
    119         }
    120
    121         IoSessionConfig config = new AbstractIoSessionConfig() {
    122         };
    123
    124         @Override
    125         public IoSessionConfig getConfig() {
    126             return config;
    127         }
    128
    129         /**
    130          * {@inheritDoc}
    131          */
    132         @Override
    133         public void processSessionIdle(IdleStatus status) {
    134             if (status == IdleStatus.READ_IDLE) {
    135                 readIdleCount++;
    136             }
    137             if (status == IdleStatus.WRITE_IDLE) {
    138                 writeIdleCount++;
    139             }
    140         }
    141
    142         @Override
    143         public SocketAddress getLocalAddress() {
    144             return null;
    145         }
    146
    147         @Override
    148         public SocketAddress getRemoteAddress() {
    149             return null;
    150         }
    151
    152         @Override
    153         public boolean isConnected() {
    154             return false;
    155         }
    156
    157         @Override
    158         public boolean isReadSuspended() {
    159             return false;
    160         }
    161
    162         @Override
    163         public boolean isWriteSuspended() {
    164             return false;
    165         }
    166
    167         @Override
    168         public void resumeRead() {
    169         }
    170
    171         @Override
    172         public void resumeWrite() {
    173         }
    174
    175         @Override
    176         public void suspendRead() {
    177         }
    178
    179         @Override
    180         public void suspendWrite() {
    181         }
    182
    183         @Override
    184         public boolean isSecuring() {
    185             return false;
    186         }
    187
    188         @Override
    189         public boolean isSecured() {
    190             return false;
    191         }
    192
    193         @Override
    194         public boolean isClosed() {
    195             return false;
    196         }
    197
    198         @Override
    199         public WriteRequest enqueueWriteRequest(WriteRequest writeRequest) {
    200             return null;
    201         }
    202     }
    203 }