Search in sources :

Example 16 with Request

use of com.palantir.dialogue.Request 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 17 with Request

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

the class PinUntilErrorNodeSelectionStrategyChannelTest method sticky_request_do_not_switch_nodes.

@Test
void sticky_request_do_not_switch_nodes() throws ExecutionException {
    setResponse(channel1, 100);
    setResponse(channel2, 101);
    assertThat(getCode(pinUntilError)).describedAs("On channel2 initially").isEqualTo(101);
    Request requestSticky = Request.builder().build();
    StickyAttachments.requestStickyToken(requestSticky);
    int errorStatus = 500;
    setResponse(channel2, errorStatus);
    Consumer<Request> requestConsumer = StickyAttachments.copyStickyTarget(Futures.getDone(pinUntilError.maybeExecute(null, requestSticky, LimitEnforcement.DEFAULT_ENABLED).get()));
    assertThat(IntStream.range(0, 6).map(_number -> getCode(pinUntilError))).describedAs("A single error code should switch us to channel 1").contains(100, 100, 100, 100, 100, 100);
    assertThat(IntStream.range(0, 6).map(_number -> {
        Request stickyRequest = Request.builder().build();
        requestConsumer.accept(stickyRequest);
        return getCode(pinUntilError, stickyRequest);
    })).describedAs("sticky request continues being pinned to channel 2").contains(errorStatus, errorStatus, errorStatus, errorStatus, errorStatus, errorStatus);
    assertThat(IntStream.range(0, 6).map(_number -> getCode(pinUntilError))).describedAs("unpinned requests stay on channel 1").contains(100, 100, 100, 100, 100, 100);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Strictness(org.mockito.quality.Strictness) Ticker(com.github.benmanes.caffeine.cache.Ticker) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) SettableFuture(com.google.common.util.concurrent.SettableFuture) Mockito.lenient(org.mockito.Mockito.lenient) ImmutableList(com.google.common.collect.ImmutableList) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Duration(java.time.Duration) Request(com.palantir.dialogue.Request) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) LimitEnforcement(com.palantir.dialogue.core.LimitedChannel.LimitEnforcement) Mockito.when(org.mockito.Mockito.when) DefaultTaggedMetricRegistry(com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) TestResponse(com.palantir.dialogue.TestResponse) Mockito(org.mockito.Mockito) Futures(com.google.common.util.concurrent.Futures) Optional(java.util.Optional) Response(com.palantir.dialogue.Response) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test)

Example 18 with Request

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

the class QueueOverrideChannelTest method routesToOverride.

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

Example 19 with Request

use of com.palantir.dialogue.Request 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 20 with Request

use of com.palantir.dialogue.Request 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)

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