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  import java.net.PortUnreachableException;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.mina.core.buffer.IoBuffer;
28  import org.apache.mina.core.future.ConnectFuture;
29  import org.apache.mina.core.service.IoConnector;
30  import org.apache.mina.core.service.IoHandlerAdapter;
31  import org.apache.mina.core.session.IoSession;
32  import org.apache.mina.transport.socket.DatagramSessionConfig;
33  import org.apache.mina.util.AvailablePortFinder;
34  
35  /**
36   * Tests {@link DatagramSessionConfig#setCloseOnPortUnreachable(boolean)}.
37   *
38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
39   */
40  public class DatagramPortUnreachableTest extends TestCase {
41  
42      Object mutex = new Object();
43      
44      private void runTest(boolean closeOnPortUnreachable) throws Exception {
45          IoConnector connector = new NioDatagramConnector();
46          connector.setHandler(new IoHandlerAdapter() {
47  
48              @Override
49              public void exceptionCaught(IoSession session, Throwable cause)
50                      throws Exception {
51                  if (cause instanceof PortUnreachableException) {
52                      synchronized(mutex) {
53                          mutex.notify();
54                      }
55                  }
56              }
57              
58          });
59          ConnectFuture future = connector.connect(new InetSocketAddress("localhost", 
60                  AvailablePortFinder.getNextAvailable(20000)));
61          future.awaitUninterruptibly();
62          IoSession session = future.getSession();
63  
64          DatagramSessionConfig cfg = ((DatagramSessionConfig) session
65                  .getConfig());
66          cfg.setUseReadOperation(true);
67          cfg.setCloseOnPortUnreachable(closeOnPortUnreachable);
68          
69          synchronized(mutex) {
70              session.write(IoBuffer.allocate(1)).awaitUninterruptibly().isWritten();
71              session.read();
72              mutex.wait();
73          }
74          
75          Thread.sleep(500);
76          
77          assertEquals(closeOnPortUnreachable, session.isClosing());
78          connector.dispose();
79      }
80  
81      public void testPortUnreachableClosesSession() throws Exception {
82          // session should be closing
83          runTest(true);
84      }
85  
86      public void testNormal() throws Exception {
87          // test that session is not closed on port unreachable exception
88          runTest(false);
89      }
90  }