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(
57                  new AbstractPollingIoProcessor<NioSession>(ex) {
58  
59                      private NioProcessor proc = new NioProcessor(ex);
60  
61                      @Override
62                      protected Iterator<NioSession> allSessions() {
63                          return proc.allSessions();
64                      }
65  
66                      @Override
67                      protected void destroy(NioSession session) throws Exception {
68                          proc.destroy(session);
69                      }
70  
71                      @Override
72                      protected void dispose0() throws Exception {
73                          proc.dispose0();
74                      }
75  
76                      @Override
77                      protected void init(NioSession session) throws Exception {
78                          proc.init(session);
79                      }
80  
81                      @Override
82                      protected boolean isInterestedInRead(NioSession session) {
83                          return proc.isInterestedInRead(session);
84                      }
85  
86                      @Override
87                      protected boolean isInterestedInWrite(NioSession session) {
88                          return proc.isInterestedInWrite(session);
89                      }
90  
91                      @Override
92                      protected boolean isReadable(NioSession session) {
93                          return proc.isReadable(session);
94                      }
95  
96                      @Override
97                      protected boolean isSelectorEmpty() {
98                          return proc.isSelectorEmpty();
99                      }
100 
101                     @Override
102                     protected boolean isWritable(NioSession session) {
103                         return proc.isWritable(session);
104                     }
105 
106                     @Override
107                     protected int read(NioSession session, IoBuffer buf)
108                             throws Exception {
109                         return proc.read(session, buf);
110                     }
111 
112                     @Override
113                     protected int select(long timeout) throws Exception {
114                         return proc.select(timeout);
115                     }
116 
117                     @Override
118                     protected int select() throws Exception {
119                         return proc.select();
120                     }
121 
122                     @Override
123                     protected Iterator<NioSession> selectedSessions() {
124                         return proc.selectedSessions();
125                     }
126 
127                     @Override
128                     protected void setInterestedInRead(NioSession session,
129                             boolean interested) throws Exception {
130                         proc.setInterestedInRead(session, interested);
131                     }
132 
133                     @Override
134                     protected void setInterestedInWrite(NioSession session,
135                             boolean interested) throws Exception {
136                         proc.setInterestedInWrite(session, interested);
137                     }
138 
139                     @Override
140                     protected SessionState getState(NioSession session) {
141                         return proc.getState(session);
142                     }
143 
144                     @Override
145                     protected int transferFile(NioSession session,
146                             FileRegion region, int length) throws Exception {
147                         return proc.transferFile(session, region, length);
148                     }
149 
150                     @Override
151                     protected void wakeup() {
152                         proc.wakeup();
153                     }
154 
155                     @Override
156                     protected int write(NioSession session, IoBuffer buf,
157                             int length) throws Exception {
158                         throw new NoRouteToHostException(
159                                 "No Route To Host Test");
160                     }
161 
162                     @Override
163                     protected boolean isBrokenConnection() throws IOException {
164                         return proc.isBrokenConnection();
165                     }
166 
167                     @Override
168                     protected void registerNewSelector() throws IOException {
169                         proc.registerNewSelector();
170                     }
171 
172                 });
173         connector.setHandler(new IoHandlerAdapter());
174 
175         IoAcceptor acceptor = new NioSocketAcceptor();
176         acceptor.setHandler(new IoHandlerAdapter());
177 
178         InetSocketAddress addr = new InetSocketAddress("localhost",
179                 AvailablePortFinder.getNextAvailable(20000));
180 
181         acceptor.bind(addr);
182         ConnectFuture future = connector.connect(addr);
183         future.awaitUninterruptibly();
184         IoSession session = future.getSession();
185         WriteFuture wf = session.write(IoBuffer.allocate(1))
186                 .awaitUninterruptibly();
187         assertNotNull(wf.getException());
188 
189         connector.dispose();
190         acceptor.dispose();
191     }
192 }