Search in sources :

Example 1 with StreamingHttpClientFilterFactory

use of io.servicetalk.http.api.StreamingHttpClientFilterFactory in project servicetalk by apple.

the class ErrorHandlingTest method setUp.

private void setUp(TestMode testMode, GrpcExecutionStrategy serverStrategy, GrpcExecutionStrategy clientStrategy) throws Exception {
    this.testMode = testMode;
    cannedResponse = TestResponse.newBuilder().setMessage("foo").build();
    ServiceFactory serviceFactory;
    StreamingHttpServiceFilterFactory serviceFilterFactory = IDENTITY_FILTER;
    StreamingHttpClientFilterFactory clientFilterFactory = IDENTITY_CLIENT_FILTER;
    Publisher<TestRequest> requestPublisher = from(TestRequest.newBuilder().build());
    switch(testMode) {
        case HttpClientFilterThrows:
            clientFilterFactory = new ErrorProducingClientFilter(true, DELIBERATE_EXCEPTION);
            serviceFactory = setupForSuccess();
            break;
        case HttpClientFilterThrowsGrpcException:
            clientFilterFactory = new ErrorProducingClientFilter(true, cannedException);
            serviceFactory = setupForSuccess();
            break;
        case HttpClientFilterEmitsError:
            clientFilterFactory = new ErrorProducingClientFilter(false, DELIBERATE_EXCEPTION);
            serviceFactory = setupForSuccess();
            break;
        case HttpClientFilterEmitsGrpcException:
            clientFilterFactory = new ErrorProducingClientFilter(false, cannedException);
            serviceFactory = setupForSuccess();
            break;
        case HttpFilterThrows:
            serviceFilterFactory = new ErrorProducingSvcFilter(true, DELIBERATE_EXCEPTION);
            serviceFactory = setupForSuccess();
            break;
        case HttpFilterThrowsGrpcException:
            serviceFilterFactory = new ErrorProducingSvcFilter(true, cannedException);
            serviceFactory = setupForSuccess();
            break;
        case HttpFilterEmitsError:
            serviceFilterFactory = new ErrorProducingSvcFilter(false, DELIBERATE_EXCEPTION);
            serviceFactory = setupForSuccess();
            break;
        case HttpFilterEmitsGrpcException:
            serviceFilterFactory = new ErrorProducingSvcFilter(false, cannedException);
            serviceFactory = setupForSuccess();
            break;
        case ServiceThrows:
            serviceFactory = setupForServiceThrows(DELIBERATE_EXCEPTION);
            break;
        case ServiceThrowsGrpcException:
            serviceFactory = setupForServiceThrows(cannedException);
            break;
        case ServiceOperatorThrows:
            serviceFactory = setupForServiceOperatorThrows(DELIBERATE_EXCEPTION);
            break;
        case ServiceOperatorThrowsGrpcException:
            serviceFactory = setupForServiceOperatorThrows(cannedException);
            break;
        case ServiceSecondOperatorThrowsGrpcException:
            serviceFactory = setupForServiceSecondOperatorThrows(cannedException);
            requestPublisher = from(TestRequest.newBuilder().build(), TestRequest.newBuilder().setName(REQ_THROW_NAME).build());
            break;
        case ServiceEmitsError:
            serviceFactory = setupForServiceEmitsError(DELIBERATE_EXCEPTION);
            break;
        case ServiceEmitsGrpcException:
            serviceFactory = setupForServiceEmitsError(cannedException);
            break;
        case ServiceEmitsDataThenError:
            serviceFactory = setupForServiceEmitsDataThenError(DELIBERATE_EXCEPTION);
            break;
        case ServiceEmitsDataThenGrpcException:
            serviceFactory = setupForServiceEmitsDataThenError(cannedException);
            break;
        case BlockingServiceThrows:
            serviceFactory = setupForBlockingServiceThrows(DELIBERATE_EXCEPTION);
            break;
        case BlockingServiceThrowsGrpcException:
            serviceFactory = setupForBlockingServiceThrows(cannedException);
            break;
        case BlockingServiceWritesThenThrows:
            serviceFactory = setupForBlockingServiceWritesThenThrows(DELIBERATE_EXCEPTION);
            break;
        case BlockingServiceWritesThenThrowsGrpcException:
            serviceFactory = setupForBlockingServiceWritesThenThrows(cannedException);
            break;
        default:
            throw new IllegalArgumentException("Unknown mode: " + testMode);
    }
    this.requestPublisher = requestPublisher;
    final StreamingHttpServiceFilterFactory filterFactory = serviceFilterFactory;
    serverContext = GrpcServers.forAddress(localAddress(0)).initializeHttp(builder -> builder.appendServiceFilter(filterFactory).executionStrategy(serverStrategy)).listenAndAwait(serviceFactory);
    final StreamingHttpClientFilterFactory pickedClientFilterFactory = clientFilterFactory;
    GrpcClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = GrpcClients.forAddress(serverHostAndPort(serverContext)).initializeHttp(builder -> builder.appendClientFilter(pickedClientFilterFactory).executionStrategy(clientStrategy));
    client = clientBuilder.build(new ClientFactory());
    blockingClient = clientBuilder.buildBlocking(new ClientFactory());
}
Also used : StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) ServiceFactory(io.servicetalk.grpc.netty.TesterProto.Tester.ServiceFactory) InetSocketAddress(java.net.InetSocketAddress) ClientFactory(io.servicetalk.grpc.netty.TesterProto.Tester.ClientFactory) StreamingHttpServiceFilterFactory(io.servicetalk.http.api.StreamingHttpServiceFilterFactory) TestRequest(io.servicetalk.grpc.netty.TesterProto.TestRequest)

Example 2 with StreamingHttpClientFilterFactory

use of io.servicetalk.http.api.StreamingHttpClientFilterFactory in project servicetalk by apple.

the class RedirectingHttpRequesterFilterTest method newClient.

private StreamingHttpClient newClient(RedirectConfig config, StreamingHttpClientFilterFactory... other) {
    StreamingHttpClientFilterFactory result = new RedirectingHttpRequesterFilter(config);
    for (StreamingHttpClientFilterFactory next : other) {
        result = appendClientFilterFactory(result, next);
    }
    StreamingHttpClientFilterFactory mockResponse = client -> new StreamingHttpClientFilter(client) {

        @Override
        protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
            return httpClient.request(request);
        }
    };
    return from(reqRespFactory, mock(HttpExecutionContext.class), appendClientFilterFactory(result, mockResponse));
}
Also used : StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) MULTIPLE_CHOICES(io.servicetalk.http.api.HttpResponseStatus.MULTIPLE_CHOICES) HttpRequestMethod(io.servicetalk.http.api.HttpRequestMethod) LOCATION(io.servicetalk.http.api.HttpHeaderNames.LOCATION) Matchers.not(org.hamcrest.Matchers.not) CONTENT_TYPE(io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE) BAD_REQUEST(io.servicetalk.http.api.HttpResponseStatus.BAD_REQUEST) TEMPORARY_REDIRECT(io.servicetalk.http.api.HttpResponseStatus.TEMPORARY_REDIRECT) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HEAD(io.servicetalk.http.api.HttpRequestMethod.HEAD) RedirectConfig(io.servicetalk.http.api.RedirectConfig) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Executor(io.servicetalk.concurrent.api.Executor) FOUND(io.servicetalk.http.api.HttpResponseStatus.FOUND) SEE_OTHER(io.servicetalk.http.api.HttpResponseStatus.SEE_OTHER) StreamingHttpRequestFactory(io.servicetalk.http.api.StreamingHttpRequestFactory) PUT(io.servicetalk.http.api.HttpRequestMethod.PUT) ExecutorExtension(io.servicetalk.concurrent.api.ExecutorExtension) FilterFactoryUtils.appendClientFilterFactory(io.servicetalk.http.api.FilterFactoryUtils.appendClientFilterFactory) Mockito.clearInvocations(org.mockito.Mockito.clearInvocations) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) TRANSFER_ENCODING(io.servicetalk.http.api.HttpHeaderNames.TRANSFER_ENCODING) CONNECT(io.servicetalk.http.api.HttpRequestMethod.CONNECT) POST(io.servicetalk.http.api.HttpRequestMethod.POST) StatelessTrailersTransformer(io.servicetalk.http.api.StatelessTrailersTransformer) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) CONTENT_LENGTH(io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH) Matchers.startsWith(org.hamcrest.Matchers.startsWith) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Buffer(io.servicetalk.buffer.api.Buffer) CONTINUE(io.servicetalk.http.api.HttpResponseStatus.CONTINUE) PATCH(io.servicetalk.http.api.HttpRequestMethod.PATCH) NOT_MODIFIED(io.servicetalk.http.api.HttpResponseStatus.NOT_MODIFIED) Matchers.is(org.hamcrest.Matchers.is) Queue(java.util.Queue) Matchers.endsWith(org.hamcrest.Matchers.endsWith) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) TestStreamingHttpClient.from(io.servicetalk.http.api.TestStreamingHttpClient.from) HttpHeaders(io.servicetalk.http.api.HttpHeaders) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) ArgumentCaptor(org.mockito.ArgumentCaptor) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HOST(io.servicetalk.http.api.HttpHeaderNames.HOST) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Matchers.contentEqualTo(io.servicetalk.buffer.api.Matchers.contentEqualTo) OPTIONS(io.servicetalk.http.api.HttpRequestMethod.OPTIONS) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) DELETE(io.servicetalk.http.api.HttpRequestMethod.DELETE) USE_PROXY(io.servicetalk.http.api.HttpResponseStatus.USE_PROXY) Nullable(javax.annotation.Nullable) CHUNKED(io.servicetalk.http.api.HttpHeaderValues.CHUNKED) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) ValueSource(org.junit.jupiter.params.provider.ValueSource) Mockito.ignoreStubs(org.mockito.Mockito.ignoreStubs) INTERNAL_SERVER_ERROR(io.servicetalk.http.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) StreamingHttpRequestResponseFactory(io.servicetalk.http.api.StreamingHttpRequestResponseFactory) Single(io.servicetalk.concurrent.api.Single) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) OK(io.servicetalk.http.api.HttpResponseStatus.OK) GET(io.servicetalk.http.api.HttpRequestMethod.GET) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) ACCEPT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.ACCEPT_ENCODING) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Matchers.emptyIterable(org.hamcrest.Matchers.emptyIterable) String.valueOf(java.lang.String.valueOf) ArgumentCaptor.forClass(org.mockito.ArgumentCaptor.forClass) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) PERMANENT_REDIRECT(io.servicetalk.http.api.HttpResponseStatus.PERMANENT_REDIRECT) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Single.failed(io.servicetalk.concurrent.api.Single.failed) StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) HttpResponseStatus(io.servicetalk.http.api.HttpResponseStatus) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) MOVED_PERMANENTLY(io.servicetalk.http.api.HttpResponseStatus.MOVED_PERMANENTLY) DefaultStreamingHttpRequestResponseFactory(io.servicetalk.http.api.DefaultStreamingHttpRequestResponseFactory) HTTP_1_0(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_0) TRACE(io.servicetalk.http.api.HttpRequestMethod.TRACE) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 3 with StreamingHttpClientFilterFactory

use of io.servicetalk.http.api.StreamingHttpClientFilterFactory in project servicetalk by apple.

the class ConditionalFilterFactory method append.

public FilterFactory append(FilterFactory append) {
    StreamingHttpClientFilterFactory clientFactory = appendClientFilterFactory(this, append);
    StreamingHttpConnectionFilterFactory connectionFactory = appendConnectionFilterFactory(this, append);
    return new FilterFactory() {

        @Override
        public StreamingHttpClientFilter create(final FilterableStreamingHttpClient client) {
            return clientFactory.create(client);
        }

        @Override
        public StreamingHttpConnectionFilter create(final FilterableStreamingHttpConnection connection) {
            return connectionFactory.create(connection);
        }
    };
}
Also used : StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) StreamingHttpConnectionFilterFactory(io.servicetalk.http.api.StreamingHttpConnectionFilterFactory) StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) StreamingHttpConnectionFilterFactory(io.servicetalk.http.api.StreamingHttpConnectionFilterFactory)

Example 4 with StreamingHttpClientFilterFactory

use of io.servicetalk.http.api.StreamingHttpClientFilterFactory in project servicetalk by apple.

the class ClientEffectiveStrategyTest method clientStrategy.

@ParameterizedTest(name = "API={0} builder={1} filter={2} LB={3} CF={4}")
@MethodSource("casesSupplier")
void clientStrategy(ClientType clientType, @Nullable final HttpExecutionStrategy builderStrategy, @Nullable final HttpExecutionStrategy filterStrategy, @Nullable final HttpExecutionStrategy lbStrategy, @Nullable final HttpExecutionStrategy cfStrategy) throws Exception {
    HttpExecutionStrategy effectiveStrategy = computeClientExecutionStrategy(builderStrategy, filterStrategy, lbStrategy, cfStrategy);
    SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = HttpClients.forSingleAddress(serverHostAndPort(context));
    if (builderStrategy != null) {
        clientBuilder.executionStrategy(builderStrategy);
    }
    ClientInvokingThreadRecorder invokingThreadsRecorder = new ClientInvokingThreadRecorder(clientType, effectiveStrategy);
    clientBuilder.appendClientFilter(invokingThreadsRecorder);
    if (null != filterStrategy) {
        clientBuilder.appendClientFilter(new StreamingHttpClientFilterFactory() {

            @Override
            public StreamingHttpClientFilter create(final FilterableStreamingHttpClient client) {
                return new StreamingHttpClientFilter(client) {
                };
            }

            @Override
            public HttpExecutionStrategy requiredOffloads() {
                return filterStrategy;
            }
        });
    }
    if (null != lbStrategy) {
        HttpLoadBalancerFactory<InetSocketAddress> lfFactory = DefaultHttpLoadBalancerFactory.Builder.from(new LoadBalancerFactoryImpl() {

            @Override
            public ExecutionStrategy requiredOffloads() {
                return lbStrategy;
            }
        }).build();
        clientBuilder.loadBalancerFactory(lfFactory);
    }
    if (null != cfStrategy) {
        clientBuilder.appendConnectionFilter(new StreamingHttpConnectionFilterFactory() {

            @Override
            public StreamingHttpConnectionFilter create(final FilterableStreamingHttpConnection connection) {
                return new StreamingHttpConnectionFilter(connection) {
                };
            }

            @Override
            public HttpExecutionStrategy requiredOffloads() {
                return cfStrategy;
            }
        });
    }
    // Exercise the client
    try (StreamingHttpClient client = clientBuilder.buildStreaming()) {
        String responseBody = getResponse(clientType, client);
        assertThat(responseBody, is(GREETING));
        invokingThreadsRecorder.verifyOffloads();
    }
}
Also used : FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) InetSocketAddress(java.net.InetSocketAddress) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) StreamingHttpConnectionFilterFactory(io.servicetalk.http.api.StreamingHttpConnectionFilterFactory) StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) StreamingHttpConnectionFilter(io.servicetalk.http.api.StreamingHttpConnectionFilter) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

StreamingHttpClientFilterFactory (io.servicetalk.http.api.StreamingHttpClientFilterFactory)4 FilterableStreamingHttpClient (io.servicetalk.http.api.FilterableStreamingHttpClient)2 FilterableStreamingHttpConnection (io.servicetalk.http.api.FilterableStreamingHttpConnection)2 Buffer (io.servicetalk.buffer.api.Buffer)1 Matchers.contentEqualTo (io.servicetalk.buffer.api.Matchers.contentEqualTo)1 DEFAULT_ALLOCATOR (io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR)1 Executor (io.servicetalk.concurrent.api.Executor)1 ExecutorExtension (io.servicetalk.concurrent.api.ExecutorExtension)1 Publisher.from (io.servicetalk.concurrent.api.Publisher.from)1 Single (io.servicetalk.concurrent.api.Single)1 Single.failed (io.servicetalk.concurrent.api.Single.failed)1 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 TestPublisher (io.servicetalk.concurrent.api.TestPublisher)1 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)1 TestRequest (io.servicetalk.grpc.netty.TesterProto.TestRequest)1 ClientFactory (io.servicetalk.grpc.netty.TesterProto.Tester.ClientFactory)1 ServiceFactory (io.servicetalk.grpc.netty.TesterProto.Tester.ServiceFactory)1 BlockingStreamingHttpClient (io.servicetalk.http.api.BlockingStreamingHttpClient)1 DefaultHttpHeadersFactory (io.servicetalk.http.api.DefaultHttpHeadersFactory)1 DefaultStreamingHttpRequestResponseFactory (io.servicetalk.http.api.DefaultStreamingHttpRequestResponseFactory)1