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