Search in sources :

Example 61 with Response

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

the class CautiousIncreaseAggressiveDecreaseConcurrencyLimiterTest method onSuccess_dropsIfResponseIndicatesQosOrError_host_503.

@Test
public void onSuccess_dropsIfResponseIndicatesQosOrError_host_503() {
    CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter = limiter(Behavior.HOST_LEVEL);
    Response response = mock(Response.class);
    when(response.code()).thenReturn(503);
    double max = limiter.getLimit();
    limiter.acquire(LimitEnforcement.DEFAULT_ENABLED).get().onSuccess(response);
    assertThat(limiter.getLimit()).as("For status %d", 503).isCloseTo(max * 0.9, Percentage.withPercentage(5));
}
Also used : Response(com.palantir.dialogue.Response) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 62 with Response

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

the class CautiousIncreaseAggressiveDecreaseConcurrencyLimiterTest method onSuccess_releasesSuccessfully.

@ParameterizedTest
@EnumSource(Behavior.class)
public void onSuccess_releasesSuccessfully(Behavior behavior) {
    CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter = limiter(behavior);
    Response response = mock(Response.class);
    when(response.code()).thenReturn(200);
    double max = limiter.getLimit();
    limiter.acquire(LimitEnforcement.DEFAULT_ENABLED).get().onSuccess(response);
    assertThat(limiter.getLimit()).isEqualTo(max);
}
Also used : Response(com.palantir.dialogue.Response) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 63 with Response

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

the class ContentDecodingChannelTest method testOnlyDecodesGzip.

@Test
public void testOnlyDecodesGzip() throws Exception {
    byte[] content = new byte[] { 1, 2, 3, 4 };
    Response response = new ContentDecodingChannel(_request -> Futures.immediateFuture(new TestResponse(content).withHeader("content-encoding", "unknown"))).execute(Request.builder().build()).get();
    assertThat(response.headers()).containsAllEntriesOf(ImmutableListMultimap.of("content-encoding", "unknown"));
    assertThat(ByteStreams.toByteArray(response.body())).isEqualTo(content);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) TestResponse(com.palantir.dialogue.TestResponse) Test(org.junit.jupiter.api.Test)

Example 64 with Response

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

the class DialogueChannelTest method test_queue_rejection_is_not_retried.

@Test
void test_queue_rejection_is_not_retried() {
    when(mockChannel.execute(any(), any())).thenReturn(SettableFuture.create());
    channel = DialogueChannel.builder().channelName("my-channel").clientConfiguration(stubConfig).factory(_args -> mockChannel).random(new Random(123456L)).maxQueueSize(1).build();
    // Saturate the concurrency limiter
    int initialConcurrencyLimit = 20;
    for (int i = 0; i < initialConcurrencyLimit; i++) {
        ListenableFuture<Response> running = channel.execute(endpoint, request);
        assertThat(running).isNotDone();
    }
    // Queue a request
    ListenableFuture<Response> queued = channel.execute(endpoint, request);
    assertThat(queued).isNotDone();
    // Next request should be rejected.
    ListenableFuture<Response> rejected = channel.execute(endpoint, request);
    assertThat(rejected).isDone();
    assertThatThrownBy(rejected::get).hasRootCauseExactlyInstanceOf(SafeRuntimeException.class).hasMessageContaining("queue is full");
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) Random(java.util.Random) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) TestEndpoint(com.palantir.dialogue.TestEndpoint) Endpoint(com.palantir.dialogue.Endpoint) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 65 with Response

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

the class DialogueChannelTest method bad_channel_throwing_an_exception_still_returns_a_future.

@Test
public void bad_channel_throwing_an_exception_still_returns_a_future() {
    Channel badUserImplementation = new Channel() {

        @Override
        public ListenableFuture<Response> execute(Endpoint _endpoint, Request _request) {
            throw new IllegalStateException("Always throw");
        }
    };
    channel = DialogueChannel.builder().channelName("my-channel").clientConfiguration(stubConfig).factory(_args -> badUserImplementation).build();
    // this should never throw
    ListenableFuture<Response> future = channel.execute(endpoint, request);
    // only when we access things do we allow exceptions
    assertThatThrownBy(() -> Futures.getUnchecked(future)).hasCauseInstanceOf(IllegalStateException.class);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) TestEndpoint(com.palantir.dialogue.TestEndpoint) Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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