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.assertNotNull;
    23
    24  import java.io.IOException;
    25  import java.net.InetSocketAddress;
    26  import java.net.NoRouteToHostException;
    27  import java.util.Iterator;
    28  import java.util.concurrent.Executor;
    29  import java.util.concurrent.Executors;
    30
    31  import org.apache.mina.core.buffer.IoBuffer;
    32  import org.apache.mina.core.file.FileRegion;
    33  import org.apache.mina.core.future.ConnectFuture;
    34  import org.apache.mina.core.future.WriteFuture;
    35  import org.apache.mina.core.polling.AbstractPollingIoProcessor;
    36  import org.apache.mina.core.service.IoAcceptor;
    37  import org.apache.mina.core.service.IoConnector;
    38  import org.apache.mina.core.service.IoHandlerAdapter;
    39  import org.apache.mina.core.session.IoSession;
    40  import org.apache.mina.core.session.SessionState;
    41  import org.apache.mina.util.AvailablePortFinder;
    42  import org.junit.Ignore;
    43  import org.junit.Test;
    44
    45  /**
    46   * Tests non regression on issue DIRMINA-632.
    47   *
    48   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    49   */
    50  public class PollingIoProcessorTest {
    51      @Ignore
    52      @Test
    53      public void testExceptionOnWrite() throws Exception {
    54          final Executor ex = Executors.newFixedThreadPool(1);
    55
    56          IoConnector connector = new NioSocketConnector(new AbstractPollingIoProcessor<NioSession>(ex) {
    57
    58              private NioProcessor proc = new NioProcessor(ex);
    59
    60              @Override
    61              protected Iterator<NioSession> allSessions() {
    62                  return proc.allSessions();
    63              }
    64
    65              @Override
    66              protected void destroy(NioSession session) throws Exception {
    67                  proc.destroy(session);
    68              }
    69
    70              @Override
    71              protected void doDispose() throws Exception {
    72                  proc.doDispose();
    73              }
    74
    75              @Override
    76              protected void init(NioSession session) throws Exception {
    77                  proc.init(session);
    78              }
    79
    80              @Override
    81              protected boolean isInterestedInRead(NioSession session) {
    82                  return proc.isInterestedInRead(session);
    83              }
    84
    85              @Override
    86              protected boolean isInterestedInWrite(NioSession session) {
    87                  return proc.isInterestedInWrite(session);
    88              }
    89
    90              @Override
    91              protected boolean isReadable(NioSession session) {
    92                  return proc.isReadable(session);
    93              }
    94
    95              @Override
    96              protected boolean isSelectorEmpty() {
    97                  return proc.isSelectorEmpty();
    98              }
    99
    100             @Override
    101             protected boolean isWritable(NioSession session) {
    102                 return proc.isWritable(session);
    103             }
    104
    105             @Override
    106             protected int read(NioSession session, IoBuffer buf) throws Exception {
    107                 return proc.read(session, buf);
    108             }
    109
    110             @Override
    111             protected int select(long timeout) throws Exception {
    112                 return proc.select(timeout);
    113             }
    114
    115             @Override
    116             protected int select() throws Exception {
    117                 return proc.select();
    118             }
    119
    120             @Override
    121             protected Iterator<NioSession> selectedSessions() {
    122                 return proc.selectedSessions();
    123             }
    124
    125             @Override
    126             protected void setInterestedInRead(NioSession session, boolean interested) throws Exception {
    127                 proc.setInterestedInRead(session, interested);
    128             }
    129
    130             @Override
    131             protected void setInterestedInWrite(NioSession session, boolean interested) throws Exception {
    132                 proc.setInterestedInWrite(session, interested);
    133             }
    134
    135             @Override
    136             protected SessionState getState(NioSession session) {
    137                 return proc.getState(session);
    138             }
    139
    140             @Override
    141             protected int transferFile(NioSession session, FileRegion region, int length) throws Exception {
    142                 return proc.transferFile(session, region, length);
    143             }
    144
    145             @Override
    146             protected void wakeup() {
    147                 proc.wakeup();
    148             }
    149
    150             @Override
    151             protected int write(NioSession session, IoBuffer buf, int length) throws Exception {
    152                 throw new NoRouteToHostException("No Route To Host Test");
    153             }
    154
    155             @Override
    156             protected boolean isBrokenConnection() throws IOException {
    157                 return proc.isBrokenConnection();
    158             }
    159
    160             @Override
    161             protected void registerNewSelector() throws IOException {
    162                 proc.registerNewSelector();
    163             }
    164         });
    165         connector.setHandler(new IoHandlerAdapter());
    166
    167         IoAcceptor acceptor = new NioSocketAcceptor();
    168         acceptor.setHandler(new IoHandlerAdapter());
    169
    170         InetSocketAddress addr = new InetSocketAddress("localhost", AvailablePortFinder.getNextAvailable(20000));
    171
    172         acceptor.bind(addr);
    173         ConnectFuture future = connector.connect(addr);
    174         future.awaitUninterruptibly();
    175         IoSession session = future.getSession();
    176         WriteFuture wf = session.write(IoBuffer.allocate(1)).awaitUninterruptibly();
    177         assertNotNull(wf.getException());
    178
    179         connector.dispose();
    180         acceptor.dispose();
    181     }
    182 }