Add SocketEchoTest that will simplify a lot of socket testing
- SocketTestCombination generates all possible test combinations of socket transports. - SocketEchoTest iterates over the combinations and runs all tests using reflection.
This commit is contained in:
parent
3b2c25e8ed
commit
955c89fcf1
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2011 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.testsuite.transport.socket;
|
||||
|
||||
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.util.TestUtils;
|
||||
import io.netty.util.SocketAddresses;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.TestName;
|
||||
|
||||
public abstract class AbstractSocketTest {
|
||||
|
||||
private static final List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> COMBO =
|
||||
SocketTestCombination.all();
|
||||
|
||||
@Rule
|
||||
public final TestName testName = new TestName();
|
||||
|
||||
protected final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
|
||||
|
||||
protected volatile ServerBootstrap sb;
|
||||
protected volatile Bootstrap cb;
|
||||
protected volatile InetSocketAddress addr;
|
||||
|
||||
protected void run() throws Exception {
|
||||
int i = 0;
|
||||
for (Entry<Factory<ServerBootstrap>, Factory<Bootstrap>> e: COMBO) {
|
||||
sb = e.getKey().newInstance();
|
||||
cb = e.getValue().newInstance();
|
||||
addr = new InetSocketAddress(
|
||||
SocketAddresses.LOCALHOST, TestUtils.getFreePort());
|
||||
sb.localAddress(addr);
|
||||
cb.remoteAddress(addr);
|
||||
|
||||
logger.info(String.format(
|
||||
"Running: %s %d of %d", testName.getMethodName(), ++ i, COMBO.size()));
|
||||
try {
|
||||
Method m = getClass().getDeclaredMethod(
|
||||
testName.getMethodName(), ServerBootstrap.class, Bootstrap.class);
|
||||
m.invoke(this, sb, cb);
|
||||
} finally {
|
||||
sb.shutdown();
|
||||
cb.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -16,75 +16,44 @@
|
||||
package io.netty.testsuite.transport.socket;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
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 io.netty.bootstrap.ClientBootstrap;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.buffer.ChannelBuffer;
|
||||
import io.netty.buffer.ChannelBuffers;
|
||||
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.util.SocketAddresses;
|
||||
import io.netty.util.internal.ExecutorUtil;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import io.netty.channel.ChannelInboundHandlerContext;
|
||||
import io.netty.channel.ChannelInboundStreamHandlerAdapter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public abstract class AbstractSocketEchoTest {
|
||||
public class SocketEchoTest extends AbstractSocketTest {
|
||||
|
||||
private static final Random random = new Random();
|
||||
static final byte[] data = new byte[1048576];
|
||||
|
||||
private static ExecutorService executor;
|
||||
|
||||
static {
|
||||
random.nextBytes(data);
|
||||
}
|
||||
|
||||
@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 testSimpleEcho() throws Throwable {
|
||||
ServerBootstrap sb = new ServerBootstrap(newServerSocketChannelFactory(executor));
|
||||
ClientBootstrap cb = new ClientBootstrap(newClientSocketChannelFactory(executor));
|
||||
public void testSimpleEcho() throws Exception {
|
||||
run();
|
||||
}
|
||||
|
||||
public void testSimpleEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
|
||||
EchoHandler sh = new EchoHandler();
|
||||
EchoHandler ch = new EchoHandler();
|
||||
|
||||
sb.pipeline().addFirst("handler", sh);
|
||||
cb.pipeline().addFirst("handler", ch);
|
||||
sb.childHandler(sh);
|
||||
cb.handler(ch);
|
||||
|
||||
Channel sc = sb.bind(new InetSocketAddress(0));
|
||||
int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();
|
||||
Channel sc = sb.bind().sync().channel();
|
||||
Channel cc = cb.connect().sync().channel();
|
||||
|
||||
ChannelFuture ccf = cb.connect(new InetSocketAddress(SocketAddresses.LOCALHOST, port));
|
||||
assertTrue(ccf.awaitUninterruptibly().isSuccess());
|
||||
|
||||
Channel cc = ccf.channel();
|
||||
for (int i = 0; i < data.length;) {
|
||||
int length = Math.min(random.nextInt(1024 * 64), data.length - i);
|
||||
cc.write(ChannelBuffers.wrappedBuffer(data, i, length));
|
||||
@ -121,9 +90,9 @@ public abstract class AbstractSocketEchoTest {
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
@ -139,7 +108,7 @@ public abstract class AbstractSocketEchoTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static class EchoHandler extends SimpleChannelUpstreamHandler {
|
||||
private static class EchoHandler extends ChannelInboundStreamHandlerAdapter {
|
||||
volatile Channel channel;
|
||||
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
|
||||
volatile int counter;
|
||||
@ -148,35 +117,35 @@ public abstract class AbstractSocketEchoTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
|
||||
public void channelActive(ChannelInboundHandlerContext<Byte> ctx)
|
||||
throws Exception {
|
||||
channel = e.channel();
|
||||
channel = ctx.channel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
||||
public void inboundBufferUpdated(
|
||||
ChannelInboundHandlerContext<Byte> ctx, ChannelBuffer in)
|
||||
throws Exception {
|
||||
ChannelBuffer m = (ChannelBuffer) e.getMessage();
|
||||
byte[] actual = new byte[m.readableBytes()];
|
||||
m.getBytes(0, actual);
|
||||
byte[] actual = new byte[in.readableBytes()];
|
||||
in.readBytes(actual);
|
||||
|
||||
int lastIdx = counter;
|
||||
for (int i = 0; i < actual.length; i ++) {
|
||||
assertEquals(data[i + lastIdx], actual[i]);
|
||||
}
|
||||
|
||||
if (channel.getParent() != null) {
|
||||
channel.write(m);
|
||||
if (channel.parent() != null) {
|
||||
channel.write(ChannelBuffers.wrappedBuffer(actual));
|
||||
}
|
||||
|
||||
counter += actual.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
|
||||
throws Exception {
|
||||
if (exception.compareAndSet(null, e.cause())) {
|
||||
e.channel().close();
|
||||
public void exceptionCaught(ChannelInboundHandlerContext<Byte> ctx,
|
||||
Throwable cause) throws Exception {
|
||||
if (exception.compareAndSet(null, cause)) {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package io.netty.testsuite.transport.socket;
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
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.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;
|
||||
|
||||
final class SocketTestCombination {
|
||||
|
||||
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>>>();
|
||||
|
||||
// Make the list of ServerBootstrap factories.
|
||||
List<Factory<ServerBootstrap>> sbfs =
|
||||
new ArrayList<SocketTestCombination.Factory<ServerBootstrap>>();
|
||||
sbfs.add(new Factory<ServerBootstrap>() {
|
||||
@Override
|
||||
public ServerBootstrap newInstance() {
|
||||
return new ServerBootstrap().
|
||||
eventLoop(new NioEventLoop(), new NioEventLoop()).
|
||||
channel(new NioServerSocketChannel());
|
||||
}
|
||||
});
|
||||
sbfs.add(new Factory<ServerBootstrap>() {
|
||||
@Override
|
||||
public ServerBootstrap newInstance() {
|
||||
return new ServerBootstrap().
|
||||
eventLoop(new OioEventLoop(), new OioEventLoop()).
|
||||
channel(new OioServerSocketChannel());
|
||||
}
|
||||
});
|
||||
|
||||
// Make the list of Bootstrap factories.
|
||||
List<Factory<Bootstrap>> cbfs =
|
||||
new ArrayList<SocketTestCombination.Factory<Bootstrap>>();
|
||||
cbfs.add(new Factory<Bootstrap>() {
|
||||
@Override
|
||||
public Bootstrap newInstance() {
|
||||
return new Bootstrap().eventLoop(new NioEventLoop()).channel(new NioSocketChannel());
|
||||
}
|
||||
});
|
||||
cbfs.add(new Factory<Bootstrap>() {
|
||||
@Override
|
||||
public Bootstrap newInstance() {
|
||||
return new Bootstrap().eventLoop(new OioEventLoop()).channel(new OioSocketChannel());
|
||||
}
|
||||
});
|
||||
|
||||
// Populate the combinations
|
||||
for (Factory<ServerBootstrap> sbf: sbfs) {
|
||||
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>>() {
|
||||
@Override
|
||||
public Factory<ServerBootstrap> getKey() {
|
||||
return sbf0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Factory<Bootstrap> getValue() {
|
||||
return cbf0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Factory<Bootstrap> setValue(Factory<Bootstrap> value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private SocketTestCombination() {}
|
||||
|
||||
static interface Factory<T> {
|
||||
T newInstance();
|
||||
}
|
||||
}
|
@ -249,6 +249,5 @@ public class ServerBootstrap {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user