Search in sources :

Example 66 with Response

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

the class DialogueChannelTest method test_serialization_socket_timeout_is_not_retried.

@Test
void test_serialization_socket_timeout_is_not_retried() {
    ListenableFuture<Response> result = makeStructuredRequestToClosedConnection(() -> new SocketTimeoutException("oops"));
    assertThat(result).as("Socket timeouts should not be retried").failsWithin(Duration.ofSeconds(2));
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) SocketTimeoutException(java.net.SocketTimeoutException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 67 with Response

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

the class DialogueChannelTest method bad_channel_throwing_an_error_still_returns_a_future.

@Test
public void bad_channel_throwing_an_error_still_returns_a_future() {
    Channel badUserImplementation = new Channel() {

        @Override
        public ListenableFuture<Response> execute(Endpoint _endpoint, Request _request) {
            throw new NoClassDefFoundError("something is broken");
        }
    };
    channel = DialogueChannel.builder().channelName("my-channel").clientConfiguration(stubConfig).factory(_args -> badUserImplementation).build();
    // this should never throw
    ListenableFuture<Response> future = channel.execute(endpoint, request);
    // only when we access things do we allow exceptions
    assertThatThrownBy(() -> Futures.getUnchecked(future)).hasCauseInstanceOf(NoClassDefFoundError.class);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) TestEndpoint(com.palantir.dialogue.TestEndpoint) Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 68 with Response

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

the class RetryingChannelTest method nonRetryableRequestBodyIsNotRetried.

@Test
public void nonRetryableRequestBodyIsNotRetried() 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.builder().body(new RequestBody() {

        @Override
        public void writeTo(OutputStream _output) {
        }

        @Override
        public String contentType() {
            return "application/octet-stream";
        }

        @Override
        public boolean repeatable() {
            return false;
        }

        @Override
        public void close() {
        }
    }).build());
    assertThat(response).isDone();
    assertThat(response).as("non-repeatable request bodies should not be retried").isEqualTo(FAILED);
    verify(channel, times(1)).execute(any());
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) OutputStream(java.io.OutputStream) EndpointChannel(com.palantir.dialogue.EndpointChannel) RequestBody(com.palantir.dialogue.RequestBody) Test(org.junit.jupiter.api.Test)

Example 69 with Response

use of com.palantir.dialogue.Response 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 70 with Response

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

the class RetryingChannelTest method retries_503s.

@Test
public void retries_503s() throws Exception {
    Response mockResponse = mock(Response.class);
    when(mockResponse.code()).thenReturn(503);
    when(channel.execute(any())).thenReturn(Futures.immediateFuture(mockResponse));
    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()).as("After retries are exhausted the 503 response should be returned").isSameAs(mockResponse);
    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)

Aggregations

Response (com.palantir.dialogue.Response)93 Test (org.junit.jupiter.api.Test)72 TestResponse (com.palantir.dialogue.TestResponse)56 EndpointChannel (com.palantir.dialogue.EndpointChannel)27 Channel (com.palantir.dialogue.Channel)24 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)22 Endpoint (com.palantir.dialogue.Endpoint)16 Request (com.palantir.dialogue.Request)15 ClientConfiguration (com.palantir.conjure.java.client.config.ClientConfiguration)11 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)10 TestEndpoint (com.palantir.dialogue.TestEndpoint)10 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)9 Duration (java.time.Duration)9 Meter (com.codahale.metrics.Meter)8 IOException (java.io.IOException)8 Optional (java.util.Optional)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 QosException (com.palantir.conjure.java.api.errors.QosException)5 Arrays (java.util.Arrays)5 Stream (java.util.stream.Stream)5