Skip to content

Commit

Permalink
wip: attempt to test power-server client
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Mar 1, 2024
1 parent efe77f6 commit 59e8651
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 33 deletions.
6 changes: 6 additions & 0 deletions deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.laprun.sustainability</groupId>
<artifactId>power-server</artifactId>
<version>0.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkiverse.power.deployment;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.net.URI;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkiverse.power.runtime.PowerMeasurer;
import io.quarkiverse.power.runtime.ServerSampler;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import net.laprun.sustainability.power.PowerResource;

public class PowerMeasurerTest {
@TestHTTPResource
URI uri;

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClasses(PowerResource.class, TestPowerMeasurer.class, TestPowerSensor.class));

@Test
void startShouldAccumulateOverSpecifiedDurationAndStop() throws Exception {
final var measurer = new PowerMeasurer<>(new ServerSampler(uri));

measurer.start(1, 100);
measurer.onCompleted(measure -> {
assertEquals(10, measure.numberOfSamples());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkiverse.power.deployment;

import io.quarkus.test.Mock;
import net.laprun.sustainability.power.PowerMeasurer;

@Mock
public class TestPowerMeasurer extends PowerMeasurer {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkiverse.power.deployment;

import io.quarkus.test.Mock;

@Mock
public class TestPowerSensor extends net.laprun.sustainability.power.sensors.test.TestPowerSensor {
}
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>me.fabriciorby</groupId>
<artifactId>maven-surefire-junit5-tree-reporter</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
<configuration>
<reportFormat>plain</reportFormat>
<consoleOutputReporter>
<disable>true</disable>
</consoleOutputReporter>
<statelessTestsetInfoReporter
implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter">
<theme>UNICODE</theme>
</statelessTestsetInfoReporter>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
Expand Down
9 changes: 7 additions & 2 deletions runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
<artifactId>quarkus-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.github.metacosm</groupId>
<groupId>net.laprun.sustainability</groupId>
<artifactId>power-server-metadata</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -37,6 +37,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-common</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;

import io.github.metacosm.power.SensorMetadata;
import io.smallrye.mutiny.Multi;
import net.laprun.sustainability.power.SensorMetadata;

@Path("/power")
public interface PowerServerClient {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkiverse.power.runtime;

import java.net.URI;
import java.util.Arrays;
import java.util.function.Consumer;

Expand All @@ -8,10 +9,13 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.sse.SseEventSource;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.github.metacosm.power.SensorMetadata;
import io.quarkus.rest.client.reactive.jackson.runtime.serialisers.ClientJacksonMessageBodyReader;
import net.laprun.sustainability.power.SensorMeasure;
import net.laprun.sustainability.power.SensorMetadata;

public class ServerSampler implements Sampler {
private final SseEventSource powerAPI;
Expand All @@ -20,14 +24,25 @@ public class ServerSampler implements Sampler {
private io.quarkiverse.power.runtime.SensorMetadata metadata;
private static final long pid = ProcessHandle.current().pid();

public ServerSampler() {
@ConfigProperty(name = "power-server.url", defaultValue = "http://localhost:20432")
URI powerServerURI;

public ServerSampler(URI powerServerURI) {
if (powerServerURI != null) {
this.powerServerURI = powerServerURI;
}
final var client = ClientBuilder.newClient();
client.register(new ClientJacksonMessageBodyReader(new ObjectMapper()));
base = client.target("http://localhost:20432/power");
base = client.target(this.powerServerURI.resolve("power"));

final var powerForPid = base.path("{pid}").resolveTemplate("pid", pid);
powerAPI = SseEventSource.target(powerForPid).build();
powerAPI.register((sseEvent) -> update(sseEvent.readData()), (e) -> System.out.println("Exception: " + e.getMessage()));
powerAPI.register((sseEvent) -> update(sseEvent.readData(SensorMeasure.class)),
(e) -> System.out.println("Exception: " + e.getMessage()));
}

public ServerSampler() {
this(null);
}

@Override
Expand Down Expand Up @@ -55,9 +70,9 @@ public int componentCardinality() {
powerAPI.open();
}

private void update(String measureAsString) {
if (measureAsString != null) {
final var components = Arrays.stream(measureAsString.split(" ")).mapToDouble(Double::parseDouble).toArray();
private void update(SensorMeasure measureFromServer) {
if (measureFromServer != null) {
final var components = measureFromServer.components();
if (Arrays.equals(new double[] { -1.0 }, components)) {
System.out.println("Skipping invalid measure");
} else {
Expand Down

This file was deleted.

0 comments on commit 59e8651

Please sign in to comment.