Search in sources :

Example 36 with Response

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

the class RetryingChannelTest method retries_429s.

@Test
public void retries_429s() throws Exception {
    Response mockResponse = mock(Response.class);
    when(mockResponse.code()).thenReturn(429);
    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 429 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)

Example 37 with Response

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

the class RetryingChannelTest method testPropagatesCancel.

@Test
public void testPropagatesCancel() {
    ListenableFuture<Response> delegateResult = SettableFuture.create();
    when(channel.execute(any())).thenReturn(delegateResult);
    EndpointChannel retryer = new RetryingChannel(channel, TestEndpoint.POST, "my-channel", 3, Duration.ZERO, ClientConfiguration.ServerQoS.AUTOMATIC_RETRY, ClientConfiguration.RetryOnTimeout.DISABLED);
    ListenableFuture<Response> retryingResult = retryer.execute(REQUEST);
    assertThat(retryingResult.cancel(true)).isTrue();
    assertThat(delegateResult).as("Failed to cancel the delegate future").isCancelled();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) EndpointChannel(com.palantir.dialogue.EndpointChannel) Test(org.junit.jupiter.api.Test)

Example 38 with Response

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

the class StickyAttachmentsTest method validate_is_passthrough_if_attachments_present.

@Test
public void validate_is_passthrough_if_attachments_present() throws ExecutionException {
    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(stickyTokenHandler, TestEndpoint.GET, request, LimitEnforcement.DEFAULT_ENABLED).get();
    TestResponse response = TestResponse.withBody(null);
    responseSettableFuture.set(response);
    assertThat(Futures.getDone(responseListenableFuture)).isEqualTo(response);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) TestResponse(com.palantir.dialogue.TestResponse) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test)

Example 39 with Response

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

the class HostMetricsChannelTest method calls_sink_when_response_comes_back.

@Test
void calls_sink_when_response_comes_back() {
    AtomicBoolean recorded = new AtomicBoolean();
    Channel channel = HostMetricsChannel.create(config(ClientConfiguration.builder().from(TestConfigurations.create("https://unused", "https://unused2")).hostEventsSink(new HostEventsSink() {

        @Override
        public void record(String serviceName, String hostname, int port, int statusCode, long micros) {
            assertThat(serviceName).isEqualTo("channelName");
            assertThat(hostname).isEqualTo("foo");
            assertThat(port).isEqualTo(1001);
            assertThat(statusCode).isEqualTo(200);
            assertThat(micros).isEqualTo(TimeUnit.SECONDS.toMicros(3));
            recorded.set(true);
        }

        @Override
        public void recordIoException(String _serviceName, String _hostname, int _port) {
            Assertions.fail("no IOExceptions expected");
        }
    }).build()), mockChannel, "https://foo:1001");
    SettableFuture<Response> settable = SettableFuture.create();
    when(mockChannel.execute(any(), any())).thenReturn(settable);
    ListenableFuture<Response> future = channel.execute(TestEndpoint.GET, Request.builder().build());
    when(ticker.read()).thenReturn(Duration.ofSeconds(3).toNanos());
    settable.set(new TestResponse().code(200));
    assertThat(recorded).isTrue();
    assertThat(future).isDone();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HostEventsSink(com.palantir.conjure.java.client.config.HostEventsSink) TestResponse(com.palantir.dialogue.TestResponse) Channel(com.palantir.dialogue.Channel) Test(org.junit.jupiter.api.Test)

Example 40 with Response

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

the class PinUntilErrorNodeSelectionStrategyChannelTest method setResponse.

private static void setResponse(LimitedChannel mockChannel, int status) {
    Mockito.clearInvocations(mockChannel);
    Mockito.reset(mockChannel);
    Response resp = response(status);
    lenient().when(mockChannel.maybeExecute(any(), any(), eq(LimitEnforcement.DEFAULT_ENABLED))).thenReturn(Optional.of(Futures.immediateFuture(resp)));
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response)

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