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.socket.nio;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.net.InetSocketAddress;
26  
27  import org.apache.mina.core.service.IoHandlerAdapter;
28  import org.apache.mina.core.session.IdleStatus;
29  import org.apache.mina.core.session.IoSession;
30  import org.apache.mina.util.AvailablePortFinder;
31  import org.junit.Test;
32  
33  /**
34   * Test for DIRMINA-732
35   *
36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
37   */
38  public class DatagramSessionIdleTest {
39  
40      private boolean readerIdleReceived;
41  
42      private boolean writerIdleReceived;
43  
44      private boolean bothIdleReceived;
45  
46      private Object mutex = new Object();
47  
48      private class TestHandler extends IoHandlerAdapter {
49  
50          @Override
51          public void sessionIdle(IoSession session, IdleStatus status)
52                  throws Exception {
53              if (status == IdleStatus.BOTH_IDLE) {
54                  bothIdleReceived = true;
55              } else if (status == IdleStatus.READER_IDLE) {
56                  readerIdleReceived = true;
57              } else if (status == IdleStatus.WRITER_IDLE) {
58                  writerIdleReceived = true;
59              }
60              
61              synchronized (mutex) {
62                  mutex.notifyAll();
63              }
64              
65              super.sessionIdle(session, status);
66          }
67      }
68  
69      @Test
70      public void testSessionIdle() throws Exception {
71          final int READER_IDLE_TIME = 3;//seconds
72          final int WRITER_IDLE_TIME = READER_IDLE_TIME + 2;//seconds
73          final int BOTH_IDLE_TIME = WRITER_IDLE_TIME + 2;//seconds
74          
75          NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
76          acceptor.getSessionConfig().setBothIdleTime(BOTH_IDLE_TIME);
77          acceptor.getSessionConfig().setReaderIdleTime(READER_IDLE_TIME);
78          acceptor.getSessionConfig().setWriterIdleTime(WRITER_IDLE_TIME);
79          InetSocketAddress bindAddress = new InetSocketAddress(  AvailablePortFinder.getNextAvailable());
80          acceptor.setHandler(new TestHandler());
81          acceptor.bind(bindAddress);
82          IoSession session = acceptor.newSession(new InetSocketAddress(
83                  "127.0.0.1", AvailablePortFinder.getNextAvailable()), bindAddress);
84          
85          //check properties to be copied from acceptor to session
86          assertEquals(BOTH_IDLE_TIME, session.getConfig().getBothIdleTime());
87          assertEquals(READER_IDLE_TIME, session.getConfig().getReaderIdleTime());
88          assertEquals(WRITER_IDLE_TIME, session.getConfig().getWriterIdleTime());
89          
90          //verify that IDLE events really received by handler
91          long startTime = System.currentTimeMillis();
92          
93          synchronized (mutex) {
94              while (!readerIdleReceived
95                      && (System.currentTimeMillis() - startTime) < (READER_IDLE_TIME + 1) * 1000)
96                  try {
97                      mutex.wait(READER_IDLE_TIME * 1000);
98                  } catch (Exception e) {
99                      e.printStackTrace();
100                 }
101         }
102         
103         assertTrue(readerIdleReceived);
104         
105         synchronized (mutex) {
106             while (!writerIdleReceived
107                     && (System.currentTimeMillis() - startTime) < (WRITER_IDLE_TIME + 1) * 1000)
108                 try {
109                     mutex.wait((WRITER_IDLE_TIME - READER_IDLE_TIME) * 1000);
110                 } catch (Exception e) {
111                     e.printStackTrace();
112                 }
113         }
114         
115         assertTrue(writerIdleReceived);
116         
117         synchronized (mutex) {
118             while (!bothIdleReceived
119                     && (System.currentTimeMillis() - startTime) < (BOTH_IDLE_TIME + 1) * 1000)
120                 try {
121                     mutex.wait((BOTH_IDLE_TIME - WRITER_IDLE_TIME) * 1000);
122                 } catch (Exception e) {
123                     e.printStackTrace();
124                 }
125         }
126         
127         assertTrue(bothIdleReceived);
128     }
129 }