Better naming / Port socket object echo test
This commit is contained in:
parent
7f96221fe9
commit
49ef5672be
@ -19,7 +19,7 @@ import io.netty.bootstrap.Bootstrap;
|
|||||||
import io.netty.bootstrap.ServerBootstrap;
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
import io.netty.logging.InternalLogger;
|
import io.netty.logging.InternalLogger;
|
||||||
import io.netty.logging.InternalLoggerFactory;
|
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.testsuite.util.TestUtils;
|
||||||
import io.netty.util.SocketAddresses;
|
import io.netty.util.SocketAddresses;
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ import org.junit.rules.TestName;
|
|||||||
public abstract class AbstractSocketTest {
|
public abstract class AbstractSocketTest {
|
||||||
|
|
||||||
private static final List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> COMBO =
|
private static final List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> COMBO =
|
||||||
SocketTestCombination.all();
|
SocketTestPermutation.socket();
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public final TestName testName = new TestName();
|
public final TestName testName = new TestName();
|
||||||
|
@ -16,41 +16,28 @@
|
|||||||
package io.netty.testsuite.transport.socket;
|
package io.netty.testsuite.transport.socket;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import io.netty.bootstrap.ClientBootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.bootstrap.ServerBootstrap;
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelFactory;
|
import io.netty.channel.ChannelInboundHandlerContext;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.ChannelStateEvent;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.ExceptionEvent;
|
|
||||||
import io.netty.channel.MessageEvent;
|
|
||||||
import io.netty.channel.SimpleChannelUpstreamHandler;
|
|
||||||
import io.netty.handler.codec.serialization.ClassResolvers;
|
import io.netty.handler.codec.serialization.ClassResolvers;
|
||||||
import io.netty.handler.codec.serialization.ObjectDecoder;
|
import io.netty.handler.codec.serialization.ObjectDecoder;
|
||||||
import io.netty.handler.codec.serialization.ObjectEncoder;
|
import io.netty.handler.codec.serialization.ObjectEncoder;
|
||||||
import io.netty.util.SocketAddresses;
|
|
||||||
import io.netty.util.internal.ExecutorUtil;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.util.Random;
|
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 java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public abstract class AbstractSocketObjectStreamEchoTest {
|
public class SocketObjectEchoTest extends AbstractSocketTest {
|
||||||
|
|
||||||
static final Random random = new Random();
|
static final Random random = new Random();
|
||||||
static final String[] data = new String[1024];
|
static final String[] data = new String[1024];
|
||||||
|
|
||||||
private static ExecutorService executor;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
for (int i = 0; i < data.length; i ++) {
|
for (int i = 0; i < data.length; i ++) {
|
||||||
int eLen = random.nextInt(512);
|
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
|
@Test
|
||||||
public void testObjectEcho() throws Throwable {
|
public void testObjectEcho() throws Throwable {
|
||||||
ServerBootstrap sb = new ServerBootstrap(newServerSocketChannelFactory(executor));
|
run();
|
||||||
ClientBootstrap cb = new ClientBootstrap(newClientSocketChannelFactory(executor));
|
}
|
||||||
|
|
||||||
EchoHandler sh = new EchoHandler();
|
public void testObjectEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
|
||||||
EchoHandler ch = new EchoHandler();
|
final EchoHandler sh = new EchoHandler();
|
||||||
|
final EchoHandler ch = new EchoHandler();
|
||||||
|
|
||||||
sb.pipeline().addLast("decoder", new ObjectDecoder(
|
sb.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||||
ClassResolvers.cacheDisabled(getClass().getClassLoader())));
|
@Override
|
||||||
sb.pipeline().addLast("encoder", new ObjectEncoder());
|
public void initChannel(SocketChannel sch) throws Exception {
|
||||||
sb.pipeline().addLast("handler", sh);
|
sch.pipeline().addLast(
|
||||||
|
new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
|
||||||
|
new ObjectEncoder(),
|
||||||
|
sh);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
cb.pipeline().addLast("decoder", new ObjectDecoder(
|
cb.handler(new ChannelInitializer<SocketChannel>() {
|
||||||
ClassResolvers.cacheDisabled(getClass().getClassLoader())));
|
@Override
|
||||||
cb.pipeline().addLast("encoder", new ObjectEncoder());
|
public void initChannel(SocketChannel sch) throws Exception {
|
||||||
cb.pipeline().addLast("handler", ch);
|
sch.pipeline().addLast(
|
||||||
|
new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
|
||||||
|
new ObjectEncoder(),
|
||||||
|
ch);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Channel sc = sb.bind(new InetSocketAddress(0));
|
Channel sc = sb.bind().sync().channel();
|
||||||
int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();
|
Channel cc = cb.connect().sync().channel();
|
||||||
|
|
||||||
ChannelFuture ccf = cb.connect(new InetSocketAddress(SocketAddresses.LOCALHOST, port));
|
|
||||||
assertTrue(ccf.awaitUninterruptibly().isSuccess());
|
|
||||||
|
|
||||||
Channel cc = ccf.channel();
|
|
||||||
for (String element : data) {
|
for (String element : data) {
|
||||||
cc.write(element);
|
cc.write(element);
|
||||||
}
|
}
|
||||||
@ -135,9 +115,9 @@ public abstract class AbstractSocketObjectStreamEchoTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sh.channel.close().awaitUninterruptibly();
|
sh.channel.close().sync();
|
||||||
ch.channel.close().awaitUninterruptibly();
|
ch.channel.close().sync();
|
||||||
sc.close().awaitUninterruptibly();
|
sc.close().sync();
|
||||||
|
|
||||||
if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
|
if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
|
||||||
throw sh.exception.get();
|
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;
|
volatile Channel channel;
|
||||||
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
|
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
|
||||||
volatile int counter;
|
volatile int counter;
|
||||||
@ -162,30 +142,28 @@ public abstract class AbstractSocketObjectStreamEchoTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
|
public void channelActive(ChannelInboundHandlerContext<String> ctx)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
channel = e.channel();
|
channel = ctx.channel();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
public void messageReceived(ChannelInboundHandlerContext<String> ctx,
|
||||||
throws Exception {
|
String msg) throws Exception {
|
||||||
|
assertEquals(data[counter], msg);
|
||||||
|
|
||||||
String m = (String) e.getMessage();
|
if (channel.parent() != null) {
|
||||||
assertEquals(data[counter], m);
|
channel.write(msg);
|
||||||
|
|
||||||
if (channel.getParent() != null) {
|
|
||||||
channel.write(m);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
counter ++;
|
counter ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
|
public void exceptionCaught(ChannelInboundHandlerContext<String> ctx,
|
||||||
throws Exception {
|
Throwable cause) throws Exception {
|
||||||
if (exception.compareAndSet(null, e.cause())) {
|
if (exception.compareAndSet(null, cause)) {
|
||||||
e.channel().close();
|
ctx.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -58,8 +58,8 @@ public class SocketStringEchoTest extends AbstractSocketTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testStringEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
|
public void testStringEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
|
||||||
final EchoHandler sh = new EchoHandler();
|
final StringEchoHandler sh = new StringEchoHandler();
|
||||||
final EchoHandler ch = new EchoHandler();
|
final StringEchoHandler ch = new StringEchoHandler();
|
||||||
|
|
||||||
sb.childHandler(new ChannelInitializer<SocketChannel>() {
|
sb.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||||
@Override
|
@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;
|
volatile Channel channel;
|
||||||
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
|
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
|
||||||
volatile int counter;
|
volatile int counter;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelInboundHandlerContext<String> ctx)
|
public void channelActive(ChannelInboundHandlerContext<String> ctx)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
@ -2,26 +2,28 @@ package io.netty.testsuite.transport.socket;
|
|||||||
|
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.bootstrap.ServerBootstrap;
|
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.NioEventLoop;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
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.OioEventLoop;
|
||||||
import io.netty.channel.socket.oio.OioServerSocketChannel;
|
import io.netty.channel.socket.oio.OioServerSocketChannel;
|
||||||
import io.netty.channel.socket.oio.OioSocketChannel;
|
import io.netty.channel.socket.oio.OioSocketChannel;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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() {
|
static List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> socket() {
|
||||||
List<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> list =
|
List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> list =
|
||||||
new ArrayList<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>>();
|
new ArrayList<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>>();
|
||||||
|
|
||||||
// Make the list of ServerBootstrap factories.
|
// Make the list of ServerBootstrap factories.
|
||||||
List<Factory<ServerBootstrap>> sbfs =
|
List<Factory<ServerBootstrap>> sbfs =
|
||||||
new ArrayList<SocketTestCombination.Factory<ServerBootstrap>>();
|
new ArrayList<Factory<ServerBootstrap>>();
|
||||||
sbfs.add(new Factory<ServerBootstrap>() {
|
sbfs.add(new Factory<ServerBootstrap>() {
|
||||||
@Override
|
@Override
|
||||||
public ServerBootstrap newInstance() {
|
public ServerBootstrap newInstance() {
|
||||||
@ -41,7 +43,7 @@ final class SocketTestCombination {
|
|||||||
|
|
||||||
// Make the list of Bootstrap factories.
|
// Make the list of Bootstrap factories.
|
||||||
List<Factory<Bootstrap>> cbfs =
|
List<Factory<Bootstrap>> cbfs =
|
||||||
new ArrayList<SocketTestCombination.Factory<Bootstrap>>();
|
new ArrayList<Factory<Bootstrap>>();
|
||||||
cbfs.add(new Factory<Bootstrap>() {
|
cbfs.add(new Factory<Bootstrap>() {
|
||||||
@Override
|
@Override
|
||||||
public Bootstrap newInstance() {
|
public Bootstrap newInstance() {
|
||||||
@ -60,7 +62,7 @@ final class SocketTestCombination {
|
|||||||
for (Factory<Bootstrap> cbf: cbfs) {
|
for (Factory<Bootstrap> cbf: cbfs) {
|
||||||
final Factory<ServerBootstrap> sbf0 = sbf;
|
final Factory<ServerBootstrap> sbf0 = sbf;
|
||||||
final Factory<Bootstrap> cbf0 = cbf;
|
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
|
@Override
|
||||||
public Factory<ServerBootstrap> getKey() {
|
public Factory<ServerBootstrap> getKey() {
|
||||||
return sbf0;
|
return sbf0;
|
||||||
@ -82,7 +84,53 @@ final class SocketTestCombination {
|
|||||||
return list;
|
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> {
|
static interface Factory<T> {
|
||||||
T newInstance();
|
T newInstance();
|
Loading…
Reference in New Issue
Block a user