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