use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class NettyHttpServerConnectionDrainTest method requestTimesOutWithoutAutoDrainingOrUserConsuming.
@Disabled("https://github.com/apple/servicetalk/issues/981")
@Test
void requestTimesOutWithoutAutoDrainingOrUserConsuming() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
StreamingHttpClient client = null;
try (ServerContext serverContext = server(false, respondOkWithoutReadingRequest(latch::countDown))) {
client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).buildStreaming();
client.request(client.post("/").payloadBody(from(LARGE_TEXT), appSerializerUtf8FixLen())).ignoreElement().subscribe();
// Wait till the request is received
assertThrows(TimeoutException.class, latch::await);
// before initiating graceful close of the server
} finally {
closeClient(client);
}
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class NettyHttpServerConnectionDrainTest method requestIsAutoDrainedWhenUserFailsToConsume.
@Disabled("https://github.com/apple/servicetalk/issues/981")
@Test
void requestIsAutoDrainedWhenUserFailsToConsume() throws Exception {
BlockingHttpClient client = null;
try (ServerContext serverContext = server(true, respondOkWithoutReadingRequest())) {
client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).buildBlocking();
postLargePayloadAndAssertResponseOk(client);
} finally {
closeClient(client);
}
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class NettyHttpServerConnectionTest method updateFlushStrategy.
@ParameterizedTest(name = "server={0} client={1}")
@MethodSource("executionStrategies")
void updateFlushStrategy(HttpExecutionStrategy serverExecutionStrategy, HttpExecutionStrategy clientExecutionStrategy) throws Exception {
customStrategy = new MockFlushStrategy();
AtomicReference<Cancellable> customCancellableRef = new AtomicReference<>();
AtomicBoolean handledFirstRequest = new AtomicBoolean();
serverContext = HttpServers.forAddress(localAddress(0)).ioExecutor(contextRule.ioExecutor()).appendConnectionAcceptorFilter(original -> original.append(ctx -> {
customCancellableRef.set(((NettyConnectionContext) ctx).updateFlushStrategy((__, ___) -> customStrategy));
return completed();
})).executionStrategy(serverExecutionStrategy).listenStreaming((ctx, request, responseFactory) -> {
if (handledFirstRequest.compareAndSet(false, true)) {
customStrategy.afterFirstWrite(FlushStrategy.FlushSender::flush);
return succeeded(responseFactory.ok().payloadBody(responsePublisher));
}
return succeeded(responseFactory.ok().payloadBody(responsePublisher2));
}).toFuture().get();
client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).executionStrategy(clientExecutionStrategy).buildStreaming();
StreamingHttpResponse response = client.request(client.newRequest(GET, "/1")).toFuture().get();
FlushStrategy.FlushSender customFlushSender = customStrategy.verifyApplied();
Cancellable customCancellable = customCancellableRef.get();
assertNotNull(customCancellable);
// Verify that the custom strategy is applied and used for flushing.
customStrategy.verifyWriteStarted();
customStrategy.verifyItemWritten(1);
customStrategy.verifyNoMoreInteractions();
String payloadBodyString = "foo";
TestSubscription testSubscription1 = new TestSubscription();
responsePublisher.onSubscribe(testSubscription1);
testSubscription1.awaitRequestN(1);
responsePublisher.onNext(DEFAULT_ALLOCATOR.fromAscii(payloadBodyString));
responsePublisher.onComplete();
customFlushSender.flush();
Buffer responsePayload = response.payloadBody().collect(DEFAULT_ALLOCATOR::newBuffer, (results, current) -> {
results.writeBytes(current);
return results;
}).toFuture().get();
assertEquals(payloadBodyString, responsePayload.toString(US_ASCII));
customStrategy.verifyItemWritten(2);
customStrategy.verifyWriteTerminated();
// Restore the default flush strategy, which should flush on each
customCancellable.cancel();
StreamingHttpResponse response2 = client.request(client.newRequest(GET, "/2")).toFuture().get();
TestSubscription testSubscription2 = new TestSubscription();
responsePublisher2.onSubscribe(testSubscription2);
responsePublisher2.onNext(DEFAULT_ALLOCATOR.fromAscii(payloadBodyString));
responsePublisher2.onComplete();
responsePayload = response2.payloadBody().collect(DEFAULT_ALLOCATOR::newBuffer, (results, current) -> {
results.writeBytes(current);
return results;
}).toFuture().get();
assertEquals(payloadBodyString, responsePayload.toString(US_ASCII));
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class NettyHttpServerTest method testServiceThrowsReturnsErrorResponse.
@ParameterizedTest(name = "{displayName} [{index}] disableOffloading={0}")
@ValueSource(booleans = { true, false })
void testServiceThrowsReturnsErrorResponse(boolean disableOffloading) throws Exception {
// the test suite state isn't used by this individual test, but cleanup code requires it is initialized.
setUp(IMMEDIATE, IMMEDIATE);
HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0));
if (disableOffloading) {
serverBuilder.executionStrategy(offloadNone());
}
try (ServerContext serverCtx = serverBuilder.listenStreamingAndAwait((ctx, request, responseFactory) -> {
throw DELIBERATE_EXCEPTION;
});
BlockingHttpClient client = disableOffloading(HttpClients.forResolvedAddress(serverHostAndPort(serverCtx)), disableOffloading).buildBlocking()) {
HttpResponse resp = client.request(client.get("/"));
assertThat(resp.status(), is(INTERNAL_SERVER_ERROR));
}
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class PartitionedHttpClientTest method testClientGroupPartitioning.
@Test
void testClientGroupPartitioning() throws Exception {
// user partition discovery service, userId=1 => srv1 | userId=2 => srv2
try (ServerContext userDisco = HttpServers.forAddress(localAddress(0)).listenBlockingAndAwait((ctx, request, responseFactory) -> {
if ("/partition".equals(request.path())) {
String userIdParam = request.queryParameter("userId");
if (userIdParam == null || userIdParam.isEmpty()) {
return responseFactory.badRequest();
}
int userId = Integer.parseInt(userIdParam);
if (userId != 1 && userId != 2) {
return responseFactory.notFound();
}
ServerContext dSrv = userId == 1 ? srv1 : srv2;
InetSocketAddress socketAddress = (InetSocketAddress) dSrv.listenAddress();
return responseFactory.ok().payloadBody(socketAddress.getPort() + "", textSerializerUtf8());
}
return responseFactory.notFound();
})) {
try (PartitioningHttpClientWithOutOfBandDiscovery client = new PartitioningHttpClientWithOutOfBandDiscovery(userDisco)) {
StreamingHttpResponse httpResponse1 = client.request(new User(1), client.get("/foo")).toFuture().get();
StreamingHttpResponse httpResponse2 = client.request(new User(2), client.get("/foo")).toFuture().get();
MatcherAssert.assertThat(httpResponse1.status(), is(OK));
MatcherAssert.assertThat(httpResponse1.headers().get(X_SERVER), hasToString(SRV_1));
MatcherAssert.assertThat(httpResponse2.status(), is(OK));
MatcherAssert.assertThat(httpResponse2.headers().get(X_SERVER), hasToString(SRV_2));
}
}
}
Aggregations