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.InetAddress;
26  import java.net.InetSocketAddress;
27  
28  import org.apache.mina.core.service.IoHandlerAdapter;
29  import org.apache.mina.core.session.IdleStatus;
30  import org.apache.mina.core.session.IoSession;
31  import org.apache.mina.util.AvailablePortFinder;
32  import org.junit.Test;
33  
34  /**
35   * Test for DIRMINA-732
36   *
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   */
39  public class DatagramSessionIdleTest {
40  
41      private boolean readerIdleReceived;
42  
43      private boolean writerIdleReceived;
44  
45      private boolean bothIdleReceived;
46  
47      private Object mutex = new Object();
48  
49      private class TestHandler extends IoHandlerAdapter {
50  
51          @Override
52          public void sessionIdle(IoSession session, IdleStatus status) 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(
83                  new InetSocketAddress(InetAddress.getByName(null), 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 && (System.currentTimeMillis() - startTime) < (READER_IDLE_TIME + 1) * 1000)
95                  try {
96                      mutex.wait(READER_IDLE_TIME * 1000);
97                  } catch (Exception e) {
98                      e.printStackTrace();
99                  }
100         }
101 
102         assertTrue(readerIdleReceived);
103 
104         synchronized (mutex) {
105             while (!writerIdleReceived && (System.currentTimeMillis() - startTime) < (WRITER_IDLE_TIME + 1) * 1000)
106                 try {
107                     mutex.wait((WRITER_IDLE_TIME - READER_IDLE_TIME) * 1000);
108                 } catch (Exception e) {
109                     e.printStackTrace();
110                 }
111         }
112 
113         assertTrue(writerIdleReceived);
114 
115         synchronized (mutex) {
116             while (!bothIdleReceived && (System.currentTimeMillis() - startTime) < (BOTH_IDLE_TIME + 1) * 1000)
117                 try {
118                     mutex.wait((BOTH_IDLE_TIME - WRITER_IDLE_TIME) * 1000);
119                 } catch (Exception e) {
120                     e.printStackTrace();
121                 }
122         }
123 
124         assertTrue(bothIdleReceived);
125     }
126 }