use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class NettyHttpServerConnectionDrainTest method server.
private static ServerContext server(boolean autoDrain, StreamingHttpService handler) throws Exception {
HttpServerBuilder httpServerBuilder = HttpServers.forAddress(AddressUtils.localAddress(0));
if (!autoDrain) {
httpServerBuilder = httpServerBuilder.drainRequestPayloadBody(false);
}
ServerContext serverContext = httpServerBuilder.listenStreamingAndAwait(handler);
return new ServerContext() {
@Override
public SocketAddress listenAddress() {
return serverContext.listenAddress();
}
@Override
public ExecutionContext<?> executionContext() {
return serverContext.executionContext();
}
@Override
public void acceptConnections(final boolean accept) {
serverContext.acceptConnections(accept);
}
@Override
public Completable onClose() {
return serverContext.onClose();
}
@Override
public Completable closeAsync() {
return serverContext.closeAsync();
}
@Override
public void close() {
// Without draining the request is expected to hang, don't wait too long unless on CI
int timeoutSeconds = CI ? 15 : 1;
awaitTermination(serverContext.closeAsyncGracefully().timeout(timeoutSeconds, SECONDS).onErrorResume(t -> serverContext.closeAsync().concat(Completable.failed(t))).toFuture());
}
};
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class NettyHttpServerConnectionDrainTest method requestIsConsumedByUserWithDrainingEnabled.
@Test
void requestIsConsumedByUserWithDrainingEnabled() throws Exception {
AtomicReference<String> resultRef = new AtomicReference<>();
try (ServerContext serverContext = server(true, (ctx, request, responseFactory) -> request.payloadBody().collect(() -> new StringBuilder(LARGE_TEXT.length()), (sb, b) -> sb.append(b.toString(CharsetUtil.US_ASCII))).map(StringBuilder::toString).whenOnSuccess(resultRef::set).toCompletable().concat(succeeded(responseFactory.ok().payloadBody(from("OK"), appSerializerUtf8FixLen()))));
BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).buildBlocking()) {
postLargePayloadAndAssertResponseOk(client);
assertThat(resultRef.get(), equalTo(LARGE_TEXT));
}
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class HttpProvidersTest method testHttpServerBuilderProvider.
@Test
void testHttpServerBuilderProvider() throws Exception {
final InetSocketAddress serverAddress = localAddress(0);
TestHttpServerBuilderProvider.MODIFY_FOR_ADDRESS.set(serverAddress);
try (ServerContext serverContext = HttpServers.forAddress(serverAddress).listenStreamingAndAwait(new TestServiceStreaming())) {
assertThat(TestHttpServerBuilderProvider.BUILD_COUNTER.get(), is(1));
try (BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).buildBlocking()) {
HttpResponse response = client.request(client.get(SVC_ECHO));
assertThat(response.status(), is(OK));
assertThat(TestHttpServerBuilderProvider.CONNECTION_COUNTER.get(), is(1));
}
}
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class HttpProvidersTest method testNoProvidersForAddress.
@Test
void testNoProvidersForAddress() throws Exception {
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).listenStreamingAndAwait(new TestServiceStreaming());
BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).buildBlocking()) {
HttpResponse response = client.request(client.get(SVC_ECHO));
assertThat(response.status(), is(OK));
}
assertThat(TestHttpServerBuilderProvider.BUILD_COUNTER.get(), is(0));
assertThat(TestHttpServerBuilderProvider.CONNECTION_COUNTER.get(), is(0));
assertThat(TestSingleAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(0));
assertThat(TestSingleAddressHttpClientBuilderProvider.CONNECTION_COUNTER.get(), is(0));
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class HttpProvidersTest method testMultiAddressHttpClientBuilderProvider.
@Test
void testMultiAddressHttpClientBuilderProvider() throws Exception {
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).listenStreamingAndAwait(new TestServiceStreaming())) {
HostAndPort serverAddress = serverHostAndPort(serverContext);
TestSingleAddressHttpClientBuilderProvider.MODIFY_FOR_ADDRESS.set(serverAddress);
try (BlockingHttpClient client = HttpClients.forMultiAddressUrl().buildBlocking()) {
assertThat(TestMultiAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(1));
HttpResponse response = client.request(client.get("http://" + serverAddress + SVC_ECHO));
assertThat(response.status(), is(OK));
assertThat(TestSingleAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(1));
assertThat(TestSingleAddressHttpClientBuilderProvider.CONNECTION_COUNTER.get(), is(1));
}
}
}
Aggregations