netty5/testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketTestPermutation.java
田欧 934a07fbe2 migrate java8 (#8779)
Motivation:

We can omit argument types when using Java8.

Modification:

Omit arguments where possible.

Result:

Cleaner code.
2019-01-28 05:55:30 +01:00

162 lines
6.3 KiB
Java

/*
* 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.AbstractBootstrap;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFactory;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.MultithreadEventLoopGroup;
import io.netty.channel.nio.NioHandler;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.testsuite.transport.TestsuitePermutation.BootstrapComboFactory;
import io.netty.testsuite.transport.TestsuitePermutation.BootstrapFactory;
import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SocketTestPermutation {
static final String BAD_HOST = SystemPropertyUtil.get("io.netty.testsuite.badHost", "198.51.100.254");
static final int BAD_PORT = SystemPropertyUtil.getInt("io.netty.testsuite.badPort", 65535);
static {
InternalLogger logger = InternalLoggerFactory.getInstance(SocketConnectionAttemptTest.class);
logger.debug("-Dio.netty.testsuite.badHost: {}", BAD_HOST);
logger.debug("-Dio.netty.testsuite.badPort: {}", BAD_PORT);
}
static final SocketTestPermutation INSTANCE = new SocketTestPermutation();
protected static final int BOSSES = 2;
protected static final int WORKERS = 3;
protected final EventLoopGroup nioBossGroup =
new MultithreadEventLoopGroup(BOSSES, new DefaultThreadFactory("testsuite-nio-boss", true),
NioHandler.newFactory());
protected final EventLoopGroup nioWorkerGroup =
new MultithreadEventLoopGroup(WORKERS, new DefaultThreadFactory("testsuite-nio-worker", true),
NioHandler.newFactory());
protected <A extends AbstractBootstrap<?, ?, ?>, B extends AbstractBootstrap<?, ?, ?>>
List<BootstrapComboFactory<A, B>> combo(List<BootstrapFactory<A>> sbfs, List<BootstrapFactory<B>> cbfs) {
List<BootstrapComboFactory<A, B>> list = new ArrayList<>();
// Populate the combinations
for (BootstrapFactory<A> sbf: sbfs) {
for (BootstrapFactory<B> cbf: cbfs) {
final BootstrapFactory<A> sbf0 = sbf;
final BootstrapFactory<B> cbf0 = cbf;
list.add(new BootstrapComboFactory<A, B>() {
@Override
public A newServerInstance() {
return sbf0.newInstance();
}
@Override
public B newClientInstance() {
return cbf0.newInstance();
}
});
}
}
return list;
}
public List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> socket() {
// Make the list of ServerBootstrap factories.
List<BootstrapFactory<ServerBootstrap>> sbfs = serverSocket();
// Make the list of Bootstrap factories.
List<BootstrapFactory<Bootstrap>> cbfs = clientSocket();
// Populate the combinations
return combo(sbfs, cbfs);
}
public List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram() {
// Make the list of Bootstrap factories.
List<BootstrapFactory<Bootstrap>> bfs = Collections.singletonList(
new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel(EventLoop eventLoop) {
return new NioDatagramChannel(eventLoop, InternetProtocolFamily.IPv4);
}
@Override
public String toString() {
return NioDatagramChannel.class.getSimpleName() + ".class";
}
});
}
}
);
// Populare the combinations.
return combo(bfs, bfs);
}
public List<BootstrapFactory<ServerBootstrap>> serverSocket() {
return Collections.singletonList(
new BootstrapFactory<ServerBootstrap>() {
@Override
public ServerBootstrap newInstance() {
return new ServerBootstrap().group(nioBossGroup, nioWorkerGroup)
.channel(NioServerSocketChannel.class);
}
}
);
}
public List<BootstrapFactory<Bootstrap>> clientSocket() {
return Collections.singletonList(
new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channel(NioSocketChannel.class);
}
}
);
}
public List<BootstrapFactory<Bootstrap>> datagramSocket() {
return Collections.singletonList(
new BootstrapFactory<Bootstrap>() {
@Override
public Bootstrap newInstance() {
return new Bootstrap().group(nioWorkerGroup).channel(NioDatagramChannel.class);
}
}
);
}
}