Search in sources :

Example 21 with Response

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

the class ContentDecodingChannelTest method testDecoding_delayedFailure.

@Test
public void testDecoding_delayedFailure() throws Exception {
    Response response = new ContentDecodingChannel(_request -> Futures.immediateFuture(// Will fail because it's not valid gzip content
    new TestResponse(new byte[] { 1, 2, 3, 4 }).withHeader("content-encoding", "gzip"))).execute(Request.builder().build()).get();
    assertThat(response.headers().get("content-encoding")).isEmpty();
    assertThatThrownBy(response.body()::read).isInstanceOf(IOException.class);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) TestResponse(com.palantir.dialogue.TestResponse) Test(org.junit.jupiter.api.Test)

Example 22 with Response

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

the class DialogueChannelTest method test_can_use_sticky_attachments_impl.

private void test_can_use_sticky_attachments_impl(NodeSelectionStrategy nodeSelectionStrategy, ImmutableList<String> uris) throws ExecutionException {
    String uriHeader = "uri";
    DialogueChannelFactory factory = args -> {
        mockChannel = Mockito.mock(Channel.class);
        lenient().when(mockChannel.execute(eq(endpoint), any())).thenAnswer((Answer<ListenableFuture<Response>>) _invocation -> Futures.immediateFuture(TestResponse.withBody(null).withHeader(uriHeader, args.uri())));
        return mockChannel;
    };
    channel = DialogueChannel.builder().channelName("my-channel").clientConfiguration(ClientConfiguration.builder().uris(uris).from(stubConfig).nodeSelectionStrategy(nodeSelectionStrategy).build()).factory(factory).random(new Random(1L)).build();
    request = createRequestWithAddExecutedOnAttachment();
    response = Futures.getDone(channel.execute(endpoint, request));
    String expectedUri = response.getFirstHeader(uriHeader).get();
    Consumer<Request> stickyTarget = StickyAttachments.copyStickyTarget(response);
    request = Request.builder().build();
    stickyTarget.accept(request);
    response = Futures.getDone(channel.execute(endpoint, request));
    assertThat(response.getFirstHeader(uriHeader)).hasValue(expectedUri);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SslConfiguration(com.palantir.conjure.java.api.config.ssl.SslConfiguration) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) Random(java.util.Random) SettableFuture(com.google.common.util.concurrent.SettableFuture) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) NodeSelectionStrategy(com.palantir.conjure.java.client.config.NodeSelectionStrategy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) UserAgent(com.palantir.conjure.java.api.config.service.UserAgent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) RequestBody(com.palantir.dialogue.RequestBody) Channel(com.palantir.dialogue.Channel) Test(org.junit.jupiter.api.Test) TestResponse(com.palantir.dialogue.TestResponse) List(java.util.List) Optional(java.util.Optional) TypeMarker(com.palantir.dialogue.TypeMarker) Response(com.palantir.dialogue.Response) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Mock(org.mockito.Mock) EnumSource(org.junit.jupiter.params.provider.EnumSource) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration) Mockito.lenient(org.mockito.Mockito.lenient) TestEndpoint(com.palantir.dialogue.TestEndpoint) Supplier(java.util.function.Supplier) Answer(org.mockito.stubbing.Answer) SocketException(java.net.SocketException) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ImmutableList(com.google.common.collect.ImmutableList) SocketTimeoutException(java.net.SocketTimeoutException) DefaultConjureRuntime(com.palantir.conjure.java.dialogue.serde.DefaultConjureRuntime) Endpoint(com.palantir.dialogue.Endpoint) ServiceConfiguration(com.palantir.conjure.java.api.config.service.ServiceConfiguration) Request(com.palantir.dialogue.Request) ClientConfigurations(com.palantir.conjure.java.client.config.ClientConfigurations) OutputStream(java.io.OutputStream) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) TestTracing(com.palantir.tracing.TestTracing) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) Futures(com.google.common.util.concurrent.Futures) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Paths(java.nio.file.Paths) Collections(java.util.Collections) TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) Answer(org.mockito.stubbing.Answer) Random(java.util.Random) Request(com.palantir.dialogue.Request)

Example 23 with Response

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

the class DialogueChannelTest method when_thread_is_interrupted_no_calls_to_delegate.

@Test
public void when_thread_is_interrupted_no_calls_to_delegate() {
    Channel delegate = mock(Channel.class);
    channel = DialogueChannel.builder().channelName("my-channel").clientConfiguration(stubConfig).factory(_args -> delegate).build();
    Thread.currentThread().interrupt();
    ListenableFuture<Response> future = channel.execute(endpoint, request);
    assertThat(future).isDone();
    assertThat(future).isNotCancelled();
    verifyNoInteractions(delegate);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) Channel(com.palantir.dialogue.Channel) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 24 with Response

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

the class DialogueChannelTest method test_serialization_socket_error_is_retried.

@Test
void test_serialization_socket_error_is_retried() {
    ListenableFuture<Response> result = makeStructuredRequestToClosedConnection(() -> new SocketException("broken pipe"));
    assertThat(result).succeedsWithin(Duration.ofSeconds(2)).satisfies(res -> {
        assertThat(res.code()).isEqualTo(204);
        assertThat(res.getFirstHeader("Interactions")).as("Expected retries").hasValue("2");
    });
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) SocketException(java.net.SocketException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 25 with Response

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

the class DialogueClientsTest method getStickyChannels_behaves_when_service_doesnt_exist.

@Test
void getStickyChannels_behaves_when_service_doesnt_exist() {
    StickyChannelFactory stickyChannels = DialogueClients.create(Refreshable.only(scb)).withUserAgent(TestConfigurations.AGENT).withMaxNumRetries(0).getStickyChannels("asldjaslkdjslad");
    ListenableFuture<Response> future = stickyChannels.getStickyChannel().execute(TestEndpoint.POST, Request.builder().build());
    assertThatThrownBy(future::get).describedAs("Nice error message when services doesn't exist").hasCauseInstanceOf(SafeIllegalStateException.class).hasMessageContaining("Service not configured");
}
Also used : Response(com.palantir.dialogue.Response) StickyChannelFactory(com.palantir.dialogue.clients.DialogueClients.StickyChannelFactory) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) 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