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:
Trustin Lee 2012-06-03 01:52:39 -07:00
parent 3b2c25e8ed
commit 955c89fcf1
4 changed files with 193 additions and 65 deletions

View File

@ -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();
}
}
}
}

View File

@ -16,75 +16,44 @@
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.Bootstrap;
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.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers; import io.netty.buffer.ChannelBuffers;
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.ChannelInboundStreamHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelStateEvent; import java.io.IOException;
import io.netty.channel.ExceptionEvent; import java.util.Random;
import io.netty.channel.MessageEvent; import java.util.concurrent.atomic.AtomicReference;
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 org.junit.Test; import org.junit.Test;
public abstract class AbstractSocketEchoTest { public class SocketEchoTest extends AbstractSocketTest {
private static final Random random = new Random(); private static final Random random = new Random();
static final byte[] data = new byte[1048576]; static final byte[] data = new byte[1048576];
private static ExecutorService executor;
static { static {
random.nextBytes(data); 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 @Test
public void testSimpleEcho() throws Throwable { public void testSimpleEcho() throws Exception {
ServerBootstrap sb = new ServerBootstrap(newServerSocketChannelFactory(executor)); run();
ClientBootstrap cb = new ClientBootstrap(newClientSocketChannelFactory(executor)); }
public void testSimpleEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
EchoHandler sh = new EchoHandler(); EchoHandler sh = new EchoHandler();
EchoHandler ch = new EchoHandler(); EchoHandler ch = new EchoHandler();
sb.pipeline().addFirst("handler", sh); sb.childHandler(sh);
cb.pipeline().addFirst("handler", ch); cb.handler(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 (int i = 0; i < data.length;) { for (int i = 0; i < data.length;) {
int length = Math.min(random.nextInt(1024 * 64), data.length - i); int length = Math.min(random.nextInt(1024 * 64), data.length - i);
cc.write(ChannelBuffers.wrappedBuffer(data, i, length)); cc.write(ChannelBuffers.wrappedBuffer(data, i, length));
@ -121,9 +90,9 @@ public abstract class AbstractSocketEchoTest {
} }
} }
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();
@ -139,7 +108,7 @@ public abstract class AbstractSocketEchoTest {
} }
} }
private static class EchoHandler extends SimpleChannelUpstreamHandler { private static class EchoHandler extends ChannelInboundStreamHandlerAdapter {
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;
@ -148,35 +117,35 @@ public abstract class AbstractSocketEchoTest {
} }
@Override @Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) public void channelActive(ChannelInboundHandlerContext<Byte> ctx)
throws Exception { throws Exception {
channel = e.channel(); channel = ctx.channel();
} }
@Override @Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) public void inboundBufferUpdated(
ChannelInboundHandlerContext<Byte> ctx, ChannelBuffer in)
throws Exception { throws Exception {
ChannelBuffer m = (ChannelBuffer) e.getMessage(); byte[] actual = new byte[in.readableBytes()];
byte[] actual = new byte[m.readableBytes()]; in.readBytes(actual);
m.getBytes(0, actual);
int lastIdx = counter; int lastIdx = counter;
for (int i = 0; i < actual.length; i ++) { for (int i = 0; i < actual.length; i ++) {
assertEquals(data[i + lastIdx], actual[i]); assertEquals(data[i + lastIdx], actual[i]);
} }
if (channel.getParent() != null) { if (channel.parent() != null) {
channel.write(m); channel.write(ChannelBuffers.wrappedBuffer(actual));
} }
counter += actual.length; counter += actual.length;
} }
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) public void exceptionCaught(ChannelInboundHandlerContext<Byte> ctx,
throws Exception { Throwable cause) throws Exception {
if (exception.compareAndSet(null, e.cause())) { if (exception.compareAndSet(null, cause)) {
e.channel().close(); ctx.close();
} }
} }
} }

View File

@ -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();
}
}

View File

@ -249,6 +249,5 @@ public class ServerBootstrap {
} }
} }
} }
} }
} }