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();
}
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);
}
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);
}
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);
}
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);
}
Aggregations