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.
        config - for the server.
    • Method Detail

      • bind

        public ServerContext bind​(ExecutionContext executionContext,
                                  int port,
                                  ConnectionAcceptor connectionAcceptor,
                                  java.util.function.Function<NettyConnection<Buffer,​Buffer>,​Completable> service,
                                  ExecutionStrategy executionStrategy)
                           throws java.util.concurrent.ExecutionException,
        Starts the server at the passed port and invoke the passed service for each accepted connection. Awaits for the server to start.
        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.
        ServerContext for the started server.
        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.
        context - for the server.
        Listening port.
        java.lang.ClassCastException - If the SocketAddress returned by ServerContext.listenAddress() is not an InetSocketAddress.