Class TcpServer


  • public class TcpServer
    extends java.lang.Object
    A utility to create a TCP server for tests.
    • Constructor Detail

      • TcpServer

        public TcpServer()
        New instance with default configuration.
      • TcpServer

        public TcpServer​(TcpServerConfig config)
        New instance.
        Parameters:
        config - for the server.
    • Method Detail

      • bind

        public ServerContext bind​(ExecutionContext executionContext,
                                  int port,
                                  @Nullable
                                  ConnectionAcceptor connectionAcceptor,
                                  java.util.function.Function<NettyConnection<Buffer,​Buffer>,​Completable> service,
                                  ExecutionStrategy executionStrategy)
                           throws java.util.concurrent.ExecutionException,
                                  java.lang.InterruptedException
        Starts the server at the passed port and invoke the passed service for each accepted connection. Awaits for the server to start.
        Parameters:
        executionContext - ExecutionContext to use for incoming connections.
        port - Port for the server.
        connectionAcceptor - to use for filtering accepted connections. The returned ServerContext manages the lifecycle of the connectionAcceptor, ensuring it is closed when the ServerContext is closed.
        service - Function that is invoked for each accepted connection.
        executionStrategy - ExecutionStrategy to use.
        Returns:
        ServerContext for the started server.
        Throws:
        java.util.concurrent.ExecutionException - If the server start failed.
        java.lang.InterruptedException - If the calling thread was interrupted waiting for the server to start.
      • getServerPort

        public static int getServerPort​(ServerContext context)
        Returns the listening port for the server represented by ServerContext.
        Parameters:
        context - for the server.
        Returns:
        Listening port.
        Throws:
        java.lang.ClassCastException - If the SocketAddress returned by ServerContext.listenAddress() is not an InetSocketAddress.