use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class ConnectionAcceptorOffloadingTest method testAcceptorOffloading.
@ParameterizedTest
@ValueSource(booleans = { false, true })
void testAcceptorOffloading(boolean offload) throws Exception {
AtomicReference<Boolean> offloaded = new AtomicReference<>();
ConnectionAcceptorFactory factory = ConnectionAcceptorFactory.withStrategy(original -> context -> {
boolean isIoThread = IoThreadFactory.IoThread.currentThreadIsIoThread();
offloaded.set(!isIoThread);
return original.accept(context);
}, offload ? ConnectExecutionStrategy.offloadAll() : ConnectExecutionStrategy.offloadNone());
try (ServerContext server = HttpServers.forPort(0).appendConnectionAcceptorFilter(factory).listenAndAwait(this::helloWorld)) {
SocketAddress serverAddress = server.listenAddress();
try (BlockingHttpClient client = HttpClients.forResolvedAddress(serverAddress).buildBlocking()) {
HttpResponse response = client.request(client.get("/sayHello"));
assertThat("unexpected status", response.status(), is(HttpResponseStatus.OK));
}
}
assertThat("factory was not invoked", offloaded.get(), is(notNullValue()));
assertThat("incorrect offloading", offloaded.get(), is(offload));
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class ConsumeRequestPayloadOnResponsePathTest method test.
private void test(final BiFunction<Single<StreamingHttpResponse>, StreamingHttpRequest, Single<StreamingHttpResponse>> consumeRequestPayload) throws Exception {
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).appendServiceFilter(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
return consumeRequestPayload.apply(delegate().handle(ctx, request, responseFactory), request);
}
}).listenStreamingAndAwait((ctx, request, responseFactory) -> {
final StreamingHttpResponse response = responseFactory.ok().addHeader(TRAILER, X_TOTAL_LENGTH).payloadBody(from("Response\n", "Payload\n", "Body\n"), appSerializerUtf8FixLen()).transform(new TrailersTransformer<AtomicInteger, Buffer>() {
@Override
public AtomicInteger newState() {
return new AtomicInteger();
}
@Override
public Buffer accept(final AtomicInteger total, final Buffer chunk) {
total.addAndGet(chunk.readableBytes());
return chunk;
}
@Override
public HttpHeaders payloadComplete(final AtomicInteger total, final HttpHeaders trailers) {
trailers.add(X_TOTAL_LENGTH, String.valueOf(total.get()));
return trailers;
}
@Override
public HttpHeaders catchPayloadFailure(final AtomicInteger __, final Throwable ___, final HttpHeaders trailers) {
return trailers;
}
});
return succeeded(response);
})) {
HttpResponse response;
try (BlockingHttpClient client = HttpClients.forSingleAddress(AddressUtils.serverHostAndPort(serverContext)).buildBlocking()) {
response = client.request(client.post("/").payloadBody(EXPECTED_REQUEST_PAYLOAD, textSerializerUtf8()));
serverLatch.await();
}
assertThat(response.status(), is(OK));
assertThat("Request payload body might be consumed by someone else", errorRef.get(), is(nullValue()));
assertThat(receivedPayload.toString(), is(EXPECTED_REQUEST_PAYLOAD));
assertThat(response.headers().contains(TRAILER, X_TOTAL_LENGTH), is(true));
assertThat(response.trailers().contains(X_TOTAL_LENGTH), is(true));
CharSequence trailerLength = response.trailers().get(X_TOTAL_LENGTH);
assertNotNull(trailerLength);
assertThat("Unexpected response payload: '" + response.payloadBody().toString(UTF_8) + "'", trailerLength.toString(), is(Integer.toString(response.payloadBody().readableBytes())));
}
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class BasicAuthStrategyInfluencerTest method noOffloadsInfluence.
@Test
void noOffloadsInfluence() throws Exception {
BlockingHttpClient client = setup(true);
assert service != null;
HttpResponse response = makeRequest(client);
assertThat("Unexpected response.", response.status().code(), is(200));
service.assertHandleOffload(startsWith(IO_EXECUTOR_NAME_PREFIX));
service.assertRequestOffload(startsWith(IO_EXECUTOR_NAME_PREFIX));
service.assertResponseOffload(startsWith(IO_EXECUTOR_NAME_PREFIX));
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class BlockingHelloWorldClient method main.
public static void main(String[] args) throws Exception {
try (BlockingHttpClient client = HttpClients.forSingleAddress("localhost", 8080).buildBlocking()) {
HttpResponse response = client.request(client.get("/sayHello"));
System.out.println(response.toString((name, value) -> value));
System.out.println(response.payloadBody(textSerializerUtf8()));
}
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class DefaultGrpcClientCallFactory method newBlockingCall.
@Override
public <Req, Resp> BlockingClientCall<Req, Resp> newBlockingCall(final MethodDescriptor<Req, Resp> methodDescriptor, final BufferDecoderGroup decompressors) {
final BlockingHttpClient client = streamingHttpClient.asBlockingClient();
GrpcSerializer<Req> serializerIdentity = serializer(methodDescriptor);
GrpcDeserializer<Resp> deserializerIdentity = deserializer(methodDescriptor);
List<GrpcDeserializer<Resp>> deserializers = deserializers(methodDescriptor, decompressors.decoders());
CharSequence acceptedEncoding = decompressors.advertisedMessageEncoding();
CharSequence requestContentType = grpcContentType(methodDescriptor.requestDescriptor().serializerDescriptor().contentType());
CharSequence responseContentType = grpcContentType(methodDescriptor.responseDescriptor().serializerDescriptor().contentType());
return (metadata, request) -> {
Duration timeout = timeoutForRequest(metadata.timeout());
GrpcSerializer<Req> serializer = serializer(methodDescriptor, serializerIdentity, metadata.requestCompressor());
String mdPath = methodDescriptor.httpPath();
HttpRequest httpRequest = client.post(UNKNOWN_PATH.equals(mdPath) ? metadata.path() : mdPath);
initRequest(httpRequest, requestContentType, serializer.messageEncoding(), acceptedEncoding, timeout);
httpRequest.payloadBody(serializer.serialize(request, client.executionContext().bufferAllocator()));
try {
assignStrategy(httpRequest, metadata);
final HttpResponse response = client.request(httpRequest);
return validateResponseAndGetPayload(response, responseContentType, client.executionContext().bufferAllocator(), readGrpcMessageEncodingRaw(response.headers(), deserializerIdentity, deserializers, GrpcDeserializer::messageEncoding));
} catch (Throwable cause) {
throw toGrpcException(cause);
}
};
}
Aggregations