Search in sources :

Example 36 with Request

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

the class DefaultClientsTest method testCallClosesRequestOnCompletion_failure.

@ParameterizedTest
@EnumSource(CallType.class)
public void testCallClosesRequestOnCompletion_failure(CallType callType) {
    RequestBody body = mock(RequestBody.class);
    Request request = Request.builder().body(body).build();
    when(channel.execute(eq(endpoint), eq(request))).thenReturn(responseFuture);
    ListenableFuture<String> result = call(callType, request);
    // The request has been sent, but not yet completed
    verifyExecutionStarted(request);
    verify(body, never()).close();
    // Upon completion the request should be closed
    IllegalStateException exception = new IllegalStateException();
    responseFuture.setException(exception);
    assertThat(result).failsWithin(Duration.ofSeconds(1)).withThrowableOfType(ExecutionException.class).withCause(exception);
    verify(body).close();
}
Also used : SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) Request(com.palantir.dialogue.Request) ExecutionException(java.util.concurrent.ExecutionException) RequestBody(com.palantir.dialogue.RequestBody) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 37 with Request

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

the class QueueOverrideChannelTest method routesToDefault.

@Test
public void routesToDefault() {
    Request request = Request.builder().build();
    assertThat(queueOverrideChannel.execute(TestEndpoint.GET, request)).isNull();
    verify(defaultDelegate).execute(TestEndpoint.GET, request);
}
Also used : Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test)

Example 38 with Request

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

the class QueuedChannelTest method testQueuedResponseAvoidsExecutingCancelled.

@Test
public void testQueuedResponseAvoidsExecutingCancelled() {
    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);
    assertThat(result.cancel(true)).isTrue();
    when(delegate.maybeExecute(endpoint, request, DO_NOT_SKIP_LIMITS)).thenReturn(Optional.of(Futures.immediateFuture(Mockito.mock(Response.class))));
    queuedChannel.maybeExecute(endpoint, request);
    verify(delegate, times(1)).maybeExecute(endpoint, request, DO_NOT_SKIP_LIMITS);
    // Should not have been invoked any more.
    verify(delegate, times(2)).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 39 with Request

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

the class QueuedChannelTest method testQueuedRequestExecutedOnNextSubmission_throws.

@Test
public void testQueuedRequestExecutedOnNextSubmission_throws() throws ExecutionException, InterruptedException {
    // First request is limited by the channel and queued
    Request queuedRequest = Mockito.mock(Request.class);
    when(delegate.maybeExecute(endpoint, queuedRequest, DO_NOT_SKIP_LIMITS)).thenReturn(Optional.empty());
    ListenableFuture<Response> queuedFuture = queuedChannel.maybeExecute(endpoint, queuedRequest).get();
    verify(delegate, times(2)).maybeExecute(endpoint, queuedRequest, DO_NOT_SKIP_LIMITS);
    assertThat(queuedFuture).isNotDone();
    // Second request succeeds and the queued request is attempted, but throws an exception
    futureResponse.set(mockResponse);
    when(delegate.maybeExecute(endpoint, request, DO_NOT_SKIP_LIMITS)).thenReturn(maybeResponse);
    when(delegate.maybeExecute(endpoint, queuedRequest, DO_NOT_SKIP_LIMITS)).thenThrow(new NullPointerException("expected"));
    ListenableFuture<Response> completed = queuedChannel.maybeExecute(endpoint, request).get();
    // Both results should be completed. The thrown exception should
    // be converted into a failed future by NeverThrowLimitedChannel
    assertThat(completed).isDone();
    assertThat(queuedFuture).isDone();
    assertThat(completed.get()).isEqualTo(mockResponse);
    assertThatThrownBy(queuedFuture::get).hasRootCauseMessage("expected");
    verify(delegate, times(1)).maybeExecute(endpoint, request, DO_NOT_SKIP_LIMITS);
    verify(delegate, times(3)).maybeExecute(endpoint, queuedRequest, DO_NOT_SKIP_LIMITS);
}
Also used : Response(com.palantir.dialogue.Response) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test)

Example 40 with Request

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

the class RetryOtherValidatingChannelTest method execute.

private void execute(@Nullable String retryOtherUri) {
    RetryOtherValidatingChannel channel = new RetryOtherValidatingChannel(delegate, ImmutableSet.of("https://host3.palantir.dev:9090/service/api", "https://host1.palantir.dev:9090/service/api").stream().map(RetryOtherValidatingChannel::strictParseHost).collect(Collectors.toSet()), failureReporter);
    Request request = Request.builder().build();
    TestResponse response = TestResponse.withBody(null).code(308);
    if (retryOtherUri != null) {
        response = response.withHeader(HttpHeaders.LOCATION, retryOtherUri);
    }
    when(delegate.execute(TestEndpoint.GET, request)).thenReturn(Futures.immediateFuture(response));
    assertThat(channel.execute(TestEndpoint.GET, request)).succeedsWithin(Duration.ZERO).isEqualTo(response);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Request(com.palantir.dialogue.Request)

Aggregations

Request (com.palantir.dialogue.Request)40 Test (org.junit.jupiter.api.Test)21 Channel (com.palantir.dialogue.Channel)14 Endpoint (com.palantir.dialogue.Endpoint)14 Response (com.palantir.dialogue.Response)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 TestResponse (com.palantir.dialogue.TestResponse)8 Test (org.junit.Test)8 TestEndpoint (com.palantir.dialogue.TestEndpoint)6 Optional (java.util.Optional)6 Futures (com.google.common.util.concurrent.Futures)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)5 RequestBody (com.palantir.dialogue.RequestBody)5 UrlBuilder (com.palantir.dialogue.UrlBuilder)5 OutputStream (java.io.OutputStream)5 Duration (java.time.Duration)5 ExecutionException (java.util.concurrent.ExecutionException)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 EnumSource (org.junit.jupiter.params.provider.EnumSource)5 ImmutableList (com.google.common.collect.ImmutableList)4