Search in sources :

Example 16 with TestResponse

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

the class RetryingChannelTest method retries_500s_when_method_is_safe_and_idempotent.

@Test
public void retries_500s_when_method_is_safe_and_idempotent() throws Exception {
    when(channel.execute(any())).thenReturn(Futures.immediateFuture(new TestResponse().code(500))).thenReturn(Futures.immediateFuture(new TestResponse().code(200)));
    EndpointChannel retryer = new RetryingChannel(channel, TestEndpoint.GET, "my-channel", 3, Duration.ZERO, ClientConfiguration.ServerQoS.AUTOMATIC_RETRY, ClientConfiguration.RetryOnTimeout.DISABLED);
    ListenableFuture<Response> response = retryer.execute(REQUEST);
    assertThat(response).isDone();
    assertThat(response.get().code()).isEqualTo(200);
    verify(channel, times(2)).execute(REQUEST);
}
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 17 with TestResponse

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

the class RetryingChannelTest method doesnt_retry_500s_for_post.

@Test
public void doesnt_retry_500s_for_post() throws Exception {
    when(channel.execute(any())).thenReturn(Futures.immediateFuture(new TestResponse().code(500)));
    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).isDone();
    assertThat(response.get().code()).isEqualTo(500);
    verify(channel, times(1)).execute(REQUEST);
}
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 18 with TestResponse

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

the class RetryingChannelTest method retries_500s_for_delete.

@Test
public void retries_500s_for_delete() throws Exception {
    when(channel.execute(any())).thenReturn(Futures.immediateFuture(new TestResponse().code(500))).thenReturn(Futures.immediateFuture(new TestResponse().code(200)));
    EndpointChannel retryer = new RetryingChannel(channel, TestEndpoint.DELETE, "my-channel", 3, Duration.ZERO, ClientConfiguration.ServerQoS.AUTOMATIC_RETRY, ClientConfiguration.RetryOnTimeout.DISABLED);
    ListenableFuture<Response> response = retryer.execute(REQUEST);
    assertThat(response).isDone();
    assertThat(response.get().code()).isEqualTo(200);
    verify(channel, times(2)).execute(REQUEST);
}
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 19 with TestResponse

use of com.palantir.dialogue.TestResponse 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 20 with TestResponse

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

the class StickyAttachmentsTest method validate_throws_and_closes_response_if_attachments_are_not_present.

@Test
public void validate_throws_and_closes_response_if_attachments_are_not_present() {
    Request request = Request.builder().build();
    StickyAttachments.requestStickyToken(request);
    when(delegate.maybeExecute(TestEndpoint.GET, request, LimitEnforcement.DEFAULT_ENABLED)).thenReturn(Optional.of(responseSettableFuture));
    ListenableFuture<Response> responseListenableFuture = StickyAttachments.maybeExecuteAndValidateRequestStickyToken(delegate, TestEndpoint.GET, request, LimitEnforcement.DEFAULT_ENABLED).get();
    TestResponse response = TestResponse.withBody(null);
    responseSettableFuture.set(response);
    assertThatThrownBy(() -> Futures.getDone(responseListenableFuture)).isInstanceOf(ExecutionException.class).hasRootCauseExactlyInstanceOf(SafeRuntimeException.class).hasRootCauseMessage("Requested sticky token on request but token not present on response");
    assertThat(response.isClosed()).isTrue();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) TestResponse(com.palantir.dialogue.TestResponse) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test)

Aggregations

TestResponse (com.palantir.dialogue.TestResponse)31 Test (org.junit.jupiter.api.Test)26 BodySerDe (com.palantir.dialogue.BodySerDe)13 Response (com.palantir.dialogue.Response)13 EndpointChannel (com.palantir.dialogue.EndpointChannel)7 CloseRecordingInputStream (com.palantir.dialogue.CloseRecordingInputStream)4 InputStream (java.io.InputStream)4 Request (com.palantir.dialogue.Request)3 TypeMarker (com.palantir.dialogue.TypeMarker)3 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)3 IOException (java.io.IOException)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 EnumSource (org.junit.jupiter.params.provider.EnumSource)3 RemoteException (com.palantir.conjure.java.api.errors.RemoteException)2 SerializableError (com.palantir.conjure.java.api.errors.SerializableError)2 ServiceException (com.palantir.conjure.java.api.errors.ServiceException)2 Channel (com.palantir.dialogue.Channel)2 Endpoint (com.palantir.dialogue.Endpoint)2 Random (java.util.Random)2 Meter (com.codahale.metrics.Meter)1