Search in sources :

Example 51 with Single

use of io.servicetalk.concurrent.api.Single in project servicetalk by apple.

the class HttpLifecycleObserverTest method testConnectionFailsRequestBeforeWrite.

@ParameterizedTest(name = "{displayName} [{index}] protocol={0}")
@EnumSource(HttpProtocol.class)
void testConnectionFailsRequestBeforeWrite(HttpProtocol protocol) throws Exception {
    connectionFilterFactory(client -> new StreamingHttpConnectionFilter(client) {

        @Override
        public Single<StreamingHttpResponse> request(StreamingHttpRequest request) {
            return failed(DELIBERATE_EXCEPTION);
        }
    });
    setUp(protocol);
    ExecutionException e = assertThrows(ExecutionException.class, () -> makeRequestAndAssertResponse(SVC_ECHO, protocol, OK, CONTENT.readableBytes()));
    assertThat(e.getCause(), sameInstance(DELIBERATE_EXCEPTION));
    // server is not involved in this test, count down manually
    bothTerminate.countDown();
    bothTerminate.await();
    clientInOrder.verify(clientLifecycleObserver).onNewExchange();
    clientInOrder.verify(clientExchangeObserver).onRequest(any(StreamingHttpRequest.class));
    clientInOrder.verify(clientExchangeObserver).onConnectionSelected(any(ConnectionInfo.class));
    clientInOrder.verify(clientExchangeObserver).onResponseError(e.getCause());
    clientInOrder.verify(clientExchangeObserver).onExchangeFinally();
    verifyNoMoreInteractions(clientLifecycleObserver, clientExchangeObserver);
    verifyNoInteractions(clientRequestObserver, clientResponseObserver, serverLifecycleObserver, serverExchangeObserver, serverRequestObserver, serverResponseObserver);
}
Also used : StreamingHttpConnectionFilter(io.servicetalk.http.api.StreamingHttpConnectionFilter) Single(io.servicetalk.concurrent.api.Single) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ConnectionInfo(io.servicetalk.transport.api.ConnectionInfo) ExecutionException(java.util.concurrent.ExecutionException) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 52 with Single

use of io.servicetalk.concurrent.api.Single in project servicetalk by apple.

the class TrailersOnlyErrorTest method testServiceFilterThrows.

@Test
void testServiceFilterThrows() throws Exception {
    final BlockingQueue<Throwable> asyncErrors = new LinkedBlockingDeque<>();
    final TesterService service = mockTesterService();
    final GrpcServerBuilder serverBuilder = GrpcServers.forAddress(localAddress(0)).initializeHttp(builder -> builder.appendServiceFilter(svc -> new StreamingHttpServiceFilter(svc) {

        @Override
        public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
            throw DELIBERATE_EXCEPTION;
        }
    }));
    try (ServerContext serverContext = serverBuilder.listenAndAwait(new Tester.ServiceFactory(service))) {
        final GrpcClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = GrpcClients.forAddress(serverHostAndPort(serverContext)).initializeHttp(builder -> builder.appendClientFilter(__ -> true, setupResponseVerifierFilter(asyncErrors)));
        try (TesterClient client = clientBuilder.build(new Tester.ClientFactory())) {
            verifyException(client.test(TestRequest.newBuilder().build()).toFuture(), UNKNOWN);
            assertNoAsyncErrors(asyncErrors);
            verifyException(client.testRequestStream(Publisher.from(TestRequest.newBuilder().build())).toFuture(), UNKNOWN);
            assertNoAsyncErrors(asyncErrors);
            verifyException(client.testResponseStream(TestRequest.newBuilder().build()).toFuture(), UNKNOWN);
            assertNoAsyncErrors(asyncErrors);
            verifyException(client.testBiDiStream(from(TestRequest.newBuilder().build()).concat(never())).toFuture(), UNKNOWN);
            assertNoAsyncErrors(asyncErrors);
            verifyException(client.testBiDiStream(from(TestRequest.newBuilder().build())).toFuture(), UNKNOWN);
            assertNoAsyncErrors(asyncErrors);
        }
    }
}
Also used : GrpcServerBuilder(io.servicetalk.grpc.api.GrpcServerBuilder) TestUtils.assertNoAsyncErrors(io.servicetalk.test.resources.TestUtils.assertNoAsyncErrors) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) UNKNOWN(io.servicetalk.grpc.api.GrpcStatusCode.UNKNOWN) GrpcStatusCode(io.servicetalk.grpc.api.GrpcStatusCode) HelloRequest(io.grpc.examples.helloworld.HelloRequest) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) GreeterClient(io.grpc.examples.helloworld.Greeter.GreeterClient) GrpcStatusException(io.servicetalk.grpc.api.GrpcStatusException) Future(java.util.concurrent.Future) UNIMPLEMENTED(io.servicetalk.grpc.api.GrpcStatusCode.UNIMPLEMENTED) TesterClient(io.servicetalk.grpc.netty.TesterProto.Tester.TesterClient) Greeter(io.grpc.examples.helloworld.Greeter) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) BlockingQueue(java.util.concurrent.BlockingQueue) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test) Matchers.equalTo(org.hamcrest.Matchers.equalTo) GrpcClientBuilder(io.servicetalk.grpc.api.GrpcClientBuilder) Mockito.mock(org.mockito.Mockito.mock) Publisher.never(io.servicetalk.concurrent.api.Publisher.never) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Publisher(io.servicetalk.concurrent.api.Publisher) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) GrpcServerBuilder(io.servicetalk.grpc.api.GrpcServerBuilder) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Single(io.servicetalk.concurrent.api.Single) TesterService(io.servicetalk.grpc.netty.TesterProto.Tester.TesterService) TestRequest(io.servicetalk.grpc.netty.TesterProto.TestRequest) Mockito.when(org.mockito.Mockito.when) ExecutionException(java.util.concurrent.ExecutionException) GRPC_STATUS(io.servicetalk.grpc.api.GrpcHeaderNames.GRPC_STATUS) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) Tester(io.servicetalk.grpc.netty.TesterProto.Tester) HostAndPort(io.servicetalk.transport.api.HostAndPort) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Tester(io.servicetalk.grpc.netty.TesterProto.Tester) InetSocketAddress(java.net.InetSocketAddress) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) TesterService(io.servicetalk.grpc.netty.TesterProto.Tester.TesterService) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) TesterClient(io.servicetalk.grpc.netty.TesterProto.Tester.TesterClient) ServerContext(io.servicetalk.transport.api.ServerContext) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 53 with Single

use of io.servicetalk.concurrent.api.Single in project servicetalk by apple.

the class ContentEncodingHttpRequesterFilter method applyEncodingAndDecoding.

private Single<StreamingHttpResponse> applyEncodingAndDecoding(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
    return Single.defer(() -> {
        boolean decompressResponse = false;
        CharSequence encodings = decompressors.advertisedMessageEncoding();
        if (encodings != null && !request.headers().contains(ACCEPT_ENCODING)) {
            request.headers().set(ACCEPT_ENCODING, encodings);
            decompressResponse = true;
        }
        BufferEncoder encoder = request.contentEncoding();
        final StreamingHttpRequest encodedRequest;
        if (encoder != null && !identityEncoder().equals(encoder)) {
            addContentEncoding(request.headers(), encoder.encodingName());
            encodedRequest = request.transformPayloadBody(pub -> encoder.streamingEncoder().serialize(pub, delegate.executionContext().bufferAllocator()));
        } else {
            encodedRequest = request;
        }
        Single<StreamingHttpResponse> respSingle = delegate.request(encodedRequest);
        return (decompressResponse ? respSingle.map(response -> {
            Iterator<? extends CharSequence> contentEncodingItr = response.headers().valuesIterator(CONTENT_ENCODING);
            final boolean hasContentEncoding = contentEncodingItr.hasNext();
            if (!hasContentEncoding) {
                return response;
            }
            BufferDecoder decoder = matchAndRemoveEncoding(decompressors.decoders(), BufferDecoder::encodingName, contentEncodingItr, response.headers());
            if (decoder == null) {
                throw new UnsupportedContentEncodingException(response.headers().get(CONTENT_ENCODING, "<null>").toString());
            }
            return response.transformPayloadBody(pub -> decoder.streamingDecoder().deserialize(pub, delegate.executionContext().bufferAllocator()));
        }) : respSingle).shareContextOnSubscribe();
    });
}
Also used : ACCEPT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.ACCEPT_ENCODING) Iterator(java.util.Iterator) HeaderUtils.addContentEncoding(io.servicetalk.http.api.HeaderUtils.addContentEncoding) Single(io.servicetalk.concurrent.api.Single) Objects.requireNonNull(java.util.Objects.requireNonNull) BufferDecoderGroup(io.servicetalk.encoding.api.BufferDecoderGroup) ContentEncodingHttpServiceFilter.matchAndRemoveEncoding(io.servicetalk.http.api.ContentEncodingHttpServiceFilter.matchAndRemoveEncoding) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) Identity.identityEncoder(io.servicetalk.encoding.api.Identity.identityEncoder) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder) CONTENT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.CONTENT_ENCODING) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) Iterator(java.util.Iterator) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder)

Aggregations

Single (io.servicetalk.concurrent.api.Single)53 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)32 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)32 Nullable (javax.annotation.Nullable)26 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)24 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)22 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)21 OK (io.servicetalk.http.api.HttpResponseStatus.OK)20 Buffer (io.servicetalk.buffer.api.Buffer)19 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)19 StreamingHttpServiceFilter (io.servicetalk.http.api.StreamingHttpServiceFilter)18 Matchers.is (org.hamcrest.Matchers.is)18 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 Publisher (io.servicetalk.concurrent.api.Publisher)17 HttpResponse (io.servicetalk.http.api.HttpResponse)17 ServerContext (io.servicetalk.transport.api.ServerContext)17 InetSocketAddress (java.net.InetSocketAddress)16 Test (org.junit.jupiter.api.Test)16 Completable (io.servicetalk.concurrent.api.Completable)15 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)15