Skip to content

Commit a794044

Browse files
committed
fix: checking the streaming endpoint now properly handles the stream
Tests are only currently passing due to an Arc exception that stops the stream after one item. Quarkus 3.24 fixes that issue thus making the endpoint stream forever leading to the tests never ending.
1 parent 5683ed6 commit a794044

File tree

4 files changed

+53
-46
lines changed

4 files changed

+53
-46
lines changed

server/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
<artifactId>power-server-metadata</artifactId>
2323
<version>${project.version}</version>
2424
</dependency>
25+
<dependency>
26+
<groupId>org.assertj</groupId>
27+
<artifactId>assertj-core</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>io.quarkus</groupId>
32+
<artifactId>quarkus-rest-client</artifactId>
33+
<scope>test</scope>
34+
</dependency>
2535
</dependencies>
2636
<build>
2737
<extensions>
Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,13 @@
11
package net.laprun.sustainability.power;
22

3-
import static io.restassured.RestAssured.given;
4-
import static org.junit.jupiter.api.Assertions.*;
5-
6-
import java.util.Set;
7-
8-
import org.junit.jupiter.api.Test;
9-
103
import io.quarkus.test.junit.QuarkusTest;
114
import io.quarkus.test.junit.TestProfile;
125

136
@QuarkusTest
147
@TestProfile(CIQuarkusTestProfile.class) // only activated when quarkus.test.profile.tags='ci', uses @Mock annotated beans
15-
public class CIPowerResourceTest {
8+
public class CIPowerResourceTest extends PowerResourceTest {
169

1710
protected long getPid() {
1811
return 29419;
1912
}
20-
21-
@Test
22-
public void testPowerEndpoint() {
23-
final var pid = getPid();
24-
given()
25-
.when().get("/power/" + pid)
26-
.then()
27-
.statusCode(200);
28-
}
29-
30-
@Test
31-
public void testMacOSAppleSiliconMetadataEndpoint() {
32-
final var metadata = given()
33-
.when().get("/power/metadata")
34-
.then()
35-
.statusCode(200)
36-
.extract().body().as(SensorMetadata.class);
37-
assertEquals(4, metadata.componentCardinality());
38-
assertTrue(metadata.documentation().contains("powermetrics"));
39-
assertTrue(metadata.components().keySet().containsAll(Set.of("CPU", "GPU", "ANE", "cpuShare")));
40-
41-
final var cpu = metadata.metadataFor("CPU");
42-
assertEquals(0, cpu.index());
43-
assertEquals("CPU", cpu.name());
44-
assertEquals(SensorUnit.mW, cpu.unit());
45-
assertTrue(cpu.isAttributed());
46-
47-
final var cpuShare = metadata.metadataFor("cpuShare");
48-
assertEquals(3, cpuShare.index());
49-
assertEquals("cpuShare", cpuShare.name());
50-
assertEquals(SensorUnit.decimalPercentage, cpuShare.unit());
51-
assertFalse(cpuShare.isAttributed());
52-
}
5313
}

server/src/test/java/net/laprun/sustainability/power/PowerResourceTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static io.restassured.RestAssured.given;
44
import static org.junit.jupiter.api.Assertions.*;
55

6+
import java.net.URI;
67
import java.time.Duration;
78
import java.util.Set;
89

@@ -11,18 +12,19 @@
1112
import org.junit.jupiter.api.condition.EnabledOnOs;
1213
import org.junit.jupiter.api.condition.OS;
1314

15+
import io.quarkus.test.common.http.TestHTTPResource;
1416
import io.quarkus.test.junit.QuarkusTest;
1517

1618
@QuarkusTest
1719
public class PowerResourceTest {
1820

21+
@TestHTTPResource
22+
URI uri;
23+
1924
@Test
20-
public void testPowerEndpoint() {
25+
public void testPowerEndpoint() throws Exception {
2126
final var pid = getPid();
22-
given()
23-
.when().get("/power/" + pid)
24-
.then()
25-
.statusCode(200);
27+
StreamChecker.checkPowerForPID(uri, pid);
2628
}
2729

2830
@Test
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.laprun.sustainability.power;
2+
3+
import java.net.URI;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.concurrent.CompletableFuture;
8+
import java.util.concurrent.TimeUnit;
9+
10+
import jakarta.ws.rs.client.ClientBuilder;
11+
import jakarta.ws.rs.sse.SseEventSource;
12+
13+
import org.assertj.core.api.Assertions;
14+
15+
public class StreamChecker {
16+
17+
static void checkPowerForPID(URI uri, long pid) throws Exception {
18+
final var powerForPid = ClientBuilder.newClient().target(uri.resolve("power"))
19+
.path("{pid}").resolveTemplate("pid", pid);
20+
21+
try (final var eventSource = SseEventSource.target(powerForPid).build()) {
22+
CompletableFuture<List<String>> res = new CompletableFuture<>();
23+
List<String> collect = Collections.synchronizedList(new ArrayList<>());
24+
eventSource.register(inboundSseEvent -> {
25+
collect.add(inboundSseEvent.readData());
26+
// stop after one event
27+
eventSource.close();
28+
},
29+
res::completeExceptionally,
30+
() -> res.complete(collect));
31+
eventSource.open();
32+
Assertions.assertThat(res.get(5, TimeUnit.SECONDS)).hasSize(1);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)