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();
}
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);
}
}
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));
}
}
Aggregations