Search in sources :

Example 6 with Response

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

the class ApacheHandshakeTimeoutTest method testHandshakeLongerThanConnectDoesNotTimeout.

@Test
public void testHandshakeLongerThanConnectDoesNotTimeout() throws Exception {
    int serverPort = getPort(server);
    ClientConfiguration config = ClientConfiguration.builder().from(TestConfigurations.create("https://localhost:" + serverPort)).connectTimeout(Duration.ofMillis(500)).readTimeout(Duration.ofSeconds(2)).writeTimeout(Duration.ofMillis(2)).maxNumRetries(0).build();
    Channel channel = create(config);
    executor.delayNextTask(Duration.ofSeconds(1));
    try (Response response = channel.execute(TestEndpoint.POST, request).get()) {
        assertThat(response.code()).isEqualTo(200);
    }
}
Also used : Response(com.palantir.dialogue.Response) Channel(com.palantir.dialogue.Channel) TestEndpoint(com.palantir.dialogue.TestEndpoint) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration) Test(org.junit.jupiter.api.Test)

Example 7 with Response

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

the class ApacheHandshakeTimeoutTest method testHandshakeTimeoutIsRetried.

@Test
public void testHandshakeTimeoutIsRetried() throws Exception {
    int serverPort = getPort(server);
    ClientConfiguration retryingConfig = ClientConfiguration.builder().from(TestConfigurations.create("https://localhost:" + serverPort)).connectTimeout(Duration.ofMillis(500)).readTimeout(Duration.ofMillis(500)).writeTimeout(Duration.ofMillis(500)).maxNumRetries(1).build();
    Channel retryChannel = create(retryingConfig);
    executor.delayNextTask(Duration.ofSeconds(1));
    try (Response response = retryChannel.execute(TestEndpoint.POST, request).get()) {
        assertThat(response.code()).isEqualTo(200);
    }
}
Also used : Response(com.palantir.dialogue.Response) Channel(com.palantir.dialogue.Channel) TestEndpoint(com.palantir.dialogue.TestEndpoint) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration) Test(org.junit.jupiter.api.Test)

Example 8 with Response

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

the class ApacheHttpClientChannelsTest method countsConnectErrors.

@Test
public void countsConnectErrors() throws Exception {
    ClientConfiguration conf = ClientConfiguration.builder().from(TestConfigurations.create("http://unused")).connectTimeout(Duration.ofMillis(1)).build();
    try (ApacheHttpClientChannels.CloseableClient client = ApacheHttpClientChannels.createCloseableHttpClient(conf, "testClient")) {
        Meter connectionCreateError = DialogueClientMetrics.of(conf.taggedMetricRegistry()).connectionCreateError().clientName("testClient").cause("ConnectTimeoutException").build();
        assertThat(connectionCreateError.getCount()).isZero();
        // 203.0.113.0/24 is a test network that should never exist
        Channel channel = ApacheHttpClientChannels.createSingleUri("http://203.0.113.23", client);
        ListenableFuture<Response> future = channel.execute(TestEndpoint.GET, Request.builder().build());
        try (Response response = Futures.getUnchecked(future)) {
            fail("This request should have failed with an connection timeout! (code: %d)", response.code());
        } catch (UncheckedExecutionException exception) {
            assertThat(exception.getCause()).isInstanceOf(SafeConnectTimeoutException.class);
        }
        assertThat(connectionCreateError.getCount()).isEqualTo(1L);
    }
}
Also used : Response(com.palantir.dialogue.Response) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Meter(com.codahale.metrics.Meter) Channel(com.palantir.dialogue.Channel) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration) AbstractChannelTest(com.palantir.dialogue.AbstractChannelTest) Test(org.junit.jupiter.api.Test)

Example 9 with Response

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

the class ApacheHttpClientChannelsTest method metrics.

@Test
public void metrics() throws Exception {
    ClientConfiguration conf = TestConfigurations.create("http://unused");
    try (ApacheHttpClientChannels.CloseableClient client = ApacheHttpClientChannels.createCloseableHttpClient(conf, "testClient")) {
        Channel channel = ApacheHttpClientChannels.createSingleUri("http://neverssl.com", client);
        ListenableFuture<Response> future = channel.execute(TestEndpoint.GET, Request.builder().build());
        TaggedMetricRegistry metrics = conf.taggedMetricRegistry();
        try (Response response = Futures.getUnchecked(future)) {
            assertThat(response.code()).isEqualTo(200);
            assertThat(poolGaugeValue(metrics, "testClient", "idle")).describedAs("available").isZero();
            assertThat(poolGaugeValue(metrics, "testClient", "leased")).describedAs("leased").isEqualTo(1L);
        }
        assertThat(poolGaugeValue(metrics, "testClient", "idle")).describedAs("available after response closed").isOne();
        assertThat(poolGaugeValue(metrics, "testClient", "leased")).describedAs("leased after response closed").isZero();
    }
}
Also used : Response(com.palantir.dialogue.Response) Channel(com.palantir.dialogue.Channel) TaggedMetricRegistry(com.palantir.tritium.metrics.registry.TaggedMetricRegistry) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration) AbstractChannelTest(com.palantir.dialogue.AbstractChannelTest) Test(org.junit.jupiter.api.Test)

Example 10 with Response

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

the class SimulationServer method execute.

@Override
public ListenableFuture<Response> execute(Endpoint endpoint, Request request) {
    Meter perEndpointRequests = MetricNames.requestMeter(simulation.taggedMetrics(), serverName, endpoint);
    activeRequests.inc();
    perEndpointRequests.mark();
    simulation.metricsReporter().report();
    for (ServerHandler handler : handlers) {
        long beforeNanos = simulation.clock().read();
        Optional<ListenableFuture<Response>> maybeResp = handler.maybeExecute(this, endpoint, request);
        if (!maybeResp.isPresent()) {
            continue;
        }
        ListenableFuture<Response> resp = maybeResp.get();
        DialogueFutures.addDirectCallback(resp, DialogueFutures.onSuccess(_ignored -> globalResponses.inc()));
        resp.addListener(() -> {
            activeRequests.dec();
            globalServerTimeNanos.inc(simulation.clock().read() - beforeNanos);
        }, DialogueFutures.safeDirectExecutor());
        if (log.isDebugEnabled()) {
            DialogueFutures.addDirectCallback(resp, DialogueFutures.onSuccess(result -> {
                log.debug("time={} server={} status={} id={}", Duration.ofNanos(simulation.clock().read()), serverName, result.code(), request != null ? request.headerParams().get(Benchmark.REQUEST_ID_HEADER) : null);
            }));
        }
        return resp;
    }
    log.error("No handler available for request {}", request);
    activeRequests.dec();
    return Futures.immediateFailedFuture(new SafeRuntimeException("No handler"));
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) Logger(org.slf4j.Logger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListMultimap(com.google.common.collect.ListMultimap) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) Predicate(java.util.function.Predicate) LoggerFactory(org.slf4j.LoggerFactory) Channel(com.palantir.dialogue.Channel) Function(java.util.function.Function) TimeUnit(java.util.concurrent.TimeUnit) Meter(com.codahale.metrics.Meter) TestResponse(com.palantir.dialogue.TestResponse) Futures(com.google.common.util.concurrent.Futures) DialogueFutures(com.palantir.dialogue.futures.DialogueFutures) ImmutableList(com.google.common.collect.ImmutableList) Duration(java.time.Duration) Counter(com.codahale.metrics.Counter) Endpoint(com.palantir.dialogue.Endpoint) Optional(java.util.Optional) ResponseAttachments(com.palantir.dialogue.ResponseAttachments) Request(com.palantir.dialogue.Request) Response(com.palantir.dialogue.Response) Preconditions(com.palantir.logsafe.Preconditions) InputStream(java.io.InputStream) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) Meter(com.codahale.metrics.Meter) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

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