All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.netty.testsuite.transport.socket.SocketTestPermutation Maven / Gradle / Ivy

The newest version!
/*
 * 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.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.oio.OioEventLoopGroup;
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.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.DefaultExecutorServiceFactory;
import io.netty.util.concurrent.DefaultThreadFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SocketTestPermutation {

    static final SocketTestPermutation INSTANCE = new SocketTestPermutation();

    protected static final int BOSSES = 2;
    protected static final int WORKERS = 3;

    protected static final int OIO_SO_TIMEOUT = 10;  // Use short timeout for faster runs.

    protected final EventLoopGroup nioBossGroup =
            new NioEventLoopGroup(BOSSES, new DefaultExecutorServiceFactory("testsuite-nio-boss"));
    protected final EventLoopGroup nioWorkerGroup =
            new NioEventLoopGroup(WORKERS, new DefaultExecutorServiceFactory("testsuite-nio-worker"));
    protected final EventLoopGroup oioBossGroup =
            new OioEventLoopGroup(Integer.MAX_VALUE, new DefaultThreadFactory("testsuite-oio-boss", true));
    protected final EventLoopGroup oioWorkerGroup =
            new OioEventLoopGroup(Integer.MAX_VALUE, new DefaultThreadFactory("testsuite-oio-worker", true));

    protected , B extends AbstractBootstrap>
    List> combo(List> sbfs, List> cbfs) {

        List> list = new ArrayList>();

        // Populate the combinations
        for (BootstrapFactory sbf: sbfs) {
            for (BootstrapFactory cbf: cbfs) {
                final BootstrapFactory sbf0 = sbf;
                final BootstrapFactory cbf0 = cbf;
                list.add(new BootstrapComboFactory() {
                    @Override
                    public A newServerInstance() {
                        return sbf0.newInstance();
                    }

                    @Override
                    public B newClientInstance() {
                        return cbf0.newInstance();
                    }
                });
            }
        }

        return list;
    }

    public List> socket() {
        // Make the list of ServerBootstrap factories.
        List> sbfs = serverSocket();

        // Make the list of Bootstrap factories.
        List> cbfs = clientSocket();

        // Populate the combinations
        List> list = combo(sbfs, cbfs);

        // Remove the OIO-OIO case which often leads to a dead lock by its nature.
        list.remove(list.size() - 1);

        return list;
    }

    public List> datagram() {
        // Make the list of Bootstrap factories.
        List> bfs = Arrays.asList(
                new BootstrapFactory() {
                    @Override
                    public Bootstrap newInstance() {
                        return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory() {
                            @Override
                            public Channel newChannel() {
                                return new NioDatagramChannel(InternetProtocolFamily.IPv4);
                            }

                            @Override
                            public String toString() {
                                return NioDatagramChannel.class.getSimpleName() + ".class";
                            }
                        });
                    }
                },
                new BootstrapFactory() {
                    @Override
                    public Bootstrap newInstance() {
                        return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class);
                    }
                }
        );

        // Populare the combinations.
        return combo(bfs, bfs);
    }

    public List> serverSocket() {
        return Arrays.asList(
                new BootstrapFactory() {
                    @Override
                    public ServerBootstrap newInstance() {
                        return new ServerBootstrap().group(nioBossGroup, nioWorkerGroup)
                                .channel(NioServerSocketChannel.class);
                    }
                },
                new BootstrapFactory() {
                    @Override
                    public ServerBootstrap newInstance() {
                        return new ServerBootstrap().group(oioBossGroup, oioWorkerGroup)
                                .channel(OioServerSocketChannel.class)
                                .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
                    }
                }
        );
    }

    public List> clientSocket() {
        return Arrays.asList(
                new BootstrapFactory() {
                    @Override
                    public Bootstrap newInstance() {
                        return new Bootstrap().group(nioWorkerGroup).channel(NioSocketChannel.class);
                    }
                },
                new BootstrapFactory() {
                    @Override
                    public Bootstrap newInstance() {
                        return new Bootstrap().group(oioWorkerGroup).channel(OioSocketChannel.class)
                                .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
                    }
                }
        );
    }
}