Search in sources :

Example 1 with Timeout

use of org.apache.hc.core5.util.Timeout in project feign by OpenFeign.

the class AsyncApacheHttp5ClientTest method throwsFeignExceptionIncludingBody.

@Test
public void throwsFeignExceptionIncludingBody() throws Throwable {
    server.enqueue(new MockResponse().setBody("success!"));
    final TestInterfaceAsync api = AsyncFeign.asyncBuilder().decoder((response, type) -> {
        throw new IOException("timeout");
    }).target(TestInterfaceAsync.class, "http://localhost:" + server.getPort());
    final CompletableFuture<?> cf = api.body("Request body");
    server.takeRequest();
    try {
        unwrap(cf);
    } catch (final FeignException e) {
        assertThat(e.getMessage()).isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
        assertThat(e.contentUTF8()).isEqualTo("Request body");
        return;
    }
    fail();
}
Also used : java.util(java.util) TypeToken(com.google.gson.reflect.TypeToken) CoreMatchers.isA(org.hamcrest.CoreMatchers.isA) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseMappingDecoder(feign.Feign.ResponseMappingDecoder) feign.codec(feign.codec) FieldQueryMapEncoder(feign.querymap.FieldQueryMapEncoder) HttpMethod(feign.Request.HttpMethod) Gson(com.google.gson.Gson) MockWebServer(okhttp3.mockwebserver.MockWebServer) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) ExpectedException(org.junit.rules.ExpectedException) BeanQueryMapEncoder(feign.querymap.BeanQueryMapEncoder) Buffer(okio.Buffer) java.util.concurrent(java.util.concurrent) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) HardCodedTarget(feign.Target.HardCodedTarget) Rule(org.junit.Rule) Type(java.lang.reflect.Type) MockWebServerAssertions.assertThat(feign.assertj.MockWebServerAssertions.assertThat) MockResponse(okhttp3.mockwebserver.MockResponse) MapEntry.entry(org.assertj.core.data.MapEntry.entry) feign(feign) Assert.assertEquals(org.junit.Assert.assertEquals) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with Timeout

use of org.apache.hc.core5.util.Timeout in project rest.li by linkedin.

the class HttpNettyClient method sendRequest.

/**
 * Sends the request to the {@link ChannelPipeline}.
 */
private void sendRequest(Request request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<StreamResponse> callback) {
    final TransportCallback<StreamResponse> decoratedCallback = decorateUserCallback(request, callback);
    final NettyClientState state = _state.get();
    if (state != NettyClientState.RUNNING) {
        decoratedCallback.onResponse(TransportResponseImpl.error(new IllegalStateException("Client is not running")));
        return;
    }
    final long resolvedRequestTimeout = resolveRequestTimeout(requestContext, _requestTimeout);
    // Timeout ensures the request callback is always invoked and is cancelled before the
    // responsibility of invoking the callback is handed over to the pipeline.
    final Timeout<None> timeout = new Timeout<>(_scheduler, resolvedRequestTimeout, TimeUnit.MILLISECONDS, None.none());
    timeout.addTimeoutTask(() -> decoratedCallback.onResponse(TransportResponseImpl.error(new TimeoutException("Exceeded request timeout of " + resolvedRequestTimeout + "ms"))));
    // resolve address
    final SocketAddress address;
    try {
        TimingContextUtil.markTiming(requestContext, TIMING_KEY);
        address = resolveAddress(request, requestContext);
        TimingContextUtil.markTiming(requestContext, TIMING_KEY);
    } catch (Exception e) {
        decoratedCallback.onResponse(TransportResponseImpl.error(e));
        return;
    }
    // Serialize wire attributes
    final Request requestWithWireAttrHeaders;
    if (request instanceof StreamRequest) {
        requestWithWireAttrHeaders = buildRequestWithWireAttributes((StreamRequest) request, wireAttrs);
    } else {
        MessageType.setMessageType(MessageType.Type.REST, wireAttrs);
        requestWithWireAttrHeaders = buildRequestWithWireAttributes((RestRequest) request, wireAttrs);
    }
    // Gets channel pool
    final AsyncPool<Channel> pool;
    try {
        pool = getChannelPoolManagerPerRequest(requestWithWireAttrHeaders).getPoolForAddress(address);
    } catch (IllegalStateException e) {
        decoratedCallback.onResponse(TransportResponseImpl.error(e));
        return;
    }
    // Saves protocol version in request context
    requestContext.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, _protocolVersion);
    final Cancellable pendingGet = pool.get(new ChannelPoolGetCallback(pool, requestWithWireAttrHeaders, requestContext, decoratedCallback, timeout, resolvedRequestTimeout, _streamingTimeout));
    if (pendingGet != null) {
        timeout.addTimeoutTask(pendingGet::cancel);
    }
}
Also used : Timeout(com.linkedin.r2.util.Timeout) StreamingTimeout(com.linkedin.r2.netty.common.StreamingTimeout) Cancellable(com.linkedin.r2.util.Cancellable) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Channel(io.netty.channel.Channel) Request(com.linkedin.r2.message.Request) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) TimeoutException(java.util.concurrent.TimeoutException) ShutdownTimeoutException(com.linkedin.r2.netty.common.ShutdownTimeoutException) UnknownHostException(java.net.UnknownHostException) UnknownSchemeException(com.linkedin.r2.netty.common.UnknownSchemeException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) NettyClientState(com.linkedin.r2.netty.common.NettyClientState) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) None(com.linkedin.common.util.None) TimeoutException(java.util.concurrent.TimeoutException) ShutdownTimeoutException(com.linkedin.r2.netty.common.ShutdownTimeoutException)

Example 3 with Timeout

use of org.apache.hc.core5.util.Timeout in project feign by OpenFeign.

the class AsyncApacheHttp5ClientTest method throwsFeignExceptionWithoutBody.

@Test
public void throwsFeignExceptionWithoutBody() {
    server.enqueue(new MockResponse().setBody("success!"));
    final TestInterfaceAsync api = AsyncFeign.asyncBuilder().decoder((response, type) -> {
        throw new IOException("timeout");
    }).target(TestInterfaceAsync.class, "http://localhost:" + server.getPort());
    try {
        api.noContent();
    } catch (final FeignException e) {
        assertThat(e.getMessage()).isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
        assertThat(e.contentUTF8()).isEqualTo("");
    }
}
Also used : java.util(java.util) TypeToken(com.google.gson.reflect.TypeToken) CoreMatchers.isA(org.hamcrest.CoreMatchers.isA) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseMappingDecoder(feign.Feign.ResponseMappingDecoder) feign.codec(feign.codec) FieldQueryMapEncoder(feign.querymap.FieldQueryMapEncoder) HttpMethod(feign.Request.HttpMethod) Gson(com.google.gson.Gson) MockWebServer(okhttp3.mockwebserver.MockWebServer) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) ExpectedException(org.junit.rules.ExpectedException) BeanQueryMapEncoder(feign.querymap.BeanQueryMapEncoder) Buffer(okio.Buffer) java.util.concurrent(java.util.concurrent) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) HardCodedTarget(feign.Target.HardCodedTarget) Rule(org.junit.Rule) Type(java.lang.reflect.Type) MockWebServerAssertions.assertThat(feign.assertj.MockWebServerAssertions.assertThat) MockResponse(okhttp3.mockwebserver.MockResponse) MapEntry.entry(org.assertj.core.data.MapEntry.entry) feign(feign) Assert.assertEquals(org.junit.Assert.assertEquals) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with Timeout

use of org.apache.hc.core5.util.Timeout in project feign by OpenFeign.

the class AsyncApacheHttp5ClientTest method doesntRetryAfterResponseIsSent.

@Test
public void doesntRetryAfterResponseIsSent() throws Throwable {
    server.enqueue(new MockResponse().setBody("success!"));
    thrown.expect(FeignException.class);
    thrown.expectMessage("timeout reading POST http://");
    final TestInterfaceAsync api = new TestInterfaceAsyncBuilder().decoder((response, type) -> {
        throw new IOException("timeout");
    }).target("http://localhost:" + server.getPort());
    final CompletableFuture<?> cf = api.post();
    server.takeRequest();
    unwrap(cf);
}
Also used : java.util(java.util) TypeToken(com.google.gson.reflect.TypeToken) CoreMatchers.isA(org.hamcrest.CoreMatchers.isA) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseMappingDecoder(feign.Feign.ResponseMappingDecoder) feign.codec(feign.codec) FieldQueryMapEncoder(feign.querymap.FieldQueryMapEncoder) HttpMethod(feign.Request.HttpMethod) Gson(com.google.gson.Gson) MockWebServer(okhttp3.mockwebserver.MockWebServer) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) ExpectedException(org.junit.rules.ExpectedException) BeanQueryMapEncoder(feign.querymap.BeanQueryMapEncoder) Buffer(okio.Buffer) java.util.concurrent(java.util.concurrent) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) HardCodedTarget(feign.Target.HardCodedTarget) Rule(org.junit.Rule) Type(java.lang.reflect.Type) MockWebServerAssertions.assertThat(feign.assertj.MockWebServerAssertions.assertThat) MockResponse(okhttp3.mockwebserver.MockResponse) MapEntry.entry(org.assertj.core.data.MapEntry.entry) feign(feign) Assert.assertEquals(org.junit.Assert.assertEquals) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Gson (com.google.gson.Gson)3 TypeToken (com.google.gson.reflect.TypeToken)3 feign (feign)3 ResponseMappingDecoder (feign.Feign.ResponseMappingDecoder)3 HttpMethod (feign.Request.HttpMethod)3 HardCodedTarget (feign.Target.HardCodedTarget)3 MockWebServerAssertions.assertThat (feign.assertj.MockWebServerAssertions.assertThat)3 feign.codec (feign.codec)3 BeanQueryMapEncoder (feign.querymap.BeanQueryMapEncoder)3 FieldQueryMapEncoder (feign.querymap.FieldQueryMapEncoder)3 IOException (java.io.IOException)3 Type (java.lang.reflect.Type)3 URI (java.net.URI)3 java.util (java.util)3 java.util.concurrent (java.util.concurrent)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)3 Buffer (okio.Buffer)3 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)3