Better naming / Port socket object echo test

This commit is contained in:
Trustin Lee 2012-06-03 02:16:49 -07:00
parent 7f96221fe9
commit 49ef5672be
4 changed files with 108 additions and 83 deletions

View File

@ -19,7 +19,7 @@ import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
import io.netty.testsuite.transport.socket.SocketTestCombination.Factory;
import io.netty.testsuite.transport.socket.SocketTestPermutation.Factory;
import io.netty.testsuite.util.TestUtils;
import io.netty.util.SocketAddresses;
@ -34,7 +34,7 @@ import org.junit.rules.TestName;
public abstract class AbstractSocketTest {
private static final List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> COMBO =
SocketTestCombination.all();
SocketTestPermutation.socket();
@Rule
public final TestName testName = new TestName();

View File

@ -16,41 +16,28 @@
package io.netty.testsuite.transport.socket;
import static org.junit.Assert.*;
import io.netty.bootstrap.ClientBootstrap;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFactory;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelStateEvent;
import io.netty.channel.ExceptionEvent;
import io.netty.channel.MessageEvent;
import io.netty.channel.SimpleChannelUpstreamHandler;
import io.netty.channel.ChannelInboundHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.util.SocketAddresses;
import io.netty.util.internal.ExecutorUtil;
import java.io.IOException;
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.atomic.AtomicReference;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public abstract class AbstractSocketObjectStreamEchoTest {
public class SocketObjectEchoTest extends AbstractSocketTest {
static final Random random = new Random();
static final String[] data = new String[1024];
private static ExecutorService executor;
static {
for (int i = 0; i < data.length; i ++) {
int eLen = random.nextInt(512);
@ -63,44 +50,37 @@ public abstract class AbstractSocketObjectStreamEchoTest {
}
}
@BeforeClass
public static void init() {
executor = Executors.newCachedThreadPool();
}
@AfterClass
public static void destroy() {
ExecutorUtil.terminate(executor);
}
protected abstract ChannelFactory newServerSocketChannelFactory(Executor executor);
protected abstract ChannelFactory newClientSocketChannelFactory(Executor executor);
@Test
public void testObjectEcho() throws Throwable {
ServerBootstrap sb = new ServerBootstrap(newServerSocketChannelFactory(executor));
ClientBootstrap cb = new ClientBootstrap(newClientSocketChannelFactory(executor));
run();
}
EchoHandler sh = new EchoHandler();
EchoHandler ch = new EchoHandler();
public void testObjectEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
final EchoHandler sh = new EchoHandler();
final EchoHandler ch = new EchoHandler();
sb.pipeline().addLast("decoder", new ObjectDecoder(
ClassResolvers.cacheDisabled(getClass().getClassLoader())));
sb.pipeline().addLast("encoder", new ObjectEncoder());
sb.pipeline().addLast("handler", sh);
sb.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel sch) throws Exception {
sch.pipeline().addLast(
new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
new ObjectEncoder(),
sh);
}
});
cb.pipeline().addLast("decoder", new ObjectDecoder(
ClassResolvers.cacheDisabled(getClass().getClassLoader())));
cb.pipeline().addLast("encoder", new ObjectEncoder());
cb.pipeline().addLast("handler", ch);
cb.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel sch) throws Exception {
sch.pipeline().addLast(
new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
new ObjectEncoder(),
ch);
}
});
Channel sc = sb.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();
ChannelFuture ccf = cb.connect(new InetSocketAddress(SocketAddresses.LOCALHOST, port));
assertTrue(ccf.awaitUninterruptibly().isSuccess());
Channel cc = ccf.channel();
Channel sc = sb.bind().sync().channel();
Channel cc = cb.connect().sync().channel();
for (String element : data) {
cc.write(element);
}
@ -135,9 +115,9 @@ public abstract class AbstractSocketObjectStreamEchoTest {
}
}
sh.channel.close().awaitUninterruptibly();
ch.channel.close().awaitUninterruptibly();
sc.close().awaitUninterruptibly();
sh.channel.close().sync();
ch.channel.close().sync();
sc.close().sync();
if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
throw sh.exception.get();
@ -153,7 +133,7 @@ public abstract class AbstractSocketObjectStreamEchoTest {
}
}
private static class EchoHandler extends SimpleChannelUpstreamHandler {
private static class EchoHandler extends ChannelInboundMessageHandlerAdapter<String> {
volatile Channel channel;
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
volatile int counter;
@ -162,30 +142,28 @@ public abstract class AbstractSocketObjectStreamEchoTest {
}
@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
public void channelActive(ChannelInboundHandlerContext<String> ctx)
throws Exception {
channel = e.channel();
channel = ctx.channel();
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
public void messageReceived(ChannelInboundHandlerContext<String> ctx,
String msg) throws Exception {
assertEquals(data[counter], msg);
String m = (String) e.getMessage();
assertEquals(data[counter], m);
if (channel.getParent() != null) {
channel.write(m);
if (channel.parent() != null) {
channel.write(msg);
}
counter ++;
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
if (exception.compareAndSet(null, e.cause())) {
e.channel().close();
public void exceptionCaught(ChannelInboundHandlerContext<String> ctx,
Throwable cause) throws Exception {
if (exception.compareAndSet(null, cause)) {
ctx.close();
}
}
}

View File

@ -58,8 +58,8 @@ public class SocketStringEchoTest extends AbstractSocketTest {
}
public void testStringEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
final EchoHandler sh = new EchoHandler();
final EchoHandler ch = new EchoHandler();
final StringEchoHandler sh = new StringEchoHandler();
final StringEchoHandler ch = new StringEchoHandler();
sb.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
@ -135,12 +135,11 @@ public class SocketStringEchoTest extends AbstractSocketTest {
}
}
private static class EchoHandler extends ChannelInboundMessageHandlerAdapter<String> {
static class StringEchoHandler extends ChannelInboundMessageHandlerAdapter<String> {
volatile Channel channel;
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
volatile int counter;
@Override
public void channelActive(ChannelInboundHandlerContext<String> ctx)
throws Exception {

View File

@ -2,26 +2,28 @@ package io.netty.testsuite.transport.socket;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioEventLoop;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.oio.OioDatagramChannel;
import io.netty.channel.socket.oio.OioEventLoop;
import io.netty.channel.socket.oio.OioServerSocketChannel;
import io.netty.channel.socket.oio.OioSocketChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
final class SocketTestCombination {
final class SocketTestPermutation {
static List<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> all() {
List<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> list =
new ArrayList<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>>();
static List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> socket() {
List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> list =
new ArrayList<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>>();
// Make the list of ServerBootstrap factories.
List<Factory<ServerBootstrap>> sbfs =
new ArrayList<SocketTestCombination.Factory<ServerBootstrap>>();
new ArrayList<Factory<ServerBootstrap>>();
sbfs.add(new Factory<ServerBootstrap>() {
@Override
public ServerBootstrap newInstance() {
@ -41,7 +43,7 @@ final class SocketTestCombination {
// Make the list of Bootstrap factories.
List<Factory<Bootstrap>> cbfs =
new ArrayList<SocketTestCombination.Factory<Bootstrap>>();
new ArrayList<Factory<Bootstrap>>();
cbfs.add(new Factory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
@ -60,7 +62,7 @@ final class SocketTestCombination {
for (Factory<Bootstrap> cbf: cbfs) {
final Factory<ServerBootstrap> sbf0 = sbf;
final Factory<Bootstrap> cbf0 = cbf;
list.add(new Map.Entry<SocketTestCombination.Factory<ServerBootstrap>, SocketTestCombination.Factory<Bootstrap>>() {
list.add(new Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>() {
@Override
public Factory<ServerBootstrap> getKey() {
return sbf0;
@ -82,7 +84,53 @@ final class SocketTestCombination {
return list;
}
private SocketTestCombination() {}
static List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> datagram() {
List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> list =
new ArrayList<Entry<Factory<Bootstrap>, Factory<Bootstrap>>>();
// Make the list of Bootstrap factories.
List<Factory<Bootstrap>> bfs =
new ArrayList<Factory<Bootstrap>>();
bfs.add(new Factory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().eventLoop(new NioEventLoop()).channel(new NioDatagramChannel());
}
});
bfs.add(new Factory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().eventLoop(new OioEventLoop()).channel(new OioDatagramChannel());
}
});
// Populate the combinations
for (Factory<Bootstrap> sbf: bfs) {
for (Factory<Bootstrap> cbf: bfs) {
final Factory<Bootstrap> sbf0 = sbf;
final Factory<Bootstrap> cbf0 = cbf;
list.add(new Entry<Factory<Bootstrap>, Factory<Bootstrap>>() {
@Override
public Factory<Bootstrap> getKey() {
return sbf0;
}
@Override
public Factory<Bootstrap> getValue() {
return cbf0;
}
@Override
public Factory<Bootstrap> setValue(Factory<Bootstrap> value) {
throw new UnsupportedOperationException();
}
});
}
}
return list;
}
private SocketTestPermutation() {}
static interface Factory<T> {
T newInstance();