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