001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.transport.socket.nio;
021
022import static org.junit.Assert.assertNotNull;
023
024import java.io.IOException;
025import java.net.InetSocketAddress;
026import java.net.NoRouteToHostException;
027import java.util.Iterator;
028import java.util.concurrent.Executor;
029import java.util.concurrent.Executors;
030
031import org.apache.mina.core.buffer.IoBuffer;
032import org.apache.mina.core.file.FileRegion;
033import org.apache.mina.core.future.ConnectFuture;
034import org.apache.mina.core.future.WriteFuture;
035import org.apache.mina.core.polling.AbstractPollingIoProcessor;
036import org.apache.mina.core.service.IoAcceptor;
037import org.apache.mina.core.service.IoConnector;
038import org.apache.mina.core.service.IoHandlerAdapter;
039import org.apache.mina.core.session.IoSession;
040import org.apache.mina.core.session.SessionState;
041import org.apache.mina.util.AvailablePortFinder;
042import org.junit.Ignore;
043import org.junit.Test;
044
045/**
046 * Tests non regression on issue DIRMINA-632.
047 *
048 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
049 */
050public class PollingIoProcessorTest {
051    @Ignore
052    @Test
053    public void testExceptionOnWrite() throws Exception {
054        final Executor ex = Executors.newFixedThreadPool(1);
055
056        IoConnector connector = new NioSocketConnector(new AbstractPollingIoProcessor<NioSession>(ex) {
057
058            private NioProcessor proc = new NioProcessor(ex);
059
060            @Override
061            protected Iterator<NioSession> allSessions() {
062                return proc.allSessions();
063            }
064
065            @Override
066            protected void destroy(NioSession session) throws Exception {
067                proc.destroy(session);
068            }
069
070            @Override
071            protected void doDispose() throws Exception {
072                proc.doDispose();
073            }
074
075            @Override
076            protected void init(NioSession session) throws Exception {
077                proc.init(session);
078            }
079
080            @Override
081            protected boolean isInterestedInRead(NioSession session) {
082                return proc.isInterestedInRead(session);
083            }
084
085            @Override
086            protected boolean isInterestedInWrite(NioSession session) {
087                return proc.isInterestedInWrite(session);
088            }
089
090            @Override
091            protected boolean isReadable(NioSession session) {
092                return proc.isReadable(session);
093            }
094
095            @Override
096            protected boolean isSelectorEmpty() {
097                return proc.isSelectorEmpty();
098            }
099
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}