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.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import java.net.InetSocketAddress;
26 import java.util.regex.Pattern;
27
28 import org.apache.mina.core.buffer.IoBuffer;
29 import org.apache.mina.core.future.ConnectFuture;
30 import org.apache.mina.core.future.ReadFuture;
31 import org.apache.mina.core.service.IoConnector;
32 import org.apache.mina.core.service.IoHandlerAdapter;
33 import org.apache.mina.core.session.IoSession;
34 import org.apache.mina.util.AvailablePortFinder;
35 import org.junit.Test;
36
37
38
39
40
41
42 public class DIRMINA777Test {
43
44 @Test
45 public void checkReadFuture() throws Throwable {
46 int port = AvailablePortFinder.getNextAvailable(1025);
47 NioSocketAcceptor acceptor = new NioSocketAcceptor();
48 acceptor.setReuseAddress(true);
49 acceptor.setHandler(new IoHandlerAdapter() {
50
51 @Override
52 public void sessionOpened(IoSession session) throws Exception {
53 IoBuffer buffer = IoBuffer.allocate(1);
54 buffer.put((byte) 125);
55 buffer.rewind();
56 session.write(buffer);
57 }
58
59 });
60 acceptor.bind(new InetSocketAddress(port));
61
62 try {
63 IoConnector connector = new NioSocketConnector();
64 connector.setHandler(new IoHandlerAdapter());
65 ConnectFuture connectFuture = connector.connect(new InetSocketAddress("localhost", port));
66 connectFuture.awaitUninterruptibly();
67 if (connectFuture.getException() != null) {
68 throw connectFuture.getException();
69 }
70 connectFuture.getSession().getConfig().setUseReadOperation(true);
71 ReadFuture readFuture = connectFuture.getSession().read();
72 readFuture.awaitUninterruptibly();
73 if (readFuture.getException() != null) {
74 throw readFuture.getException();
75 }
76 IoBuffer message = (IoBuffer)readFuture.getMessage();
77 assertEquals(1, message.remaining());
78 assertEquals(125,message.get());
79 connectFuture.getSession().close(true);
80 } finally {
81 acceptor.dispose();
82 }
83 }
84
85 }