Search in sources :

Example 21 with Endpoint

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

the class BalancedNodeSelectionStrategyChannelTest method when_both_channels_are_free_we_get_roughly_fair_tiebreaking.

@Test
void when_both_channels_are_free_we_get_roughly_fair_tiebreaking() {
    set200(chan1);
    set200(chan2);
    for (int i = 0; i < 200; i++) {
        channel.maybeExecute(endpoint, request, LimitEnforcement.DEFAULT_ENABLED);
    }
    verify(chan1, times(99)).maybeExecute(eq(endpoint), any(), eq(LimitEnforcement.DEFAULT_ENABLED));
    verify(chan2, times(101)).maybeExecute(eq(endpoint), any(), eq(LimitEnforcement.DEFAULT_ENABLED));
}
Also used : TestEndpoint(com.palantir.dialogue.TestEndpoint) Endpoint(com.palantir.dialogue.Endpoint) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with Endpoint

use of com.palantir.dialogue.Endpoint 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 23 with Endpoint

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

Example 24 with Endpoint

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

the class DialogueChannelTest method makeStructuredRequestToClosedConnection.

private ListenableFuture<Response> makeStructuredRequestToClosedConnection(Supplier<IOException> failure) {
    AtomicInteger channelInteractions = new AtomicInteger();
    channel = DialogueChannel.builder().channelName("my-channel").clientConfiguration(stubConfig).factory(_args -> (_endpoint, currentRequest) -> {
        int interactions = channelInteractions.incrementAndGet();
        if (interactions > 1) {
            return Futures.immediateFuture(new TestResponse().code(204).withHeader("Interactions", Integer.toString(interactions)));
        }
        Optional<RequestBody> body = currentRequest.body();
        assertThat(body).isPresent();
        try {
            body.get().writeTo(new ThrowingOutputStream(failure));
        } catch (IOException e) {
            return Futures.immediateFailedFuture(e);
        }
        throw new AssertionError("Expected an exception");
    }).random(new Random(123456L)).maxQueueSize(1).build();
    RequestBody defaultStructuredBody = DefaultConjureRuntime.builder().build().bodySerDe().serializer(new TypeMarker<List<String>>() {
    }).serialize(ImmutableList.of("test"));
    return channel.execute(endpoint, Request.builder().body(defaultStructuredBody).build());
}
Also used : Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestResponse(com.palantir.dialogue.TestResponse) TypeMarker(com.palantir.dialogue.TypeMarker) IOException(java.io.IOException) TestEndpoint(com.palantir.dialogue.TestEndpoint) Endpoint(com.palantir.dialogue.Endpoint) RequestBody(com.palantir.dialogue.RequestBody)

Example 25 with Endpoint

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

the class DialogueChannelTest method bad_channel_throwing_an_error_still_returns_a_future.

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

        @Override
        public ListenableFuture<Response> execute(Endpoint _endpoint, Request _request) {
            throw new NoClassDefFoundError("something is broken");
        }
    };
    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(NoClassDefFoundError.class);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) 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

Endpoint (com.palantir.dialogue.Endpoint)30 Channel (com.palantir.dialogue.Channel)17 Request (com.palantir.dialogue.Request)15 Response (com.palantir.dialogue.Response)14 Test (org.junit.jupiter.api.Test)11 TestEndpoint (com.palantir.dialogue.TestEndpoint)10 TestResponse (com.palantir.dialogue.TestResponse)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 Test (org.junit.Test)8 EndpointChannel (com.palantir.dialogue.EndpointChannel)6 UrlBuilder (com.palantir.dialogue.UrlBuilder)6 Optional (java.util.Optional)6 Random (java.util.Random)6 Futures (com.google.common.util.concurrent.Futures)5 HttpMethod (com.palantir.dialogue.HttpMethod)5 IOException (java.io.IOException)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Meter (com.codahale.metrics.Meter)4 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)4