public
Last active

Netty blocking I/O API test

  • Download Gist
OioServerTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
 
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory;
import org.jboss.netty.util.CharsetUtil;
import org.junit.Test;
 
public class OioServerTest {
@Test
public void testNio() {
ServerBootstrap server = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
 
server.setPipelineFactory(new ChannelPipelineFactory() {
 
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("discard", new DiscardServerHandler());
return pipeline;
}
});
 
Channel serverBind = server.bind(new InetSocketAddress(10000));
 
ClientBootstrap client = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
 
client.setPipelineFactory(new ChannelPipelineFactory() {
 
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
return pipeline;
}
});
 
Channel channel = client.connect(new InetSocketAddress("127.0.0.1", 10000)).awaitUninterruptibly().getChannel();
channel.write(ChannelBuffers.copiedBuffer("foo", CharsetUtil.UTF_8));
channel.close();
client.releaseExternalResources();
//Nio may pass without it. Oio will hang on server.releaseExternalResources() without closing this channel.
serverBind.close().awaitUninterruptibly();
server.releaseExternalResources();
}
 
@Test
public void testOio() {
ServerBootstrap server = new ServerBootstrap(
new OioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
 
server.setPipelineFactory(new ChannelPipelineFactory() {
 
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("discard", new DiscardServerHandler());
return pipeline;
}
});
 
Channel serverBind = server.bind(new InetSocketAddress(10000));
 
ClientBootstrap client = new ClientBootstrap(
new OioClientSocketChannelFactory(
Executors.newCachedThreadPool()));
 
client.setPipelineFactory(new ChannelPipelineFactory() {
 
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("discard", new DiscardServerHandler());
return pipeline;
}
});
 
Channel channel = client.connect(new InetSocketAddress("127.0.0.1", 10000)).awaitUninterruptibly().getChannel();
channel.write(ChannelBuffers.copiedBuffer("foo", CharsetUtil.UTF_8));
channel.close();
client.releaseExternalResources();
//Nio may pass without it. Oio will hang on server.releaseExternalResources() without closing this channel.
serverBind.close().awaitUninterruptibly();
server.releaseExternalResources();
}
 
class DiscardServerHandler extends SimpleChannelUpstreamHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
e.getChannel().close();
}
 
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
e.getChannel().close();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.