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) throws Exception {
52              if (status == IdleStatus.BOTH_IDLE) {
53                  bothIdleReceived = true;
54              } else if (status == IdleStatus.READER_IDLE) {
55                  readerIdleReceived = true;
56              } else if (status == IdleStatus.WRITER_IDLE) {
57                  writerIdleReceived = true;
58              }
59  
60              synchronized (mutex) {
61                  mutex.notifyAll();
62              }
63  
64              super.sessionIdle(session, status);
65          }
66      }
67  
68      @Test
69      public void testSessionIdle() throws Exception {
70          final int READER_IDLE_TIME = 3;//seconds
71          final int WRITER_IDLE_TIME = READER_IDLE_TIME + 2;//seconds
72          final int BOTH_IDLE_TIME = WRITER_IDLE_TIME + 2;//seconds
73  
74          NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
75          acceptor.getSessionConfig().setBothIdleTime(BOTH_IDLE_TIME);
76          acceptor.getSessionConfig().setReaderIdleTime(READER_IDLE_TIME);
77          acceptor.getSessionConfig().setWriterIdleTime(WRITER_IDLE_TIME);
78          InetSocketAddress bindAddress = new InetSocketAddress(AvailablePortFinder.getNextAvailable());
79          acceptor.setHandler(new TestHandler());
80          acceptor.bind(bindAddress);
81          IoSession session = acceptor.newSession(
82                  new InetSocketAddress("127.0.0.1", AvailablePortFinder.getNextAvailable()), bindAddress);
83  
84          //check properties to be copied from acceptor to session
85          assertEquals(BOTH_IDLE_TIME, session.getConfig().getBothIdleTime());
86          assertEquals(READER_IDLE_TIME, session.getConfig().getReaderIdleTime());
87          assertEquals(WRITER_IDLE_TIME, session.getConfig().getWriterIdleTime());
88  
89          //verify that IDLE events really received by handler
90          long startTime = System.currentTimeMillis();
91  
92          synchronized (mutex) {
93              while (!readerIdleReceived && (System.currentTimeMillis() - startTime) < (READER_IDLE_TIME + 1) * 1000)
94                  try {
95                      mutex.wait(READER_IDLE_TIME * 1000);
96                  } catch (Exception e) {
97                      e.printStackTrace();
98                  }
99          }
100 
101         assertTrue(readerIdleReceived);
102 
103         synchronized (mutex) {
104             while (!writerIdleReceived && (System.currentTimeMillis() - startTime) < (WRITER_IDLE_TIME + 1) * 1000)
105                 try {
106                     mutex.wait((WRITER_IDLE_TIME - READER_IDLE_TIME) * 1000);
107                 } catch (Exception e) {
108                     e.printStackTrace();
109                 }
110         }
111 
112         assertTrue(writerIdleReceived);
113 
114         synchronized (mutex) {
115             while (!bothIdleReceived && (System.currentTimeMillis() - startTime) < (BOTH_IDLE_TIME + 1) * 1000)
116                 try {
117                     mutex.wait((BOTH_IDLE_TIME - WRITER_IDLE_TIME) * 1000);
118                 } catch (Exception e) {
119                     e.printStackTrace();
120                 }
121         }
122 
123         assertTrue(bothIdleReceived);
124     }
125 }