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 java.net.InetSocketAddress;
23  
24  import junit.framework.Assert;
25  import junit.framework.TestCase;
26  
27  import org.apache.mina.common.ByteBuffer;
28  import org.apache.mina.common.ConnectFuture;
29  import org.apache.mina.common.ExpiringSessionRecycler;
30  import org.apache.mina.common.IdleStatus;
31  import org.apache.mina.common.IoAcceptor;
32  import org.apache.mina.common.IoConnector;
33  import org.apache.mina.common.IoHandlerAdapter;
34  import org.apache.mina.common.IoSession;
35  import org.apache.mina.util.AvailablePortFinder;
36  
37  /**
38   * Tests if datagram sessions are recycled properly.
39   * 
40   * @author The Apache Directory Project (mina-dev@directory.apache.org)
41   * @version $Rev: 436993 $, $Date: 2006-08-26 07:36:56 +0900 (토, 26  8월 2006) $ 
42   */
43  public class DatagramRecyclerTest extends TestCase {
44      private final IoAcceptor acceptor = new DatagramAcceptor();
45  
46      private final IoConnector connector = new DatagramConnector();
47  
48      public DatagramRecyclerTest() {
49      }
50  
51      public void testDatagramRecycler() throws Exception {
52          int port = AvailablePortFinder.getNextAvailable(1024);
53          DatagramAcceptorConfig config = new DatagramAcceptorConfig();
54          ExpiringSessionRecycler recycler = new ExpiringSessionRecycler(1, 1);
55          config.setSessionRecycler(recycler);
56  
57          MockHandler acceptorHandler = new MockHandler();
58          MockHandler connectorHandler = new MockHandler();
59  
60          acceptor.bind(new InetSocketAddress(port), acceptorHandler, config);
61  
62          try {
63              ConnectFuture future = connector.connect(new InetSocketAddress(
64                      "localhost", port), connectorHandler, config);
65              future.join();
66  
67              // Write whatever to trigger the acceptor.
68              future.getSession().write(ByteBuffer.allocate(1)).join();
69  
70              // Wait until the connection is closed.
71              future.getSession().getCloseFuture().join(3000);
72              Assert.assertTrue(future.getSession().getCloseFuture().isClosed());
73              acceptorHandler.session.getCloseFuture().join(3000);
74              Assert.assertTrue(acceptorHandler.session.getCloseFuture()
75                      .isClosed());
76  
77              Thread.sleep(1000);
78  
79              Assert.assertEquals("CROPSECL", connectorHandler.result);
80              Assert.assertEquals("CROPRECL", acceptorHandler.result);
81          } finally {
82              acceptor.unbind(new InetSocketAddress(port));
83          }
84      }
85  
86      private class MockHandler extends IoHandlerAdapter {
87          public IoSession session;
88  
89          public String result = "";
90  
91          public void exceptionCaught(IoSession session, Throwable cause)
92                  throws Exception {
93              this.session = session;
94              result += "CA";
95          }
96  
97          public void messageReceived(IoSession session, Object message)
98                  throws Exception {
99              this.session = session;
100             result += "RE";
101         }
102 
103         public void messageSent(IoSession session, Object message)
104                 throws Exception {
105             this.session = session;
106             result += "SE";
107         }
108 
109         public void sessionClosed(IoSession session) throws Exception {
110             this.session = session;
111             result += "CL";
112         }
113 
114         public void sessionCreated(IoSession session) throws Exception {
115             this.session = session;
116             result += "CR";
117         }
118 
119         public void sessionIdle(IoSession session, IdleStatus status)
120                 throws Exception {
121             this.session = session;
122             result += "ID";
123         }
124 
125         public void sessionOpened(IoSession session) throws Exception {
126             this.session = session;
127             result += "OP";
128         }
129 
130     }
131 }