From ddbabc98ef08505146d9fde2f723621de2d0a589 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 27 Aug 2008 13:14:28 +0000 Subject: [PATCH] Integration tests (echo test) --- .../socket/oio/OioAcceptedSocketChannel.java | 14 -- .../socket/oio/OioClientSocketChannel.java | 18 -- .../channel/socket/oio/OioSocketChannel.java | 2 - .../socket/AbstractSocketEchoTest.java | 182 ++++++++++++++++++ .../channel/socket/NioNioSocketEchoTest.java | 50 +++++ .../channel/socket/NioOioSocketEchoTest.java | 50 +++++ .../channel/socket/OioNioSocketEchoTest.java | 50 +++++ .../channel/socket/OioOioSocketEchoTest.java | 50 +++++ 8 files changed, 382 insertions(+), 34 deletions(-) create mode 100644 src/test/java/org/jboss/netty/channel/socket/AbstractSocketEchoTest.java create mode 100644 src/test/java/org/jboss/netty/channel/socket/NioNioSocketEchoTest.java create mode 100644 src/test/java/org/jboss/netty/channel/socket/NioOioSocketEchoTest.java create mode 100644 src/test/java/org/jboss/netty/channel/socket/OioNioSocketEchoTest.java create mode 100644 src/test/java/org/jboss/netty/channel/socket/OioOioSocketEchoTest.java diff --git a/src/main/java/org/jboss/netty/channel/socket/oio/OioAcceptedSocketChannel.java b/src/main/java/org/jboss/netty/channel/socket/oio/OioAcceptedSocketChannel.java index 16e2974e8c..93517acbb3 100644 --- a/src/main/java/org/jboss/netty/channel/socket/oio/OioAcceptedSocketChannel.java +++ b/src/main/java/org/jboss/netty/channel/socket/oio/OioAcceptedSocketChannel.java @@ -74,18 +74,4 @@ class OioAcceptedSocketChannel extends OioSocketChannel { OutputStream getOutputStream() { return out; } - - @Override - void setInputStream(PushbackInputStream in) { - if (this.in != in) { - throw new IllegalStateException("Should not reach here."); - } - } - - @Override - void setOutputStream(OutputStream out) { - if (this.out != out) { - throw new IllegalStateException("Should not reach here."); - } - } } diff --git a/src/main/java/org/jboss/netty/channel/socket/oio/OioClientSocketChannel.java b/src/main/java/org/jboss/netty/channel/socket/oio/OioClientSocketChannel.java index 6607dcbdb6..259ebcbecf 100644 --- a/src/main/java/org/jboss/netty/channel/socket/oio/OioClientSocketChannel.java +++ b/src/main/java/org/jboss/netty/channel/socket/oio/OioClientSocketChannel.java @@ -56,22 +56,4 @@ class OioClientSocketChannel extends OioSocketChannel { OutputStream getOutputStream() { return out; } - - @Override - void setInputStream(PushbackInputStream in) { - if (this.in == null) { - this.in = in; - } else if (this.in != in) { - throw new IllegalStateException("Shouldn't reach here."); - } - } - - @Override - void setOutputStream(OutputStream out) { - if (this.out == null) { - this.out = out; - } else if (this.out != out) { - throw new IllegalStateException("Shouldn't reach here."); - } - } } diff --git a/src/main/java/org/jboss/netty/channel/socket/oio/OioSocketChannel.java b/src/main/java/org/jboss/netty/channel/socket/oio/OioSocketChannel.java index c7b9cd289e..4e109e83e2 100644 --- a/src/main/java/org/jboss/netty/channel/socket/oio/OioSocketChannel.java +++ b/src/main/java/org/jboss/netty/channel/socket/oio/OioSocketChannel.java @@ -94,9 +94,7 @@ abstract class OioSocketChannel extends AbstractChannel } abstract PushbackInputStream getInputStream(); - abstract void setInputStream(PushbackInputStream in); abstract OutputStream getOutputStream(); - abstract void setOutputStream(OutputStream out); @Override public ChannelFuture write(Object message, SocketAddress remoteAddress) { diff --git a/src/test/java/org/jboss/netty/channel/socket/AbstractSocketEchoTest.java b/src/test/java/org/jboss/netty/channel/socket/AbstractSocketEchoTest.java new file mode 100644 index 0000000000..e9a509bd0b --- /dev/null +++ b/src/test/java/org/jboss/netty/channel/socket/AbstractSocketEchoTest.java @@ -0,0 +1,182 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.channel.socket; + +import static org.junit.Assert.*; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.Random; +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import org.jboss.netty.bootstrap.ClientBootstrap; +import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelFactory; +import org.jboss.netty.channel.ChannelFuture; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelPipelineCoverage; +import org.jboss.netty.channel.ChannelStateEvent; +import org.jboss.netty.channel.ExceptionEvent; +import org.jboss.netty.channel.MessageEvent; +import org.jboss.netty.channel.SimpleChannelHandler; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + + +/** + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * + * @version $Rev$, $Date$ + * + */ +public abstract class AbstractSocketEchoTest { + + private static final Random random = new Random(); + static final byte[] data = new byte[1048576 * 32]; + + private static ExecutorService executor; + + static { + random.nextBytes(data); + } + + @BeforeClass + public static void init() { + executor = Executors.newCachedThreadPool(); + } + + @AfterClass + public static void destroy() { + executor.shutdown(); + for (;;) { + try { + if (executor.awaitTermination(1, TimeUnit.SECONDS)) { + break; + } + } catch (InterruptedException e) { + // Ignore. + } + } + } + + protected abstract ChannelFactory newServerSocketChannelFactory(Executor executor); + protected abstract ChannelFactory newClientSocketChannelFactory(Executor executor); + + @Test + public void testEcho() throws Throwable { + ServerBootstrap sb = new ServerBootstrap(newServerSocketChannelFactory(executor)); + ClientBootstrap cb = new ClientBootstrap(newClientSocketChannelFactory(executor)); + + EchoHandler sh = new EchoHandler(); + EchoHandler ch = new EchoHandler(); + + sb.getPipeline().addFirst("handler", sh); + cb.getPipeline().addFirst("handler", ch); + + Channel sc = sb.bind(new InetSocketAddress(0)); + int port = ((InetSocketAddress) sc.getLocalAddress()).getPort(); + + ChannelFuture ccf = cb.connect(new InetSocketAddress(InetAddress.getLocalHost(), port)); + ccf.awaitUninterruptibly(); + + Channel cc = ccf.getChannel(); + for (int i = 0; i < data.length;) { + int length = Math.min(random.nextInt(1024 * 512), data.length - i); + cc.write(ChannelBuffers.wrappedBuffer(data, i, length)); + i += length; + } + + while (ch.counter < data.length) { + assertNull(ch.exception); + try { + Thread.sleep(1); + } catch (InterruptedException e) { + // Ignore. + } + } + + ch.channel.close().awaitUninterruptibly(); + + while (sh.counter < data.length) { + assertNull(sh.exception); + try { + Thread.sleep(1); + } catch (InterruptedException e) { + // Ignore. + } + } + + sh.channel.close().awaitUninterruptibly(); + sc.close().awaitUninterruptibly(); + } + + @ChannelPipelineCoverage("one") + private class EchoHandler extends SimpleChannelHandler { + volatile Channel channel; + volatile Throwable exception; + volatile int counter; + + EchoHandler() { + super(); + } + + @Override + public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) + throws Exception { + channel = e.getChannel(); + } + + @Override + public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) + throws Exception { + ChannelBuffer m = (ChannelBuffer) e.getMessage(); + byte[] actual = new byte[m.readableBytes()]; + m.getBytes(0, actual); + + int lastIdx = counter; + for (int i = 0; i < actual.length; i ++) { + assertEquals(data[i + lastIdx], actual[i]); + } + counter += actual.length; + + if (channel.getParent() != null) { + channel.write(m); + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) + throws Exception { + exception = e.getCause(); + e.getChannel().close(); + } + } +} diff --git a/src/test/java/org/jboss/netty/channel/socket/NioNioSocketEchoTest.java b/src/test/java/org/jboss/netty/channel/socket/NioNioSocketEchoTest.java new file mode 100644 index 0000000000..bed3ac86c5 --- /dev/null +++ b/src/test/java/org/jboss/netty/channel/socket/NioNioSocketEchoTest.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.channel.socket; + +import java.util.concurrent.Executor; + +import org.jboss.netty.channel.ChannelFactory; +import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; +import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; + +/** + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * + * @version $Rev$, $Date$ + * + */ +public class NioNioSocketEchoTest extends AbstractSocketEchoTest { + + @Override + protected ChannelFactory newClientSocketChannelFactory(Executor executor) { + return new NioClientSocketChannelFactory(executor, executor); + } + + @Override + protected ChannelFactory newServerSocketChannelFactory(Executor executor) { + return new NioServerSocketChannelFactory(executor, executor); + } + +} diff --git a/src/test/java/org/jboss/netty/channel/socket/NioOioSocketEchoTest.java b/src/test/java/org/jboss/netty/channel/socket/NioOioSocketEchoTest.java new file mode 100644 index 0000000000..6b03133eb6 --- /dev/null +++ b/src/test/java/org/jboss/netty/channel/socket/NioOioSocketEchoTest.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.channel.socket; + +import java.util.concurrent.Executor; + +import org.jboss.netty.channel.ChannelFactory; +import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; +import org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory; + +/** + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * + * @version $Rev$, $Date$ + * + */ +public class NioOioSocketEchoTest extends AbstractSocketEchoTest { + + @Override + protected ChannelFactory newClientSocketChannelFactory(Executor executor) { + return new NioClientSocketChannelFactory(executor, executor); + } + + @Override + protected ChannelFactory newServerSocketChannelFactory(Executor executor) { + return new OioServerSocketChannelFactory(executor, executor); + } + +} diff --git a/src/test/java/org/jboss/netty/channel/socket/OioNioSocketEchoTest.java b/src/test/java/org/jboss/netty/channel/socket/OioNioSocketEchoTest.java new file mode 100644 index 0000000000..6f3e726d90 --- /dev/null +++ b/src/test/java/org/jboss/netty/channel/socket/OioNioSocketEchoTest.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.channel.socket; + +import java.util.concurrent.Executor; + +import org.jboss.netty.channel.ChannelFactory; +import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; +import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory; + +/** + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * + * @version $Rev$, $Date$ + * + */ +public class OioNioSocketEchoTest extends AbstractSocketEchoTest { + + @Override + protected ChannelFactory newClientSocketChannelFactory(Executor executor) { + return new OioClientSocketChannelFactory(executor); + } + + @Override + protected ChannelFactory newServerSocketChannelFactory(Executor executor) { + return new NioServerSocketChannelFactory(executor, executor); + } + +} diff --git a/src/test/java/org/jboss/netty/channel/socket/OioOioSocketEchoTest.java b/src/test/java/org/jboss/netty/channel/socket/OioOioSocketEchoTest.java new file mode 100644 index 0000000000..a2ccc9c53b --- /dev/null +++ b/src/test/java/org/jboss/netty/channel/socket/OioOioSocketEchoTest.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.channel.socket; + +import java.util.concurrent.Executor; + +import org.jboss.netty.channel.ChannelFactory; +import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory; +import org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory; + +/** + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * + * @version $Rev$, $Date$ + * + */ +public class OioOioSocketEchoTest extends AbstractSocketEchoTest { + + @Override + protected ChannelFactory newClientSocketChannelFactory(Executor executor) { + return new OioClientSocketChannelFactory(executor); + } + + @Override + protected ChannelFactory newServerSocketChannelFactory(Executor executor) { + return new OioServerSocketChannelFactory(executor, executor); + } + +}