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 8d431679d7
commit 2c3f02c453
11 changed files with 366 additions and 330 deletions

View File

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

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

View File

@ -17,9 +17,6 @@ package io.netty.testsuite.transport.sctp;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap; 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.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.oio.OioEventLoopGroup; 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.OioSctpChannel;
import io.netty.channel.sctp.oio.OioSctpServerChannel; import io.netty.channel.sctp.oio.OioSctpServerChannel;
import io.netty.testsuite.util.TestUtils; 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 io.netty.util.concurrent.DefaultThreadFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
public final class SctpTestPermutation { public final class SctpTestPermutation {
@ -48,14 +46,14 @@ public final class SctpTestPermutation {
private static final EventLoopGroup oioWorkerGroup = private static final EventLoopGroup oioWorkerGroup =
new OioEventLoopGroup(Integer.MAX_VALUE, new DefaultThreadFactory("testsuite-sctp-oio-worker", true)); 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()) { if (!TestUtils.isSctpSupported()) {
return Collections.emptyList(); 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. // Make the list of ServerBootstrap factories.
list.add(new Factory<ServerBootstrap>() { list.add(new BootstrapFactory<ServerBootstrap>() {
@Override @Override
public ServerBootstrap newInstance() { public ServerBootstrap newInstance() {
return new ServerBootstrap(). return new ServerBootstrap().
@ -63,7 +61,7 @@ public final class SctpTestPermutation {
channel(NioSctpServerChannel.class); channel(NioSctpServerChannel.class);
} }
}); });
list.add(new Factory<ServerBootstrap>() { list.add(new BootstrapFactory<ServerBootstrap>() {
@Override @Override
public ServerBootstrap newInstance() { public ServerBootstrap newInstance() {
return new ServerBootstrap(). return new ServerBootstrap().
@ -75,19 +73,19 @@ public final class SctpTestPermutation {
return list; return list;
} }
static List<Factory<Bootstrap>> sctpClientChannel() { static List<BootstrapFactory<Bootstrap>> sctpClientChannel() {
if (!TestUtils.isSctpSupported()) { if (!TestUtils.isSctpSupported()) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<Factory<Bootstrap>> list = new ArrayList<Factory<Bootstrap>>(); List<BootstrapFactory<Bootstrap>> list = new ArrayList<BootstrapFactory<Bootstrap>>();
list.add(new Factory<Bootstrap>() { list.add(new BootstrapFactory<Bootstrap>() {
@Override @Override
public Bootstrap newInstance() { public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channel(NioSctpChannel.class); return new Bootstrap().group(nioWorkerGroup).channel(NioSctpChannel.class);
} }
}); });
list.add(new Factory<Bootstrap>() { list.add(new BootstrapFactory<Bootstrap>() {
@Override @Override
public Bootstrap newInstance() { public Bootstrap newInstance() {
return new Bootstrap().group(oioWorkerGroup).channel(OioSctpChannel.class); return new Bootstrap().group(oioWorkerGroup).channel(OioSctpChannel.class);
@ -96,35 +94,30 @@ public final class SctpTestPermutation {
return list; return list;
} }
static List<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> sctpChannel() { static List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> sctpChannel() {
List<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> list = List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> list =
new ArrayList<Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>>(); new ArrayList<BootstrapComboFactory<ServerBootstrap, Bootstrap>>();
// Make the list of SCTP ServerBootstrap factories. // Make the list of SCTP ServerBootstrap factories.
List<Factory<ServerBootstrap>> sbfs = sctpServerChannel(); List<BootstrapFactory<ServerBootstrap>> sbfs = sctpServerChannel();
// Make the list of SCTP Bootstrap factories. // Make the list of SCTP Bootstrap factories.
List<Factory<Bootstrap>> cbfs = sctpClientChannel(); List<BootstrapFactory<Bootstrap>> cbfs = sctpClientChannel();
// Populate the combinations // Populate the combinations
for (Factory<ServerBootstrap> sbf: sbfs) { for (BootstrapFactory<ServerBootstrap> sbf: sbfs) {
for (Factory<Bootstrap> cbf: cbfs) { for (BootstrapFactory<Bootstrap> cbf: cbfs) {
final Factory<ServerBootstrap> sbf0 = sbf; final BootstrapFactory<ServerBootstrap> sbf0 = sbf;
final Factory<Bootstrap> cbf0 = cbf; final BootstrapFactory<Bootstrap> cbf0 = cbf;
list.add(new Map.Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>() { list.add(new BootstrapComboFactory<ServerBootstrap, Bootstrap>() {
@Override @Override
public Factory<ServerBootstrap> getKey() { public ServerBootstrap newServerInstance() {
return sbf0; return sbf0.newInstance();
} }
@Override @Override
public Factory<Bootstrap> getValue() { public Bootstrap newClientInstance() {
return cbf0; return cbf0.newInstance();
}
@Override
public Factory<Bootstrap> setValue(Factory<Bootstrap> value) {
throw new UnsupportedOperationException();
} }
}); });
} }
@ -133,16 +126,5 @@ public final class SctpTestPermutation {
return list; return list;
} }
static List<ByteBufAllocator> allocator() {
List<ByteBufAllocator> allocators = new ArrayList<ByteBufAllocator>();
allocators.add(UnpooledByteBufAllocator.DEFAULT);
allocators.add(PooledByteBufAllocator.DEFAULT);
return allocators;
}
private SctpTestPermutation() { } 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.bootstrap.Bootstrap;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelOption; 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.testsuite.util.TestUtils;
import io.netty.util.NetUtil; 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.net.InetSocketAddress;
import java.util.List; 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 volatile InetSocketAddress addr;
protected void run() throws Throwable { protected AbstractClientSocketTest() {
for (ByteBufAllocator allocator: ALLOCATORS) { super(Bootstrap.class);
int i = 0; }
for (Factory<Bootstrap> e: COMBO) {
cb = e.newInstance(); @Override
addr = new InetSocketAddress(NetUtil.LOCALHOST, TestUtils.getFreePort()); protected List<TestsuitePermutation.BootstrapFactory<Bootstrap>> newFactories() {
cb.remoteAddress(addr); return SocketTestPermutation.clientSocket();
cb.option(ChannelOption.ALLOCATOR, allocator); }
logger.info(String.format(
"Running: %s %d of %d with %s", @Override
testName.getMethodName(), ++ i, COMBO.size(), StringUtil.simpleClassName(allocator))); protected void configure(Bootstrap bootstrap, ByteBufAllocator allocator) {
try { addr = new InetSocketAddress(NetUtil.LOCALHOST, TestUtils.getFreePort());
Method m = getClass().getDeclaredMethod(TestUtils.testMethodName(testName), Bootstrap.class); bootstrap.remoteAddress(addr);
m.invoke(this, cb); bootstrap.option(ChannelOption.ALLOCATOR, allocator);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
}
}
} }
} }

View File

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

View File

@ -18,53 +18,33 @@ package io.netty.testsuite.transport.socket;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelOption; 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.testsuite.util.TestUtils;
import io.netty.util.NetUtil; 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.net.InetSocketAddress;
import java.util.List; 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 volatile InetSocketAddress addr;
protected void run() throws Throwable { protected AbstractServerSocketTest() {
for (ByteBufAllocator allocator: ALLOCATORS) { super(ServerBootstrap.class);
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);
logger.info(String.format( @Override
"Running: %s %d of %d (%s) with %s", protected List<TestsuitePermutation.BootstrapFactory<ServerBootstrap>> newFactories() {
testName.getMethodName(), ++ i, COMBO.size(), sb, StringUtil.simpleClassName(allocator))); return SocketTestPermutation.serverSocket();
try { }
Method m = getClass().getDeclaredMethod(TestUtils.testMethodName(testName), ServerBootstrap.class);
m.invoke(this, sb); @Override
} catch (InvocationTargetException ex) { protected void configure(ServerBootstrap bootstrap, ByteBufAllocator allocator) {
throw ex.getCause(); 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.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelOption; 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.testsuite.util.TestUtils;
import io.netty.util.NetUtil; 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.net.InetSocketAddress;
import java.util.List; 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 InetSocketAddress addr;
protected volatile Factory<Bootstrap> currentBootstrap;
protected void run() throws Throwable { protected AbstractSocketTest() {
for (ByteBufAllocator allocator: ALLOCATORS) { super(ServerBootstrap.class, Bootstrap.class);
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( @Override
"Running: %s %d of %d (%s + %s) with %s", protected List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> newFactories() {
testName.getMethodName(), ++ i, COMBO.size(), sb, cb, StringUtil.simpleClassName(allocator))); return SocketTestPermutation.socket();
try { }
Method m = getClass().getDeclaredMethod(
TestUtils.testMethodName(testName), ServerBootstrap.class, Bootstrap.class); @Override
m.invoke(this, sb, cb); protected void configure(ServerBootstrap bootstrap, Bootstrap bootstrap2, ByteBufAllocator allocator) {
} catch (InvocationTargetException ex) { addr = new InetSocketAddress(
throw ex.getCause(); 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.Bootstrap;
import io.netty.bootstrap.ChannelFactory; import io.netty.bootstrap.ChannelFactory;
import io.netty.bootstrap.ServerBootstrap; 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.Channel;
import io.netty.channel.EventLoopGroup; import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
@ -32,13 +29,18 @@ import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.oio.OioDatagramChannel; import io.netty.channel.socket.oio.OioDatagramChannel;
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 io.netty.testsuite.transport.TestsuitePermutation.BootstrapComboFactory;
import io.netty.testsuite.transport.TestsuitePermutation.BootstrapFactory;
import io.netty.util.concurrent.DefaultThreadFactory; import io.netty.util.concurrent.DefaultThreadFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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 BOSSES = 2;
private static final int WORKERS = 3; private static final int WORKERS = 3;
private static final EventLoopGroup nioBossGroup = private static final EventLoopGroup nioBossGroup =
@ -50,35 +52,30 @@ final class SocketTestPermutation {
private static final EventLoopGroup oioWorkerGroup = private static final EventLoopGroup oioWorkerGroup =
new OioEventLoopGroup(Integer.MAX_VALUE, new DefaultThreadFactory("testsuite-oio-worker", true)); new OioEventLoopGroup(Integer.MAX_VALUE, new DefaultThreadFactory("testsuite-oio-worker", true));
static List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> socket() { static List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> socket() {
List<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>> list = List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> list =
new ArrayList<Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>>(); new ArrayList<BootstrapComboFactory<ServerBootstrap, Bootstrap>>();
// Make the list of ServerBootstrap factories. // Make the list of ServerBootstrap factories.
List<Factory<ServerBootstrap>> sbfs = serverSocket(); List<BootstrapFactory<ServerBootstrap>> sbfs = serverSocket();
// Make the list of Bootstrap factories. // Make the list of Bootstrap factories.
List<Factory<Bootstrap>> cbfs = clientSocket(); List<BootstrapFactory<Bootstrap>> cbfs = clientSocket();
// Populate the combinations // Populate the combinations
for (Factory<ServerBootstrap> sbf: sbfs) { for (BootstrapFactory<ServerBootstrap> sbf: sbfs) {
for (Factory<Bootstrap> cbf: cbfs) { for (BootstrapFactory<Bootstrap> cbf: cbfs) {
final Factory<ServerBootstrap> sbf0 = sbf; final BootstrapFactory<ServerBootstrap> sbf0 = sbf;
final Factory<Bootstrap> cbf0 = cbf; final BootstrapFactory<Bootstrap> cbf0 = cbf;
list.add(new Entry<Factory<ServerBootstrap>, Factory<Bootstrap>>() { list.add(new BootstrapComboFactory<ServerBootstrap, Bootstrap>() {
@Override @Override
public Factory<ServerBootstrap> getKey() { public ServerBootstrap newServerInstance() {
return sbf0; return sbf0.newInstance();
} }
@Override @Override
public Factory<Bootstrap> getValue() { public Bootstrap newClientInstance() {
return cbf0; return cbf0.newInstance();
}
@Override
public Factory<Bootstrap> setValue(Factory<Bootstrap> value) {
throw new UnsupportedOperationException();
} }
}); });
} }
@ -90,14 +87,14 @@ final class SocketTestPermutation {
return list; return list;
} }
static List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> datagram() { static List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
List<Entry<Factory<Bootstrap>, Factory<Bootstrap>>> list = List<BootstrapComboFactory<Bootstrap, Bootstrap>> list =
new ArrayList<Entry<Factory<Bootstrap>, Factory<Bootstrap>>>(); new ArrayList<BootstrapComboFactory<Bootstrap, Bootstrap>>();
// Make the list of Bootstrap factories. // Make the list of Bootstrap factories.
List<Factory<Bootstrap>> bfs = List<BootstrapFactory<Bootstrap>> bfs =
new ArrayList<Factory<Bootstrap>>(); new ArrayList<BootstrapFactory<Bootstrap>>();
bfs.add(new Factory<Bootstrap>() { bfs.add(new BootstrapFactory<Bootstrap>() {
@Override @Override
public Bootstrap newInstance() { public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() { return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@ -113,7 +110,7 @@ final class SocketTestPermutation {
}); });
} }
}); });
bfs.add(new Factory<Bootstrap>() { bfs.add(new BootstrapFactory<Bootstrap>() {
@Override @Override
public Bootstrap newInstance() { public Bootstrap newInstance() {
return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class); return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class);
@ -121,24 +118,19 @@ final class SocketTestPermutation {
}); });
// Populate the combinations // Populate the combinations
for (Factory<Bootstrap> sbf: bfs) { for (BootstrapFactory<Bootstrap> sbf: bfs) {
for (Factory<Bootstrap> cbf: bfs) { for (BootstrapFactory<Bootstrap> cbf: bfs) {
final Factory<Bootstrap> sbf0 = sbf; final BootstrapFactory<Bootstrap> sbf0 = sbf;
final Factory<Bootstrap> cbf0 = cbf; final BootstrapFactory<Bootstrap> cbf0 = cbf;
list.add(new Entry<Factory<Bootstrap>, Factory<Bootstrap>>() { list.add(new BootstrapComboFactory<Bootstrap, Bootstrap>() {
@Override @Override
public Factory<Bootstrap> getKey() { public Bootstrap newServerInstance() {
return sbf0; return sbf0.newInstance();
} }
@Override @Override
public Factory<Bootstrap> getValue() { public Bootstrap newClientInstance() {
return cbf0; return cbf0.newInstance();
}
@Override
public Factory<Bootstrap> setValue(Factory<Bootstrap> value) {
throw new UnsupportedOperationException();
} }
}); });
} }
@ -147,18 +139,18 @@ final class SocketTestPermutation {
return list; return list;
} }
static List<Factory<ServerBootstrap>> serverSocket() { static List<BootstrapFactory<ServerBootstrap>> serverSocket() {
List<Factory<ServerBootstrap>> list = new ArrayList<Factory<ServerBootstrap>>(); List<BootstrapFactory<ServerBootstrap>> list = new ArrayList<BootstrapFactory<ServerBootstrap>>();
// Make the list of ServerBootstrap factories. // Make the list of ServerBootstrap factories.
list.add(new Factory<ServerBootstrap>() { list.add(new BootstrapFactory<ServerBootstrap>() {
@Override @Override
public ServerBootstrap newInstance() { public ServerBootstrap newInstance() {
return new ServerBootstrap().group(nioBossGroup, nioWorkerGroup) return new ServerBootstrap().group(nioBossGroup, nioWorkerGroup)
.channel(NioServerSocketChannel.class); .channel(NioServerSocketChannel.class);
} }
}); });
list.add(new Factory<ServerBootstrap>() { list.add(new BootstrapFactory<ServerBootstrap>() {
@Override @Override
public ServerBootstrap newInstance() { public ServerBootstrap newInstance() {
return new ServerBootstrap().group(oioBossGroup, oioWorkerGroup) return new ServerBootstrap().group(oioBossGroup, oioWorkerGroup)
@ -169,15 +161,15 @@ final class SocketTestPermutation {
return list; return list;
} }
static List<Factory<Bootstrap>> clientSocket() { static List<BootstrapFactory<Bootstrap>> clientSocket() {
List<Factory<Bootstrap>> list = new ArrayList<Factory<Bootstrap>>(); List<BootstrapFactory<Bootstrap>> list = new ArrayList<BootstrapFactory<Bootstrap>>();
list.add(new Factory<Bootstrap>() { list.add(new BootstrapFactory<Bootstrap>() {
@Override @Override
public Bootstrap newInstance() { public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channel(NioSocketChannel.class); return new Bootstrap().group(nioWorkerGroup).channel(NioSocketChannel.class);
} }
}); });
list.add(new Factory<Bootstrap>() { list.add(new BootstrapFactory<Bootstrap>() {
@Override @Override
public Bootstrap newInstance() { public Bootstrap newInstance() {
return new Bootstrap().group(oioWorkerGroup).channel(OioSocketChannel.class); return new Bootstrap().group(oioWorkerGroup).channel(OioSocketChannel.class);
@ -186,16 +178,4 @@ final class SocketTestPermutation {
return list; 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();
}
} }