Forward ported SCTP Echo Testcases

This commit is contained in:
Jestan Nirojan 2012-09-30 14:14:34 +08:00
parent 8a75442419
commit 7afa237f3f
4 changed files with 383 additions and 4 deletions

View File

@ -0,0 +1,74 @@
/*
* Copyright 2012 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.SocketTestPermutation.Factory;
import io.netty.testsuite.util.TestUtils;
import io.netty.util.NetworkConstants;
import org.junit.Rule;
import org.junit.rules.TestName;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map.Entry;
public abstract class AbstractSctpTest {
private static final List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> COMBO =
SocketTestPermutation.sctpChannel();
@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 volatile Factory<Bootstrap> currentBootstrap;
protected void run() throws Throwable {
int i = 0;
for (Entry<Factory<ServerBootstrap>, Factory<Bootstrap>> e: COMBO) {
currentBootstrap = e.getValue();
sb = e.getKey().newInstance();
cb = e.getValue().newInstance();
addr = new InetSocketAddress(
NetworkConstants.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);
} catch (InvocationTargetException ex) {
throw ex.getCause();
} finally {
sb.shutdown();
cb.shutdown();
}
}
}
}

View File

@ -0,0 +1,201 @@
/*
* Copyright 2012 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.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundByteHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SctpChannel;
import io.netty.handler.codec.sctp.SctpInboundByteStreamHandler;
import io.netty.handler.codec.sctp.SctpMessageCompletionHandler;
import io.netty.handler.codec.sctp.SctpOutboundByteStreamHandler;
import io.netty.testsuite.util.TestUtils;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals;
public class SctpEchoTest extends AbstractSctpTest {
private static final Random random = new Random();
static final byte[] data = new byte[4096];//could not test ultra jumbo frames
static {
random.nextBytes(data);
}
@Test
public void testSimpleEcho() throws Throwable {
Assume.assumeTrue(TestUtils.isSctpSupported());
run();
}
public void testSimpleEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
testSimpleEcho0(sb, cb, Integer.MAX_VALUE);
}
@Test
@Ignore("TODO: fix this after OioSctp EventLoop done")
public void testSimpleEchoWithBoundedBuffer() throws Throwable {
Assume.assumeTrue(TestUtils.isSctpSupported());
run();
}
public void testSimpleEchoWithBoundedBuffer(ServerBootstrap sb, Bootstrap cb) throws Throwable {
testSimpleEcho0(sb, cb, 32);
}
private static void testSimpleEcho0(ServerBootstrap sb, Bootstrap cb, int maxInboundBufferSize) throws Throwable {
final EchoHandler sh = new EchoHandler(maxInboundBufferSize);
final EchoHandler ch = new EchoHandler(maxInboundBufferSize);
sb.childHandler(new ChannelInitializer<SctpChannel>() {
@Override
public void initChannel(SctpChannel c) throws Exception {
c.pipeline().addLast(
new SctpMessageCompletionHandler(),
new SctpInboundByteStreamHandler(0, 0),
new SctpOutboundByteStreamHandler(0, 0),
sh);
}
});
cb.handler(new ChannelInitializer<SctpChannel>() {
@Override
public void initChannel(SctpChannel c) throws Exception {
c.pipeline().addLast(
new SctpMessageCompletionHandler(),
new SctpInboundByteStreamHandler(0, 0),
new SctpOutboundByteStreamHandler(0, 0),
ch);
}
});
Channel sc = sb.bind().sync().channel();
Channel cc = cb.connect().sync().channel();
for (int i = 0; i < data.length; ) {
int length = Math.min(random.nextInt(1024 * 64), data.length - i);
cc.write(Unpooled.wrappedBuffer(data, i, length));
i += length;
}
while (ch.counter < data.length) {
if (sh.exception.get() != null) {
break;
}
if (ch.exception.get() != null) {
break;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// Ignore.
}
}
while (sh.counter < data.length) {
if (sh.exception.get() != null) {
break;
}
if (ch.exception.get() != null) {
break;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// Ignore.
}
}
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();
}
if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
throw ch.exception.get();
}
if (sh.exception.get() != null) {
throw sh.exception.get();
}
if (ch.exception.get() != null) {
throw ch.exception.get();
}
}
private static class EchoHandler extends ChannelInboundByteHandlerAdapter {
private final int maxInboundBufferSize;
volatile Channel channel;
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
volatile int counter;
EchoHandler(int maxInboundBufferSize) {
this.maxInboundBufferSize = maxInboundBufferSize;
}
@Override
public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
return Unpooled.buffer(0, maxInboundBufferSize);
}
@Override
public void channelActive(ChannelHandlerContext ctx)
throws Exception {
channel = ctx.channel();
}
@Override
public void inboundBufferUpdated(
ChannelHandlerContext ctx, ByteBuf in)
throws Exception {
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.parent() != null) {
channel.write(Unpooled.wrappedBuffer(actual));
}
counter += actual.length;
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx,
Throwable cause) throws Exception {
if (exception.compareAndSet(null, cause)) {
ctx.close();
}
}
}
}

View File

@ -26,11 +26,15 @@ import io.netty.channel.socket.aio.AioSocketChannel;
import io.netty.channel.socket.nio.NioDatagramChannel; import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSctpServerChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.nio.NioSctpChannel;
import io.netty.channel.socket.oio.OioDatagramChannel; import io.netty.channel.socket.oio.OioDatagramChannel;
import io.netty.channel.socket.oio.OioEventLoopGroup; import io.netty.channel.socket.oio.OioEventLoopGroup;
import io.netty.channel.socket.oio.OioServerSocketChannel; import io.netty.channel.socket.oio.OioServerSocketChannel;
import io.netty.channel.socket.oio.OioSctpServerChannel;
import io.netty.channel.socket.oio.OioSocketChannel; import io.netty.channel.socket.oio.OioSocketChannel;
import io.netty.channel.socket.oio.OioSctpChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -130,6 +134,43 @@ final class SocketTestPermutation {
return list; return list;
} }
static List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> sctpChannel() {
List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> list =
new ArrayList<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>>();
// Make the list of SCTP ServerBootstrap factories.
List<Factory<ServerBootstrap>> sbfs = sctpServerChannel();
// Make the list of SCTP Bootstrap factories.
List<Factory<Bootstrap>> cbfs = sctpClientChannel();
// 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 Entry<Factory<ServerBootstrap>, 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;
}
static List<Factory<ServerBootstrap>> serverSocket() { static List<Factory<ServerBootstrap>> serverSocket() {
List<Factory<ServerBootstrap>> list = new ArrayList<Factory<ServerBootstrap>>(); List<Factory<ServerBootstrap>> list = new ArrayList<Factory<ServerBootstrap>>();
@ -197,6 +238,47 @@ final class SocketTestPermutation {
return list; return list;
} }
static List<Factory<ServerBootstrap>> sctpServerChannel() {
List<Factory<ServerBootstrap>> list = new ArrayList<Factory<ServerBootstrap>>();
// Make the list of ServerBootstrap factories.
list.add(new Factory<ServerBootstrap>() {
@Override
public ServerBootstrap newInstance() {
return new ServerBootstrap().
group(new NioEventLoopGroup(), new NioEventLoopGroup()).
channel(NioSctpServerChannel.class);
}
});
list.add(new Factory<ServerBootstrap>() {
@Override
public ServerBootstrap newInstance() {
return new ServerBootstrap().
group(new OioEventLoopGroup(), new OioEventLoopGroup()).
channel(OioSctpServerChannel.class);
}
});
return list;
}
static List<Factory<Bootstrap>> sctpClientChannel() {
List<Factory<Bootstrap>> list = new ArrayList<Factory<Bootstrap>>();
list.add(new Factory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(new NioEventLoopGroup()).channel(NioSctpChannel.class);
}
});
list.add(new Factory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(new OioEventLoopGroup()).channel(OioSctpChannel.class);
}
});
return list;
}
private SocketTestPermutation() {} private SocketTestPermutation() {}
interface Factory<T> { interface Factory<T> {

View File

@ -15,15 +15,13 @@
*/ */
package io.netty.testsuite.util; package io.netty.testsuite.util;
import com.sun.nio.sctp.SctpChannel;
import io.netty.util.NetworkConstants; import io.netty.util.NetworkConstants;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class TestUtils { public class TestUtils {
@ -78,5 +76,29 @@ public class TestUtils {
throw new RuntimeException("unable to find a free port"); throw new RuntimeException("unable to find a free port");
} }
/**
* Return <code>true</code> if SCTP is supported by the running os.
*
*/
public static boolean isSctpSupported() {
String os = System.getProperty("os.name").toLowerCase(Locale.UK);
if (os.equals("unix") || os.equals("linux") || os.equals("sun") || os.equals("solaris")) {
try {
SctpChannel.open();
} catch (IOException e) {
// ignore
} catch (UnsupportedOperationException e) {
// This exception may get thrown if the OS does not have
// the shared libs installed.
System.out.print("Not supported: " + e.getMessage());
return false;
}
return true;
}
return false;
}
private TestUtils() { } private TestUtils() { }
} }