Search in sources :

Example 56 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class KeepAliveTest method setUp.

@BeforeAll
static void setUp() {
    LogConfig.configureRuntime();
    server = WebServer.builder().routing(Routing.builder().register("/close", rules -> rules.any((req, res) -> {
        req.content().forEach(dataChunk -> {
            // consume only first from two chunks
            dataChunk.release();
            throw new RuntimeException("BOOM!");
        }).exceptionally(res::send);
    })).register("/plain", rules -> rules.any((req, res) -> {
        req.content().forEach(DataChunk::release).onComplete(res::send).ignoreElement();
    })).build()).build();
    server.start().await();
    String serverUrl = "http://localhost:" + server.port();
    webClient = WebClient.builder().baseUri(serverUrl).keepAlive(true).build();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) RepeatedTest(org.junit.jupiter.api.RepeatedTest) WebClient(io.helidon.webclient.WebClient) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) WebClientResponse(io.helidon.webclient.WebClientResponse) DataChunk(io.helidon.common.http.DataChunk) Matchers.not(org.hamcrest.Matchers.not) AsciiString(io.netty.util.AsciiString) CompletionException(java.util.concurrent.CompletionException) ByteBuffer(java.nio.ByteBuffer) Executors(java.util.concurrent.Executors) Matchers.hasKey(org.hamcrest.Matchers.hasKey) TimeUnit(java.util.concurrent.TimeUnit) AfterAll(org.junit.jupiter.api.AfterAll) List(java.util.List) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) Optional(java.util.Optional) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LogConfig(io.helidon.common.LogConfig) Multi(io.helidon.common.reactive.Multi) AsciiString(io.netty.util.AsciiString) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 57 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class KeepAliveTest method testCall.

private static void testCall(WebClient webClient, boolean keepAlive, String path, int expectedStatus, AsciiString expectedConnectionHeader, boolean ignoreConnectionClose) {
    WebClientResponse res = null;
    try {
        res = webClient.put().keepAlive(keepAlive).path(path).submit(Multi.interval(10, TimeUnit.MILLISECONDS, Executors.newSingleThreadScheduledExecutor()).limit(2).map(l -> "msg_" + l).map(String::getBytes).map(ByteBuffer::wrap).map(bb -> DataChunk.create(true, true, bb))).await(Duration.ofMinutes(5));
        assertThat(res.status().code(), is(expectedStatus));
        if (expectedConnectionHeader != null) {
            assertThat(res.headers().toMap(), hasEntry(HttpHeaderNames.CONNECTION.toString(), List.of(expectedConnectionHeader.toString())));
        } else {
            assertThat(res.headers().toMap(), not(hasKey(HttpHeaderNames.CONNECTION.toString())));
        }
        res.content().forEach(DataChunk::release);
    } catch (CompletionException e) {
        if (ignoreConnectionClose && e.getMessage().contains("Connection reset")) {
            // socket)
            return;
        }
        throw e;
    } finally {
        Optional.ofNullable(res).ifPresent(WebClientResponse::close);
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) RepeatedTest(org.junit.jupiter.api.RepeatedTest) WebClient(io.helidon.webclient.WebClient) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) WebClientResponse(io.helidon.webclient.WebClientResponse) DataChunk(io.helidon.common.http.DataChunk) Matchers.not(org.hamcrest.Matchers.not) AsciiString(io.netty.util.AsciiString) CompletionException(java.util.concurrent.CompletionException) ByteBuffer(java.nio.ByteBuffer) Executors(java.util.concurrent.Executors) Matchers.hasKey(org.hamcrest.Matchers.hasKey) TimeUnit(java.util.concurrent.TimeUnit) AfterAll(org.junit.jupiter.api.AfterAll) List(java.util.List) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) Optional(java.util.Optional) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LogConfig(io.helidon.common.LogConfig) Multi(io.helidon.common.reactive.Multi) WebClientResponse(io.helidon.webclient.WebClientResponse) CompletionException(java.util.concurrent.CompletionException) DataChunk(io.helidon.common.http.DataChunk) ByteBuffer(java.nio.ByteBuffer)

Example 58 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class OrderOfWritesTest method threadMixUp.

@Test
void threadMixUp() throws Exception {
    final String expected = "_1_2_3";
    final byte[] underscore = "_".getBytes(StandardCharsets.UTF_8);
    final ExecutorService exec = Executors.newSingleThreadExecutor();
    WebServer server = null;
    try {
        server = WebServer.builder().routing(Routing.builder().get((req, res) -> res.send(Multi.just("1", "2", "3").map(String::valueOf).map(String::getBytes).observeOn(exec).flatMap(number -> Multi.just(DataChunk.create(false, ByteBuffer.wrap(underscore)), DataChunk.create(true, ByteBuffer.wrap(number)))))).build()).host("localhost").port(0).build().start().await(TIME_OUT);
        WebClient client = WebClient.builder().baseUri("http://localhost:" + server.port()).connectTimeout(TIME_OUT.toMillis(), TimeUnit.MILLISECONDS).build();
        for (AtomicInteger i = new AtomicInteger(); i.get() < 5000; i.getAndIncrement()) {
            WebClientResponse response = null;
            try {
                response = client.get().request().await(TIME_OUT);
                Assertions.assertEquals(Http.ResponseStatus.Family.SUCCESSFUL, response.status().family());
                String content = response.content().as(String.class).await(TIME_OUT);
                Assertions.assertEquals(expected, content, "Failed at " + i.get() + " " + content);
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        }
    } finally {
        exec.shutdownNow();
        if (server != null) {
            server.shutdown().await(TIME_OUT);
        }
        assertThat(exec.awaitTermination(TIME_OUT.toMillis(), TimeUnit.MILLISECONDS), is(true));
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Aggregations

WebClient (io.helidon.webclient.WebClient)58 Test (org.junit.jupiter.api.Test)42 WebClientResponse (io.helidon.webclient.WebClientResponse)21 JsonObject (jakarta.json.JsonObject)15 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)13 Http (io.helidon.common.http.Http)11 Config (io.helidon.config.Config)11 Collections (java.util.Collections)10 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)9 WebClientService (io.helidon.webclient.spi.WebClientService)9 Json (jakarta.json.Json)9 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)8 IOException (java.io.IOException)8 DataChunk (io.helidon.common.http.DataChunk)7 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)7 CompletionException (java.util.concurrent.CompletionException)7 Context (io.helidon.common.context.Context)6 List (java.util.List)6 TimeUnit (java.util.concurrent.TimeUnit)6 Counter (org.eclipse.microprofile.metrics.Counter)6