Class TcpServer
- java.lang.Object
-
- io.servicetalk.tcp.netty.internal.TcpServer
-
public class TcpServer extends java.lang.Object
A utility to create a TCP server for tests.
-
-
Constructor Summary
Constructors Constructor Description TcpServer()
New instance with default configuration.TcpServer(TcpServerConfig config)
New instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ServerContext
bind(ExecutionContext executionContext, int port, ConnectionAcceptor connectionAcceptor, java.util.function.Function<NettyConnection<Buffer,Buffer>,Completable> service, ExecutionStrategy executionStrategy)
Starts the server at the passedport
and invoke the passedservice
for each accepted connection.static int
getServerPort(ServerContext context)
Returns the listening port for the server represented byServerContext
.
-
-
-
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 passedport
and invoke the passedservice
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 returnedServerContext
manages the lifecycle of theconnectionAcceptor
, ensuring it is closed when theServerContext
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 byServerContext
.- Parameters:
context
- for the server.- Returns:
- Listening port.
- Throws:
java.lang.ClassCastException
- If theSocketAddress
returned byServerContext.listenAddress()
is not anInetSocketAddress
.
-
-