1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.transport.socket.nio;
21
22 import static org.junit.Assert.assertNotNull;
23
24 import java.net.InetSocketAddress;
25 import java.net.NoRouteToHostException;
26 import java.util.Iterator;
27 import java.util.concurrent.Executor;
28 import java.util.concurrent.Executors;
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 import org.junit.Ignore;
42 import org.junit.Test;
43
44
45
46
47
48
49 public class PollingIoProcessorTest {
50 @Ignore
51 @Test
52 public void testExceptionOnWrite() throws Exception {
53 final Executor ex = Executors.newFixedThreadPool(1);
54
55 IoConnector connector = new NioSocketConnector(new AbstractPollingIoProcessor<NioSession>(ex) {
56
57 private NioProcessor proc = new NioProcessor(ex);
58
59 @Override
60 protected Iterator<NioSession> allSessions() {
61 return proc.allSessions();
62 }
63
64 @Override
65 protected void destroy(NioSession session) throws Exception {
66 proc.destroy(session);
67 }
68
69 @Override
70 protected void doDispose() throws Exception {
71 proc.doDispose();
72 }
73
74 @Override
75 protected void init(NioSession session) throws Exception {
76 proc.init(session);
77 }
78
79 @Override
80 protected boolean isInterestedInRead(NioSession session) {
81 return proc.isInterestedInRead(session);
82 }
83
84 @Override
85 protected boolean isInterestedInWrite(NioSession session) {
86 return proc.isInterestedInWrite(session);
87 }
88
89 @Override
90 protected boolean isReadable(NioSession session) {
91 return proc.isReadable(session);
92 }
93
94 @Override
95 protected boolean isSelectorEmpty() {
96 return proc.isSelectorEmpty();
97 }
98
99 @Override
100 protected boolean isWritable(NioSession session) {
101 return proc.isWritable(session);
102 }
103
104 @Override
105 protected int read(NioSession session, IoBuffer buf) throws Exception {
106 return proc.read(session, buf);
107 }
108
109 @Override
110 protected int select(long timeout) throws Exception {
111 return proc.select(timeout);
112 }
113
114 @Override
115 protected int select() throws Exception {
116 return proc.select();
117 }
118
119 @Override
120 protected Iterator<NioSession> selectedSessions() {
121 return proc.selectedSessions();
122 }
123
124 @Override
125 protected void setInterestedInRead(NioSession session, boolean interested) throws Exception {
126 proc.setInterestedInRead(session, interested);
127 }
128
129 @Override
130 protected void setInterestedInWrite(NioSession session, boolean interested) throws Exception {
131 proc.setInterestedInWrite(session, interested);
132 }
133
134 @Override
135 protected SessionState getState(NioSession session) {
136 return proc.getState(session);
137 }
138
139 @Override
140 protected int transferFile(NioSession session, FileRegion region, int length) throws Exception {
141 return proc.transferFile(session, region, length);
142 }
143
144 @Override
145 protected void wakeup() {
146 proc.wakeup();
147 }
148
149 @Override
150 protected int write(NioSession session, IoBuffer buf, int length) throws Exception {
151 throw new NoRouteToHostException("No Route To Host Test");
152 }
153 });
154 connector.setHandler(new IoHandlerAdapter());
155
156 IoAcceptor acceptor = new NioSocketAcceptor();
157 acceptor.setHandler(new IoHandlerAdapter());
158
159 InetSocketAddress addr = new InetSocketAddress("localhost", AvailablePortFinder.getNextAvailable(20000));
160
161 acceptor.bind(addr);
162 ConnectFuture future = connector.connect(addr);
163 future.awaitUninterruptibly();
164 IoSession session = future.getSession();
165 WriteFuture wf = session.write(IoBuffer.allocate(1)).awaitUninterruptibly();
166 assertNotNull(wf.getException());
167
168 connector.dispose();
169 acceptor.dispose();
170 }
171 }