Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage for issue 36402 #1508

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions http/http-sse/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.quarkus.ts.qe</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<artifactId>http-sse</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-picocli</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jsonb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
</plugin>
</plugins>
</build>
</project>
15 changes: 15 additions & 0 deletions http/http-sse/src/main/java/http/sse/MainCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package http.sse;

import io.quarkus.picocli.runtime.annotations.TopCommand;
import io.quarkus.runtime.Quarkus;

import picocli.CommandLine;

@TopCommand
@CommandLine.Command(name = "test")
public class MainCommand implements Runnable {
@Override
public void run() {
Quarkus.waitForExit();
}
}
71 changes: 71 additions & 0 deletions http/http-sse/src/main/java/http/sse/SseClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package http.sse;

import java.util.Arrays;
import java.util.concurrent.locks.LockSupport;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.sse.SseEventSource;

@ApplicationScoped
public class SseClient {

private Client client;
private SseEventSource updateSource;

@PostConstruct
void init() {
client = ClientBuilder.newClient();
}

@PreDestroy
void close() {
client.close();
if (updateSource != null)
updateSource.close();
}

public String someMethod() {
StringBuilder response = new StringBuilder();

WebTarget target = client.target("http://localhost:" + getQuarkusPort() + "/updates");
updateSource = SseEventSource.target(target).build();
updateSource.register(ev -> {
response.append("ev.isEmpty() = ").append(ev.isEmpty());
response.append("\n");
response.append("event: ").append(ev.getName()).append(" ").append(ev.readData());
response.append("\n");

}, thr -> {
response.append("Error in SSE updates\n");
response.append(Arrays.toString(thr.getStackTrace()));
// thr.printStackTrace();
});

response.append("SSE opened\n");
updateSource.open();

// LockSupport.parkNanos(7_000_000_000L);
LockSupport.parkNanos(2_000_000_000L);
return response.toString();
}

/**
* Test runner assigns random ports, on which the app should run.
* Parse this port from the CLI and return it.
* If no parameter is specified, return the default (8080)
*
* @return port on which the application is running
*/
private int getQuarkusPort() {
String value = System.getProperty("quarkus.http.port");
if (value == null || value.isEmpty()) {
return 8080;
}
return Integer.parseInt(value);
}
}
24 changes: 24 additions & 0 deletions http/http-sse/src/main/java/http/sse/SseResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package http.sse;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/sse")
public class SseResource {

@Inject
SseClient client;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getSse() {
try {
return client.someMethod();
} catch (RuntimeException exception) {
return exception.getMessage();
}
}
}
36 changes: 36 additions & 0 deletions http/http-sse/src/main/java/http/sse/UpdatesResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package http.sse;

import java.util.concurrent.locks.LockSupport;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.sse.OutboundSseEvent;
import jakarta.ws.rs.sse.Sse;
import jakarta.ws.rs.sse.SseEventSink;

@ApplicationScoped
@Path("updates")
@Produces(MediaType.SERVER_SENT_EVENTS)
public class UpdatesResource {

@Context
Sse sse;

@GET
public void updates(@Context SseEventSink eventSink) {
eventSink.send(createEvent("test234", "test"));
LockSupport.parkNanos(1_000_000_000L);
}

private OutboundSseEvent createEvent(String name, String data) {
return sse.newEventBuilder()
.name(name)
.data(data)
.build();
}

}
Empty file.
27 changes: 27 additions & 0 deletions http/http-sse/src/test/java/http/sse/HttpSseIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package http.sse;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.quarkus.test.scenarios.QuarkusScenario;

@QuarkusScenario
@Tag("QQE-257")
michalvavrik marked this conversation as resolved.
Show resolved Hide resolved
// validate issue from https://github.com/quarkusio/quarkus/issues/36402
// this test should only fail on native
public class HttpSseIT {
private static final String SSE_ERROR_MESSAGE = "java.lang.ClassNotFoundException: Provider for jakarta.ws.rs.sse.SseEventSource.Builder cannot be found";

@Test
public void testWorkingSse() {
String response = given().when().get("/sse").thenReturn().body().asString();

assertFalse(response.contains(SSE_ERROR_MESSAGE),
"SSE failed, https://github.com/quarkusio/quarkus/issues/36402 not fixed");
assertTrue(response.contains("event: test234 test"), "SSE failed, unknown bug. Response: " + response);
}
}
Loading