Allow to extend the provided tests by the testsuite

This allows custom transport implementations to just re-use the tests provided by netty and so make sure everything works like expected.
This commit is contained in:
Norman Maurer 2014-01-06 16:38:03 +01:00
parent 0244e35fa6
commit 56f8479b34
11 changed files with 366 additions and 329 deletions

View File

@ -32,27 +32,22 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymockclassextension</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit4</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
@ -79,10 +74,16 @@
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!--
<plugin>

View File

@ -0,0 +1,76 @@
/*
* Copyright 2014 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;
import io.netty.bootstrap.AbstractBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.testsuite.util.TestUtils;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Rule;
import org.junit.rules.TestName;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public abstract class AbstractComboTestsuiteTest<SB extends AbstractBootstrap<?, ?>,
CB extends AbstractBootstrap<?, ?>> {
private final Class<SB> sbClazz;
private final Class<CB> cbClazz;
protected final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
protected volatile CB cb;
protected volatile SB sb;
protected AbstractComboTestsuiteTest(Class<SB> sbClazz, Class<CB> cbClazz) {
this.sbClazz = sbClazz;
this.cbClazz = cbClazz;
}
protected abstract List<TestsuitePermutation.BootstrapComboFactory<SB, CB>> newFactories();
protected List<ByteBufAllocator> newAllocators() {
return TestsuitePermutation.allocator();
}
@Rule
public final TestName testName = new TestName();
protected void run() throws Throwable {
List<TestsuitePermutation.BootstrapComboFactory<SB, CB>> combos = newFactories();
for (ByteBufAllocator allocator: newAllocators()) {
int i = 0;
for (TestsuitePermutation.BootstrapComboFactory<SB, CB> e: combos) {
sb = e.newServerInstance();
cb = e.newClientInstance();
configure(sb, cb, allocator);
logger.info(String.format(
"Running: %s %d of %d (%s + %s) with %s",
testName.getMethodName(), ++ i, combos.size(), sb, cb, StringUtil.simpleClassName(allocator)));
try {
Method m = getClass().getMethod(
TestUtils.testMethodName(testName), sbClazz, cbClazz);
m.invoke(this, sb, cb);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
}
}
protected abstract void configure(SB sb, CB cb, ByteBufAllocator allocator);
}

View File

@ -0,0 +1,71 @@
/*
* Copyright 2014 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;
import io.netty.bootstrap.AbstractBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.testsuite.util.TestUtils;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Rule;
import org.junit.rules.TestName;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public abstract class AbstractTestsuiteTest<T extends AbstractBootstrap<?, ?>> {
private final Class<T> clazz;
protected final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
protected volatile T cb;
protected AbstractTestsuiteTest(Class<T> clazz) {
this.clazz = clazz;
}
protected abstract List<TestsuitePermutation.BootstrapFactory<T>> newFactories();
protected List<ByteBufAllocator> newAllocators() {
return TestsuitePermutation.allocator();
}
@Rule
public final TestName testName = new TestName();
protected void run() throws Throwable {
List<TestsuitePermutation.BootstrapFactory<T>> combos = newFactories();
for (ByteBufAllocator allocator: newAllocators()) {
int i = 0;
for (TestsuitePermutation.BootstrapFactory<T> e: combos) {
cb = e.newInstance();
configure(cb, allocator);
logger.info(String.format(
"Running: %s %d of %d with %s",
testName.getMethodName(), ++ i, combos.size(), StringUtil.simpleClassName(allocator)));
try {
Method m = getClass().getDeclaredMethod(
TestUtils.testMethodName(testName), clazz);
m.invoke(this, cb);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
}
}
protected abstract void configure(T bootstrap, ByteBufAllocator allocator);
}

View File

@ -0,0 +1,45 @@
/*
* Copyright 2014 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;
import io.netty.bootstrap.AbstractBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
import java.util.ArrayList;
import java.util.List;
public final class TestsuitePermutation {
public static List<ByteBufAllocator> allocator() {
List<ByteBufAllocator> allocators = new ArrayList<ByteBufAllocator>();
allocators.add(UnpooledByteBufAllocator.DEFAULT);
allocators.add(PooledByteBufAllocator.DEFAULT);
return allocators;
}
private TestsuitePermutation() { }
public interface BootstrapFactory<CB extends AbstractBootstrap<?, ?>> {
CB newInstance();
}
public interface BootstrapComboFactory<SB extends AbstractBootstrap<?, ?>, CB extends AbstractBootstrap<?, ?>> {
SB newServerInstance();
CB newClientInstance();
}
}

View File

@ -19,61 +19,33 @@ import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelOption;
import io.netty.testsuite.transport.sctp.SctpTestPermutation.Factory;
import io.netty.testsuite.transport.AbstractComboTestsuiteTest;
import io.netty.testsuite.transport.TestsuitePermutation;
import io.netty.testsuite.util.TestUtils;
import io.netty.util.NetUtil;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
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 =
SctpTestPermutation.sctpChannel();
private static final List<ByteBufAllocator> ALLOCATORS = SctpTestPermutation.allocator();
@Rule
public final TestName testName = new TestName();
protected final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
protected volatile ServerBootstrap sb;
protected volatile Bootstrap cb;
public abstract class AbstractSctpTest extends AbstractComboTestsuiteTest<ServerBootstrap, Bootstrap> {
protected volatile InetSocketAddress addr;
protected volatile Factory<Bootstrap> currentBootstrap;
protected void run() throws Throwable {
for (ByteBufAllocator allocator: ALLOCATORS) {
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(NetUtil.LOCALHOST, TestUtils.getFreePort());
sb.localAddress(addr);
sb.option(ChannelOption.ALLOCATOR, allocator);
sb.childOption(ChannelOption.ALLOCATOR, allocator);
cb.remoteAddress(addr);
cb.option(ChannelOption.ALLOCATOR, allocator);
logger.info(String.format(
"Running: %s %d of %d (%s + %s) with %s",
testName.getMethodName(), ++ i, COMBO.size(), sb, cb, StringUtil.simpleClassName(allocator)));
try {
Method m = getClass().getDeclaredMethod(
TestUtils.testMethodName(testName), ServerBootstrap.class, Bootstrap.class);
m.invoke(this, sb, cb);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
}
protected AbstractSctpTest() {
super(ServerBootstrap.class, Bootstrap.class);
}
@Override
protected List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> newFactories() {
return SctpTestPermutation.sctpChannel();
}
@Override
protected void configure(ServerBootstrap serverBootstrap, Bootstrap bootstrap, ByteBufAllocator allocator) {
addr = new InetSocketAddress(NetUtil.LOCALHOST, TestUtils.getFreePort());
serverBootstrap.localAddress(addr);
serverBootstrap.option(ChannelOption.ALLOCATOR, allocator);
serverBootstrap.childOption(ChannelOption.ALLOCATOR, allocator);
bootstrap.remoteAddress(addr);
bootstrap.option(ChannelOption.ALLOCATOR, allocator);
}
}

View File

@ -17,9 +17,6 @@ package io.netty.testsuite.transport.sctp;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.oio.OioEventLoopGroup;
@ -28,12 +25,13 @@ import io.netty.channel.sctp.nio.NioSctpServerChannel;
import io.netty.channel.sctp.oio.OioSctpChannel;
import io.netty.channel.sctp.oio.OioSctpServerChannel;
import io.netty.testsuite.util.TestUtils;
import io.netty.testsuite.transport.TestsuitePermutation.BootstrapComboFactory;
import io.netty.testsuite.transport.TestsuitePermutation.BootstrapFactory;
import io.netty.util.concurrent.DefaultThreadFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public final class SctpTestPermutation {
@ -48,14 +46,14 @@ public final class SctpTestPermutation {
private static final EventLoopGroup oioWorkerGroup =
new OioEventLoopGroup(Integer.MAX_VALUE, new DefaultThreadFactory("testsuite-sctp-oio-worker", true));
static List<Factory<ServerBootstrap>> sctpServerChannel() {
static List<BootstrapFactory<ServerBootstrap>> sctpServerChannel() {
if (!TestUtils.isSctpSupported()) {
return Collections.emptyList();
}
List<Factory<ServerBootstrap>> list = new ArrayList<Factory<ServerBootstrap>>();
List<BootstrapFactory<ServerBootstrap>> list = new ArrayList<BootstrapFactory<ServerBootstrap>>();
// Make the list of ServerBootstrap factories.
list.add(new Factory<ServerBootstrap>() {
list.add(new BootstrapFactory<ServerBootstrap>() {
@Override
public ServerBootstrap newInstance() {
return new ServerBootstrap().
@ -63,7 +61,7 @@ public final class SctpTestPermutation {
channel(NioSctpServerChannel.class);
}
});
list.add(new Factory<ServerBootstrap>() {
list.add(new BootstrapFactory<ServerBootstrap>() {
@Override
public ServerBootstrap newInstance() {
return new ServerBootstrap().
@ -75,19 +73,19 @@ public final class SctpTestPermutation {
return list;
}
static List<Factory<Bootstrap>> sctpClientChannel() {
static List<BootstrapFactory<Bootstrap>> sctpClientChannel() {
if (!TestUtils.isSctpSupported()) {
return Collections.emptyList();
}
List<Factory<Bootstrap>> list = new ArrayList<Factory<Bootstrap>>();
list.add(new Factory<Bootstrap>() {
List<BootstrapFactory<Bootstrap>> list = new ArrayList<BootstrapFactory<Bootstrap>>();
list.add(new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channel(NioSctpChannel.class);
}
});
list.add(new Factory<Bootstrap>() {
list.add(new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(oioWorkerGroup).channel(OioSctpChannel.class);
@ -96,35 +94,30 @@ public final class SctpTestPermutation {
return list;
}
static List<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> sctpChannel() {
List<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> list =
new ArrayList<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>>();
static List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> sctpChannel() {
List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> list =
new ArrayList<BootstrapComboFactory<ServerBootstrap, Bootstrap>>();
// Make the list of SCTP ServerBootstrap factories.
List<Factory<ServerBootstrap>> sbfs = sctpServerChannel();
List<BootstrapFactory<ServerBootstrap>> sbfs = sctpServerChannel();
// Make the list of SCTP Bootstrap factories.
List<Factory<Bootstrap>> cbfs = sctpClientChannel();
List<BootstrapFactory<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 Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>() {
for (BootstrapFactory<ServerBootstrap> sbf: sbfs) {
for (BootstrapFactory<Bootstrap> cbf: cbfs) {
final BootstrapFactory<ServerBootstrap> sbf0 = sbf;
final BootstrapFactory<Bootstrap> cbf0 = cbf;
list.add(new BootstrapComboFactory<ServerBootstrap, Bootstrap>() {
@Override
public Factory<ServerBootstrap> getKey() {
return sbf0;
public ServerBootstrap newServerInstance() {
return sbf0.newInstance();
}
@Override
public Factory<Bootstrap> getValue() {
return cbf0;
}
@Override
public Factory<Bootstrap> setValue(Factory<Bootstrap> value) {
throw new UnsupportedOperationException();
public Bootstrap newClientInstance() {
return cbf0.newInstance();
}
});
}
@ -133,16 +126,5 @@ public final class SctpTestPermutation {
return list;
}
static List<ByteBufAllocator> allocator() {
List<ByteBufAllocator> allocators = new ArrayList<ByteBufAllocator>();
allocators.add(UnpooledByteBufAllocator.DEFAULT);
allocators.add(PooledByteBufAllocator.DEFAULT);
return allocators;
}
private SctpTestPermutation() { }
interface Factory<T> {
T newInstance();
}
}

View File

@ -18,51 +18,31 @@ package io.netty.testsuite.transport.socket;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelOption;
import io.netty.testsuite.transport.socket.SocketTestPermutation.Factory;
import io.netty.testsuite.transport.AbstractTestsuiteTest;
import io.netty.testsuite.transport.TestsuitePermutation;
import io.netty.testsuite.util.TestUtils;
import io.netty.util.NetUtil;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
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;
public abstract class AbstractClientSocketTest {
public abstract class AbstractClientSocketTest extends AbstractTestsuiteTest<Bootstrap> {
private static final List<Factory<Bootstrap>> COMBO = SocketTestPermutation.clientSocket();
private static final List<ByteBufAllocator> ALLOCATORS = SocketTestPermutation.allocator();
@Rule
public final TestName testName = new TestName();
protected final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
protected volatile Bootstrap cb;
protected volatile InetSocketAddress addr;
protected void run() throws Throwable {
for (ByteBufAllocator allocator: ALLOCATORS) {
int i = 0;
for (Factory<Bootstrap> e: COMBO) {
cb = e.newInstance();
addr = new InetSocketAddress(NetUtil.LOCALHOST, TestUtils.getFreePort());
cb.remoteAddress(addr);
cb.option(ChannelOption.ALLOCATOR, allocator);
logger.info(String.format(
"Running: %s %d of %d with %s",
testName.getMethodName(), ++ i, COMBO.size(), StringUtil.simpleClassName(allocator)));
try {
Method m = getClass().getDeclaredMethod(TestUtils.testMethodName(testName), Bootstrap.class);
m.invoke(this, cb);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
}
protected AbstractClientSocketTest() {
super(Bootstrap.class);
}
@Override
protected List<TestsuitePermutation.BootstrapFactory<Bootstrap>> newFactories() {
return SocketTestPermutation.clientSocket();
}
@Override
protected void configure(Bootstrap bootstrap, ByteBufAllocator allocator) {
addr = new InetSocketAddress(NetUtil.LOCALHOST, TestUtils.getFreePort());
bootstrap.remoteAddress(addr);
bootstrap.option(ChannelOption.ALLOCATOR, allocator);
}
}

View File

@ -18,57 +18,34 @@ package io.netty.testsuite.transport.socket;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelOption;
import io.netty.testsuite.transport.socket.SocketTestPermutation.Factory;
import io.netty.testsuite.transport.AbstractComboTestsuiteTest;
import io.netty.testsuite.transport.TestsuitePermutation;
import io.netty.testsuite.util.TestUtils;
import io.netty.util.NetUtil;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
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 AbstractDatagramTest {
public abstract class AbstractDatagramTest extends AbstractComboTestsuiteTest<Bootstrap, Bootstrap> {
private static final List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> COMBO = SocketTestPermutation.datagram();
private static final List<ByteBufAllocator> ALLOCATORS = SocketTestPermutation.allocator();
@Rule
public final TestName testName = new TestName();
protected final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
protected volatile Bootstrap sb;
protected volatile Bootstrap cb;
protected volatile InetSocketAddress addr;
protected void run() throws Throwable {
for (ByteBufAllocator allocator: ALLOCATORS) {
int i = 0;
for (Entry<Factory<Bootstrap>, Factory<Bootstrap>> e: COMBO) {
sb = e.getKey().newInstance();
cb = e.getValue().newInstance();
addr = new InetSocketAddress(NetUtil.LOCALHOST4, TestUtils.getFreePort());
sb.localAddress(addr);
sb.option(ChannelOption.ALLOCATOR, allocator);
cb.localAddress(0).remoteAddress(addr);
cb.option(ChannelOption.ALLOCATOR, allocator);
logger.info(String.format(
"Running: %s %d of %d (%s + %s) with %s",
testName.getMethodName(), ++ i, COMBO.size(), sb, cb, StringUtil.simpleClassName(allocator)));
try {
Method m = getClass().getDeclaredMethod(
TestUtils.testMethodName(testName), Bootstrap.class, Bootstrap.class);
m.invoke(this, sb, cb);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
}
protected AbstractDatagramTest() {
super(Bootstrap.class, Bootstrap.class);
}
@Override
protected List<TestsuitePermutation.BootstrapComboFactory<Bootstrap, Bootstrap>> newFactories() {
return SocketTestPermutation.datagram();
}
@Override
protected void configure(Bootstrap bootstrap, Bootstrap bootstrap2, ByteBufAllocator allocator) {
addr = new InetSocketAddress(
NetUtil.LOCALHOST4, TestUtils.getFreePort());
bootstrap.localAddress(addr);
bootstrap.option(ChannelOption.ALLOCATOR, allocator);
bootstrap2.localAddress(0).remoteAddress(addr);
bootstrap2.option(ChannelOption.ALLOCATOR, allocator);
}
}

View File

@ -18,53 +18,33 @@ package io.netty.testsuite.transport.socket;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelOption;
import io.netty.testsuite.transport.socket.SocketTestPermutation.Factory;
import io.netty.testsuite.transport.AbstractTestsuiteTest;
import io.netty.testsuite.transport.TestsuitePermutation;
import io.netty.testsuite.util.TestUtils;
import io.netty.util.NetUtil;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
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;
public abstract class AbstractServerSocketTest {
public abstract class AbstractServerSocketTest extends AbstractTestsuiteTest<ServerBootstrap> {
private static final List<Factory<ServerBootstrap>> COMBO = SocketTestPermutation.serverSocket();
private static final List<ByteBufAllocator> ALLOCATORS = SocketTestPermutation.allocator();
@Rule
public final TestName testName = new TestName();
protected final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
protected volatile ServerBootstrap sb;
protected volatile InetSocketAddress addr;
protected void run() throws Throwable {
for (ByteBufAllocator allocator: ALLOCATORS) {
int i = 0;
for (Factory<ServerBootstrap> e: COMBO) {
sb = e.newInstance();
addr = new InetSocketAddress(NetUtil.LOCALHOST, TestUtils.getFreePort());
sb.localAddress(addr);
sb.option(ChannelOption.ALLOCATOR, allocator);
sb.childOption(ChannelOption.ALLOCATOR, allocator);
protected AbstractServerSocketTest() {
super(ServerBootstrap.class);
}
logger.info(String.format(
"Running: %s %d of %d (%s) with %s",
testName.getMethodName(), ++ i, COMBO.size(), sb, StringUtil.simpleClassName(allocator)));
try {
Method m = getClass().getDeclaredMethod(TestUtils.testMethodName(testName), ServerBootstrap.class);
m.invoke(this, sb);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
}
@Override
protected List<TestsuitePermutation.BootstrapFactory<ServerBootstrap>> newFactories() {
return SocketTestPermutation.serverSocket();
}
@Override
protected void configure(ServerBootstrap bootstrap, ByteBufAllocator allocator) {
addr = new InetSocketAddress(
NetUtil.LOCALHOST, TestUtils.getFreePort());
bootstrap.localAddress(addr);
bootstrap.option(ChannelOption.ALLOCATOR, allocator);
bootstrap.childOption(ChannelOption.ALLOCATOR, allocator);
}
}

View File

@ -19,63 +19,35 @@ import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelOption;
import io.netty.testsuite.transport.socket.SocketTestPermutation.Factory;
import io.netty.testsuite.transport.AbstractComboTestsuiteTest;
import io.netty.testsuite.transport.TestsuitePermutation;
import io.netty.testsuite.util.TestUtils;
import io.netty.util.NetUtil;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
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 AbstractSocketTest {
public abstract class AbstractSocketTest extends AbstractComboTestsuiteTest<ServerBootstrap, Bootstrap> {
private static final List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> COMBO =
SocketTestPermutation.socket();
private static final List<ByteBufAllocator> ALLOCATORS = SocketTestPermutation.allocator();
@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 {
for (ByteBufAllocator allocator: ALLOCATORS) {
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(NetUtil.LOCALHOST, TestUtils.getFreePort());
sb.localAddress(addr);
sb.option(ChannelOption.ALLOCATOR, allocator);
sb.childOption(ChannelOption.ALLOCATOR, allocator);
cb.remoteAddress(addr);
cb.option(ChannelOption.ALLOCATOR, allocator);
protected AbstractSocketTest() {
super(ServerBootstrap.class, Bootstrap.class);
}
logger.info(String.format(
"Running: %s %d of %d (%s + %s) with %s",
testName.getMethodName(), ++ i, COMBO.size(), sb, cb, StringUtil.simpleClassName(allocator)));
try {
Method m = getClass().getDeclaredMethod(
TestUtils.testMethodName(testName), ServerBootstrap.class, Bootstrap.class);
m.invoke(this, sb, cb);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
}
@Override
protected List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> newFactories() {
return SocketTestPermutation.socket();
}
@Override
protected void configure(ServerBootstrap bootstrap, Bootstrap bootstrap2, ByteBufAllocator allocator) {
addr = new InetSocketAddress(
NetUtil.LOCALHOST, TestUtils.getFreePort());
bootstrap.localAddress(addr);
bootstrap.option(ChannelOption.ALLOCATOR, allocator);
bootstrap.childOption(ChannelOption.ALLOCATOR, allocator);
bootstrap2.remoteAddress(addr);
bootstrap2.option(ChannelOption.ALLOCATOR, allocator);
}
}

View File

@ -18,9 +18,6 @@ package io.netty.testsuite.transport.socket;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ChannelFactory;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
@ -33,13 +30,18 @@ import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.oio.OioDatagramChannel;
import io.netty.channel.socket.oio.OioServerSocketChannel;
import io.netty.channel.socket.oio.OioSocketChannel;
import io.netty.testsuite.transport.TestsuitePermutation.BootstrapComboFactory;
import io.netty.testsuite.transport.TestsuitePermutation.BootstrapFactory;
import io.netty.util.concurrent.DefaultThreadFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
final class SocketTestPermutation {
public final class SocketTestPermutation {
private SocketTestPermutation() {
// utility
}
private static final int BOSSES = 2;
private static final int WORKERS = 3;
private static final EventLoopGroup nioBossGroup =
@ -51,35 +53,30 @@ final class SocketTestPermutation {
private static final EventLoopGroup oioWorkerGroup =
new OioEventLoopGroup(Integer.MAX_VALUE, new DefaultThreadFactory("testsuite-oio-worker", true));
static List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> socket() {
List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> list =
new ArrayList<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>>();
static List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> socket() {
List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> list =
new ArrayList<BootstrapComboFactory<ServerBootstrap, Bootstrap>>();
// Make the list of ServerBootstrap factories.
List<Factory<ServerBootstrap>> sbfs = serverSocket();
List<BootstrapFactory<ServerBootstrap>> sbfs = serverSocket();
// Make the list of Bootstrap factories.
List<Factory<Bootstrap>> cbfs = clientSocket();
List<BootstrapFactory<Bootstrap>> cbfs = clientSocket();
// 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>>() {
for (BootstrapFactory<ServerBootstrap> sbf: sbfs) {
for (BootstrapFactory<Bootstrap> cbf: cbfs) {
final BootstrapFactory<ServerBootstrap> sbf0 = sbf;
final BootstrapFactory<Bootstrap> cbf0 = cbf;
list.add(new BootstrapComboFactory<ServerBootstrap, Bootstrap>() {
@Override
public Factory<ServerBootstrap> getKey() {
return sbf0;
public ServerBootstrap newServerInstance() {
return sbf0.newInstance();
}
@Override
public Factory<Bootstrap> getValue() {
return cbf0;
}
@Override
public Factory<Bootstrap> setValue(Factory<Bootstrap> value) {
throw new UnsupportedOperationException();
public Bootstrap newClientInstance() {
return cbf0.newInstance();
}
});
}
@ -91,13 +88,14 @@ final class SocketTestPermutation {
return list;
}
static List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> datagram() {
List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> list =
new ArrayList<Entry<Factory<Bootstrap>, Factory<Bootstrap>>>();
static List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
List<BootstrapComboFactory<Bootstrap, Bootstrap>> list =
new ArrayList<BootstrapComboFactory<Bootstrap, Bootstrap>>();
// Make the list of Bootstrap factories.
List<Factory<Bootstrap>> bfs = new ArrayList<Factory<Bootstrap>>();
bfs.add(new Factory<Bootstrap>() {
List<BootstrapFactory<Bootstrap>> bfs =
new ArrayList<BootstrapFactory<Bootstrap>>();
bfs.add(new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@ -113,7 +111,7 @@ final class SocketTestPermutation {
});
}
});
bfs.add(new Factory<Bootstrap>() {
bfs.add(new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class);
@ -121,24 +119,19 @@ final class SocketTestPermutation {
});
// 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>>() {
for (BootstrapFactory<Bootstrap> sbf: bfs) {
for (BootstrapFactory<Bootstrap> cbf: bfs) {
final BootstrapFactory<Bootstrap> sbf0 = sbf;
final BootstrapFactory<Bootstrap> cbf0 = cbf;
list.add(new BootstrapComboFactory<Bootstrap, Bootstrap>() {
@Override
public Factory<Bootstrap> getKey() {
return sbf0;
public Bootstrap newServerInstance() {
return sbf0.newInstance();
}
@Override
public Factory<Bootstrap> getValue() {
return cbf0;
}
@Override
public Factory<Bootstrap> setValue(Factory<Bootstrap> value) {
throw new UnsupportedOperationException();
public Bootstrap newClientInstance() {
return cbf0.newInstance();
}
});
}
@ -147,18 +140,18 @@ final class SocketTestPermutation {
return list;
}
static List<Factory<ServerBootstrap>> serverSocket() {
List<Factory<ServerBootstrap>> list = new ArrayList<Factory<ServerBootstrap>>();
static List<BootstrapFactory<ServerBootstrap>> serverSocket() {
List<BootstrapFactory<ServerBootstrap>> list = new ArrayList<BootstrapFactory<ServerBootstrap>>();
// Make the list of ServerBootstrap factories.
list.add(new Factory<ServerBootstrap>() {
list.add(new BootstrapFactory<ServerBootstrap>() {
@Override
public ServerBootstrap newInstance() {
return new ServerBootstrap().group(nioBossGroup, nioWorkerGroup)
.channel(NioServerSocketChannel.class);
}
});
list.add(new Factory<ServerBootstrap>() {
list.add(new BootstrapFactory<ServerBootstrap>() {
@Override
public ServerBootstrap newInstance() {
return new ServerBootstrap().group(oioBossGroup, oioWorkerGroup)
@ -169,15 +162,15 @@ final class SocketTestPermutation {
return list;
}
static List<Factory<Bootstrap>> clientSocket() {
List<Factory<Bootstrap>> list = new ArrayList<Factory<Bootstrap>>();
list.add(new Factory<Bootstrap>() {
static List<BootstrapFactory<Bootstrap>> clientSocket() {
List<BootstrapFactory<Bootstrap>> list = new ArrayList<BootstrapFactory<Bootstrap>>();
list.add(new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channel(NioSocketChannel.class);
}
});
list.add(new Factory<Bootstrap>() {
list.add(new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(oioWorkerGroup).channel(OioSocketChannel.class);
@ -186,16 +179,4 @@ final class SocketTestPermutation {
return list;
}
static List<ByteBufAllocator> allocator() {
List<ByteBufAllocator> allocators = new ArrayList<ByteBufAllocator>();
allocators.add(UnpooledByteBufAllocator.DEFAULT);
allocators.add(PooledByteBufAllocator.DEFAULT);
return allocators;
}
private SocketTestPermutation() { }
interface Factory<T> {
T newInstance();
}
}