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));
}
}
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class MalformedDataAfterHttpMessageTest method afterRequest.
@Test
void afterRequest() throws Exception {
try (ServerContext server = stServer();
BlockingHttpClient client = stClient(server.listenAddress())) {
Buffer malformedBody = client.executionContext().bufferAllocator().fromAscii(CONTENT).writeShort(// malformed data at the end of the request msg
0);
HttpRequest request = client.post("/").setHeader(CONTENT_LENGTH, valueOf(CONTENT.length())).setHeader(CONTENT_TYPE, TEXT_PLAIN).payloadBody(malformedBody);
ReservedBlockingHttpConnection connection = client.reserveConnection(request);
CountDownLatch connectionClosedLatch = new CountDownLatch(1);
connection.connectionContext().onClose().whenFinally(connectionClosedLatch::countDown).subscribe();
assertThrows(IOException.class, () -> connection.request(request));
// Server should close the connection:
connectionClosedLatch.await();
}
}
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 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 AbstractHttpServiceAsyncContextTest method contextPreservedOverFilterBoundaries.
final void contextPreservedOverFilterBoundaries(boolean useImmediate, boolean asyncFilter, boolean asyncService) throws Exception {
Queue<Throwable> errorQueue = new ConcurrentLinkedQueue<>();
try (ServerContext ctx = serverWithService(HttpServers.forAddress(localAddress(0)).appendServiceFilter(filterFactory(useImmediate, asyncFilter, errorQueue)), useImmediate, asyncService);
StreamingHttpClient client = HttpClients.forResolvedAddress(serverHostAndPort(ctx)).buildStreaming();
StreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get()) {
makeClientRequestWithId(connection, "1");
assertThat("Error queue is not empty!", errorQueue, empty());
}
}
Aggregations