Skip to content
Snippets Groups Projects
Commit 384de3d8 authored by Sören Henning's avatar Sören Henning
Browse files

Make Titan record generator testable

parent 912abf3a
No related branches found
No related tags found
No related merge requests found
Pipeline #6089 passed
...@@ -95,7 +95,7 @@ public final class LoadGenerator { ...@@ -95,7 +95,7 @@ public final class LoadGenerator {
new KeySpace(SENSOR_PREFIX_DEFAULT, NUMBER_OF_KEYS_DEFAULT), new KeySpace(SENSOR_PREFIX_DEFAULT, NUMBER_OF_KEYS_DEFAULT),
Duration.ofMillis(PERIOD_MS_DEFAULT))) Duration.ofMillis(PERIOD_MS_DEFAULT)))
.setGeneratorConfig(new LoadGeneratorConfig( .setGeneratorConfig(new LoadGeneratorConfig(
TitanRecordGeneratorFactory.forConstantValue(VALUE_DEFAULT), TitanRecordGenerator.forConstantValue(VALUE_DEFAULT),
TitanKafkaSenderFactory.forKafkaConfig( TitanKafkaSenderFactory.forKafkaConfig(
KAFKA_BOOTSTRAP_SERVERS_DEFAULT, KAFKA_BOOTSTRAP_SERVERS_DEFAULT,
KAFKA_TOPIC_DEFAULT, KAFKA_TOPIC_DEFAULT,
...@@ -198,7 +198,7 @@ public final class LoadGenerator { ...@@ -198,7 +198,7 @@ public final class LoadGenerator {
new KeySpace(SENSOR_PREFIX_DEFAULT, numSensors), new KeySpace(SENSOR_PREFIX_DEFAULT, numSensors),
Duration.ofMillis(periodMs))) Duration.ofMillis(periodMs)))
.setGeneratorConfig(new LoadGeneratorConfig( .setGeneratorConfig(new LoadGeneratorConfig(
TitanRecordGeneratorFactory.forConstantValue(value), TitanRecordGenerator.forConstantValue(value),
recordSender)) recordSender))
.withThreads(threads); .withThreads(threads);
} }
......
package theodolite.commons.workloadgeneration; package theodolite.commons.workloadgeneration;
import java.time.Clock;
import titan.ccp.model.records.ActivePowerRecord; import titan.ccp.model.records.ActivePowerRecord;
/** /**
* A factory for creating {@link RecordGenerator}s that creates Titan {@link ActivePowerRecord}s. * A factory for creating {@link RecordGenerator}s that creates Titan {@link ActivePowerRecord}s.
*/ */
public final class TitanRecordGeneratorFactory { public final class TitanRecordGenerator implements RecordGenerator<ActivePowerRecord> {
private final Clock clock;
private TitanRecordGeneratorFactory() {} private final double constantValue;
private TitanRecordGenerator(final double constantValue) {
this.constantValue = constantValue;
this.clock = Clock.systemUTC();
}
/* default */ TitanRecordGenerator(final double constantValue, final Clock clock) {
this.constantValue = constantValue;
this.clock = clock;
}
/** /**
* Create a {@link RecordGenerator} that generates Titan {@link ActivePowerRecord}s with a * Create a {@link RecordGenerator} that generates Titan {@link ActivePowerRecord}s with a
* constant value. * constant value.
*/ */
public static RecordGenerator<ActivePowerRecord> forConstantValue(final double value) { public static RecordGenerator<ActivePowerRecord> forConstantValue(final double value) {
return sensor -> new ActivePowerRecord(sensor, System.currentTimeMillis(), value); return new TitanRecordGenerator(value);
}
@Override
public ActivePowerRecord generate(final String key) {
return new ActivePowerRecord(key, this.clock.millis(), this.constantValue);
} }
} }
package theodolite.commons.workloadgeneration;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import org.junit.Assert;
import org.junit.Test;
import titan.ccp.model.records.ActivePowerRecord;
public class TitanRecordGeneratorTest {
@Test
public void testGenerate() {
final ZoneId zoneId = ZoneOffset.UTC;
final LocalDateTime dateTime = LocalDateTime.of(2022, 1, 17, 14, 2, 42);
final Instant instant = dateTime.atZone(zoneId).toInstant();
final TitanRecordGenerator generator =
new TitanRecordGenerator(42.0, Clock.fixed(instant, zoneId));
final ActivePowerRecord activePowerRecord = generator.generate("my-identifier");
Assert.assertEquals("my-identifier", activePowerRecord.getIdentifier());
Assert.assertEquals(instant.toEpochMilli(), activePowerRecord.getTimestamp());
Assert.assertEquals(42.0, activePowerRecord.getValueInW(), 0.001);
}
@Test
public void testTimestampForArbitraryClockTimeZone() {
final LocalDateTime dateTime = LocalDateTime.of(2022, 1, 17, 14, 2, 42);
final Instant instant = dateTime.atZone(ZoneId.of("Europe/Paris")).toInstant();
// Setting of ZoneId should have no impact on result as we request epoch millis
final Clock clock = Clock.fixed(instant, ZoneId.of("America/Sao_Paulo"));
final TitanRecordGenerator generator = new TitanRecordGenerator(42.0, clock);
final ActivePowerRecord activePowerRecord = generator.generate("my-identifier");
Assert.assertEquals(instant.toEpochMilli(), activePowerRecord.getTimestamp());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment