From 200c39b79ec80c2e7a1f5d677fb6f22b128f5a70 Mon Sep 17 00:00:00 2001 From: mocenas Date: Tue, 16 Jan 2024 13:56:42 +0100 Subject: [PATCH] Add test for Sse method indexing on native (#1613) * Add test for Sse method indexing on native --- .../reactive/resources/SseResource.java | 75 +++++++++++++++++++ .../reactive/ReactiveRestClientIT.java | 12 +++ 2 files changed, 87 insertions(+) create mode 100644 http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/resources/SseResource.java diff --git a/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/resources/SseResource.java b/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/resources/SseResource.java new file mode 100644 index 000000000..ad8b642b7 --- /dev/null +++ b/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/resources/SseResource.java @@ -0,0 +1,75 @@ +package io.quarkus.ts.http.restclient.reactive.resources; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Context; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.sse.OutboundSseEvent; +import jakarta.ws.rs.sse.Sse; +import jakarta.ws.rs.sse.SseEventSink; +import jakarta.ws.rs.sse.SseEventSource; + +import org.eclipse.microprofile.config.ConfigProvider; + +/** + * Reproducer resource for https://github.com/quarkusio/quarkus/issues/36986 + */ +@ApplicationScoped +@Path("sse") +public class SseResource { + @Context + Sse sse; + + @GET + @Path("client") + @Produces(MediaType.TEXT_PLAIN) + public Response clientUpdate() throws InterruptedException { + String host = ConfigProvider.getConfig().getValue("quarkus.http.host", String.class); + int port = ConfigProvider.getConfig().getValue("quarkus.http.port", Integer.class); + List receivedData = new CopyOnWriteArrayList<>(); + + WebTarget target = ClientBuilder.newClient().target("http://" + host + ":" + port + "/sse/server-update"); + try (SseEventSource eventSource = SseEventSource.target(target).build()) { + eventSource.register( + ev -> { + String event = "event: name=" + ev.getName() + " data={" + ev.readData() + "} and is empty: " + + ev.isEmpty() + + "\n"; + receivedData.add(event); + }, thr -> { + String event = "Error: " + thr.getMessage() + "\n" + Arrays.toString(thr.getStackTrace()); + receivedData.add(event); + }); + CountDownLatch latch = new CountDownLatch(2); + eventSource.open(); + latch.await(1, TimeUnit.SECONDS); + } + + return Response.ok(String.join("\n", receivedData)).build(); + } + + @GET + @Path("server-update") + @Produces(MediaType.SERVER_SENT_EVENTS) + public void updates(@Context SseEventSink eventSink) { + eventSink.send(createEvent("SSE data", "random SSE data")); + } + + private OutboundSseEvent createEvent(String name, String data) { + return sse.newEventBuilder() + .name(name) + .data(data) + .build(); + } +} diff --git a/http/rest-client-reactive/src/test/java/io/quarkus/ts/http/restclient/reactive/ReactiveRestClientIT.java b/http/rest-client-reactive/src/test/java/io/quarkus/ts/http/restclient/reactive/ReactiveRestClientIT.java index 8d89e3a27..cec847459 100644 --- a/http/rest-client-reactive/src/test/java/io/quarkus/ts/http/restclient/reactive/ReactiveRestClientIT.java +++ b/http/rest-client-reactive/src/test/java/io/quarkus/ts/http/restclient/reactive/ReactiveRestClientIT.java @@ -2,6 +2,7 @@ import static com.github.tomakehurst.wiremock.core.Options.ChunkedEncodingPolicy.NEVER; import static io.quarkus.ts.http.restclient.reactive.resources.PlainBookResource.SEARCH_TERM_VAL; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -22,6 +23,7 @@ import io.quarkus.test.bootstrap.Protocol; import io.quarkus.test.bootstrap.RestService; import io.quarkus.test.scenarios.QuarkusScenario; +import io.quarkus.test.scenarios.annotations.EnabledOnNative; import io.quarkus.test.scenarios.annotations.DisabledOnQuarkusVersion; import io.quarkus.test.services.QuarkusApplication; import io.quarkus.ts.http.restclient.reactive.json.Book; @@ -257,6 +259,16 @@ public void malformedChunk() { Assertions.assertEquals("io.vertx.core.http.HttpClosedException", response.body().asString()); } + @Test + @EnabledOnNative + @Tag("https://github.com/quarkusio/quarkus/issues/36986") + public void sseIndexMethodOnNativeTest() { + app.given().get("/sse/client") + .then() + .statusCode(200) + .body(containsString("random SSE data")); + } + @AfterAll static void afterAll() { mockServer.stop();