Search in sources :

Example 26 with EndpointChannel

use of com.palantir.dialogue.EndpointChannel in project dialogue by palantir.

the class RetryingChannelTest method final_exhausted_failure_response_body_is_not_closed.

@Test
public void final_exhausted_failure_response_body_is_not_closed() throws Exception {
    TestResponse response1 = new TestResponse().code(503);
    TestResponse response2 = new TestResponse().code(503);
    TestResponse response3 = new TestResponse().code(503);
    when(channel.execute(any())).thenReturn(Futures.immediateFuture(response1)).thenReturn(Futures.immediateFuture(response2)).thenReturn(Futures.immediateFuture(response3));
    EndpointChannel retryer = new RetryingChannel(channel, TestEndpoint.POST, "my-channel", 2, Duration.ZERO, ClientConfiguration.ServerQoS.AUTOMATIC_RETRY, ClientConfiguration.RetryOnTimeout.DISABLED);
    ListenableFuture<Response> response = retryer.execute(REQUEST);
    assertThat(response.get(1, TimeUnit.SECONDS).code()).isEqualTo(503);
    assertThat(response1.isClosed()).isTrue();
    assertThat(response2.isClosed()).isTrue();
    assertThat(response3.isClosed()).describedAs("The last response must be left open so we can read the body" + " and deserialize it into a structured error").isFalse();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) TestResponse(com.palantir.dialogue.TestResponse) EndpointChannel(com.palantir.dialogue.EndpointChannel) Test(org.junit.jupiter.api.Test)

Example 27 with EndpointChannel

use of com.palantir.dialogue.EndpointChannel in project dialogue by palantir.

the class RetryingChannelTest method testRetriesUpToMaxRetries.

@Test
public void testRetriesUpToMaxRetries() throws ExecutionException, InterruptedException {
    when(channel.execute(any())).thenReturn(FAILED).thenReturn(SUCCESS);
    // One retry allows an initial request (not a retry) and a single retry.
    EndpointChannel retryer = new RetryingChannel(channel, TestEndpoint.POST, "my-channel", 1, Duration.ZERO, ClientConfiguration.ServerQoS.AUTOMATIC_RETRY, ClientConfiguration.RetryOnTimeout.DISABLED);
    ListenableFuture<Response> response = retryer.execute(REQUEST);
    assertThat(response).isDone();
    assertThat(response.get()).isEqualTo(EXPECTED_RESPONSE);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) EndpointChannel(com.palantir.dialogue.EndpointChannel) Test(org.junit.jupiter.api.Test)

Example 28 with EndpointChannel

use of com.palantir.dialogue.EndpointChannel in project dialogue by palantir.

the class RetryingChannelTest method retriesSocketTimeout_connectionTimeout.

@Test
public void retriesSocketTimeout_connectionTimeout() throws ExecutionException, InterruptedException {
    when(channel.execute(any())).thenReturn(Futures.immediateFailedFuture(new SocketTimeoutException("connect timed out"))).thenReturn(SUCCESS);
    EndpointChannel retryer = new RetryingChannel(channel, TestEndpoint.POST, "my-channel", 1, Duration.ZERO, ClientConfiguration.ServerQoS.AUTOMATIC_RETRY, ClientConfiguration.RetryOnTimeout.DISABLED);
    ListenableFuture<Response> response = retryer.execute(REQUEST);
    assertThat(response.get()).isEqualTo(EXPECTED_RESPONSE);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) SocketTimeoutException(java.net.SocketTimeoutException) EndpointChannel(com.palantir.dialogue.EndpointChannel) Test(org.junit.jupiter.api.Test)

Example 29 with EndpointChannel

use of com.palantir.dialogue.EndpointChannel in project dialogue by palantir.

the class RetryingChannelTest method testRetriesMax.

@Test
public void testRetriesMax() {
    when(channel.execute(any())).thenReturn(FAILED);
    EndpointChannel retryer = new RetryingChannel(channel, TestEndpoint.POST, "my-channel", 3, Duration.ZERO, ClientConfiguration.ServerQoS.AUTOMATIC_RETRY, ClientConfiguration.RetryOnTimeout.DISABLED);
    ListenableFuture<Response> response = retryer.execute(REQUEST);
    assertThatThrownBy(response::get).hasCauseInstanceOf(SafeIoException.class);
    verify(channel, times(4)).execute(REQUEST);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) EndpointChannel(com.palantir.dialogue.EndpointChannel) Test(org.junit.jupiter.api.Test)

Example 30 with EndpointChannel

use of com.palantir.dialogue.EndpointChannel in project dialogue by palantir.

the class RetryingChannelTest method response_bodies_are_closed.

@Test
public void response_bodies_are_closed() throws Exception {
    Response response1 = mockResponse(503);
    Response response2 = mockResponse(503);
    Response eventualSuccess = mockResponse(200);
    when(channel.execute(any())).thenReturn(Futures.immediateFuture(response1)).thenReturn(Futures.immediateFuture(response2)).thenReturn(Futures.immediateFuture(eventualSuccess));
    EndpointChannel retryer = new RetryingChannel(channel, TestEndpoint.POST, "my-channel", 3, Duration.ZERO, ClientConfiguration.ServerQoS.AUTOMATIC_RETRY, ClientConfiguration.RetryOnTimeout.DISABLED);
    ListenableFuture<Response> response = retryer.execute(REQUEST);
    assertThat(response.get(1, TimeUnit.SECONDS).code()).isEqualTo(200);
    verify(response1, times(1)).close();
    verify(response2, times(1)).close();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) EndpointChannel(com.palantir.dialogue.EndpointChannel) Test(org.junit.jupiter.api.Test)

Aggregations

EndpointChannel (com.palantir.dialogue.EndpointChannel)32 Test (org.junit.jupiter.api.Test)28 Response (com.palantir.dialogue.Response)25 TestResponse (com.palantir.dialogue.TestResponse)25 Endpoint (com.palantir.dialogue.Endpoint)6 HttpMethod (com.palantir.dialogue.HttpMethod)4 Request (com.palantir.dialogue.Request)4 Channel (com.palantir.dialogue.Channel)3 RequestBody (com.palantir.dialogue.RequestBody)3 TestEndpoint (com.palantir.dialogue.TestEndpoint)3 IOException (java.io.IOException)3 OutputStream (java.io.OutputStream)3 List (java.util.List)3 Optional (java.util.Optional)3 Set (java.util.Set)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 ByteStreams (com.google.common.io.ByteStreams)2 Futures (com.google.common.util.concurrent.Futures)2 SafeLong (com.palantir.conjure.java.lib.SafeLong)2 ClientEndpoint (com.palantir.conjure.java.lib.internal.ClientEndpoint)2