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) 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", AvailablePortFinder
60                  .getNextAvailable(20000)));
61          future.awaitUninterruptibly();
62          IoSession session = future.getSession();
63  
64          DatagramSessionConfig cfg = ((DatagramSessionConfig) session.getConfig());
65          cfg.setUseReadOperation(true);
66          cfg.setCloseOnPortUnreachable(closeOnPortUnreachable);
67  
68          synchronized (mutex) {
69              session.write(IoBuffer.allocate(1)).awaitUninterruptibly().isWritten();
70              session.read();
71              mutex.wait();
72          }
73  
74          Thread.sleep(500);
75  
76          assertEquals(closeOnPortUnreachable, session.isClosing());
77          connector.dispose();
78      }
79  
80      @Test
81      public void testPortUnreachableClosesSession() throws Exception {
82          // session should be closing
83          runTest(true);
84      }
85  
86      @Test
87      public void testNormal() throws Exception {
88          // test that session is not closed on port unreachable exception
89          runTest(false);
90      }
91  }