Search in sources :

Example 41 with Response

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

the class QueuedChannelTest method testQueuedResponseClosedOnCancel.

@Test
public void testQueuedResponseClosedOnCancel() {
    Request queuedRequest = Request.builder().pathParams(ImmutableMap.of("foo", "bar")).build();
    when(delegate.maybeExecute(endpoint, queuedRequest, DO_NOT_SKIP_LIMITS)).thenReturn(Optional.empty());
    ListenableFuture<Response> result = queuedChannel.maybeExecute(endpoint, queuedRequest).get();
    verify(delegate, times(2)).maybeExecute(endpoint, queuedRequest, DO_NOT_SKIP_LIMITS);
    when(delegate.maybeExecute(endpoint, request, DO_NOT_SKIP_LIMITS)).thenReturn(Optional.of(Futures.immediateFuture(Mockito.mock(Response.class))));
    when(delegate.maybeExecute(endpoint, queuedRequest, DO_NOT_SKIP_LIMITS)).thenAnswer((Answer<Optional<ListenableFuture<Response>>>) _invocation -> {
        assertThat(result.cancel(true)).isTrue();
        return Optional.of(Futures.immediateFuture(mockResponse));
    });
    // Force scheduling
    queuedChannel.maybeExecute(endpoint, request);
    assertThat(result).isCancelled();
    verify(delegate, times(1)).maybeExecute(endpoint, request, DO_NOT_SKIP_LIMITS);
    verify(mockResponse, times(1)).close();
}
Also used : Response(com.palantir.dialogue.Response) BeforeEach(org.junit.jupiter.api.BeforeEach) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SettableFuture(com.google.common.util.concurrent.SettableFuture) Answer(org.mockito.stubbing.Answer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Endpoint(com.palantir.dialogue.Endpoint) Request(com.palantir.dialogue.Request) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ImmutableMap(com.google.common.collect.ImmutableMap) LimitEnforcement(com.palantir.dialogue.core.LimitedChannel.LimitEnforcement) OngoingStubbing(org.mockito.stubbing.OngoingStubbing) Mockito.times(org.mockito.Mockito.times) TestTracing(com.palantir.tracing.TestTracing) Mockito.when(org.mockito.Mockito.when) DefaultTaggedMetricRegistry(com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Futures(com.google.common.util.concurrent.Futures) Timer(com.codahale.metrics.Timer) Optional(java.util.Optional) Response(com.palantir.dialogue.Response) Optional(java.util.Optional) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test)

Example 42 with Response

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

the class QueuedChannelTest method testQueuedResponsePropagatesCancel.

@Test
public void testQueuedResponsePropagatesCancel() {
    Request queued = Request.builder().putHeaderParams("key", "val").build();
    when(delegate.maybeExecute(endpoint, queued, DO_NOT_SKIP_LIMITS)).thenReturn(Optional.empty());
    ListenableFuture<Response> result = queuedChannel.maybeExecute(endpoint, queued).get();
    verify(delegate, times(2)).maybeExecute(endpoint, queued, DO_NOT_SKIP_LIMITS);
    when(delegate.maybeExecute(endpoint, request, DO_NOT_SKIP_LIMITS)).thenReturn(Optional.of(Futures.immediateFuture(Mockito.mock(Response.class))));
    when(delegate.maybeExecute(endpoint, queued, DO_NOT_SKIP_LIMITS)).thenReturn(maybeResponse);
    queuedChannel.maybeExecute(endpoint, request);
    result.cancel(true);
    assertThat(futureResponse).isCancelled();
    verify(delegate, times(1)).maybeExecute(endpoint, request, DO_NOT_SKIP_LIMITS);
    verify(delegate, times(3)).maybeExecute(endpoint, queued, DO_NOT_SKIP_LIMITS);
}
Also used : Response(com.palantir.dialogue.Response) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test)

Example 43 with Response

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

the class DialogueTracing method responseTranslator.

static TagTranslator<Response> responseTranslator(ImmutableMap<String, String> tags) {
    return new TagTranslator<>() {

        @Override
        public <T> void translate(TagAdapter<T> sink, T target, Response response) {
            sink.tag(target, tags);
            int status = response.code();
            sink.tag(target, "outcome", status / 100 == 2 ? "success" : "failure");
            sink.tag(target, TraceTags.HTTP_STATUS_CODE, Integer.toString(status));
        }
    };
}
Also used : Response(com.palantir.dialogue.Response) TagTranslator(com.palantir.tracing.TagTranslator) Endpoint(com.palantir.dialogue.Endpoint)

Example 44 with Response

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

the class HostMetricsChannel method execute.

@Override
public ListenableFuture<Response> execute(Endpoint endpoint, Request request) {
    ListenableFuture<Response> result = delegate.execute(endpoint, request);
    DialogueFutures.addDirectCallback(result, new Callback());
    return result;
}
Also used : Response(com.palantir.dialogue.Response) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Example 45 with Response

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

the class DialogueClientsTest method getStickyChannels_behaves_when_just_one_uri.

@Test
void getStickyChannels_behaves_when_just_one_uri() {
    StickyChannelFactory stickyChannels = DialogueClients.create(Refreshable.only(scb)).withUserAgent(TestConfigurations.AGENT).withMaxNumRetries(0).getStickyChannels("multipass");
    ListenableFuture<Response> future = stickyChannels.getStickyChannel().execute(TestEndpoint.POST, Request.builder().build());
    assertThatThrownBy(future::get).describedAs("Made a real network call").hasCauseInstanceOf(UnknownHostException.class);
}
Also used : Response(com.palantir.dialogue.Response) StickyChannelFactory(com.palantir.dialogue.clients.DialogueClients.StickyChannelFactory) 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