Created
August 2, 2011 17:17
-
-
Save isopov/1120694 to your computer and use it in GitHub Desktop.
Netty blocking I/O API test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment