|
| 1 | +package net.laprun.sustainability.power.measure; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import net.laprun.sustainability.power.SensorMetadata; |
| 8 | +import net.laprun.sustainability.power.SensorUnit; |
| 9 | +import net.laprun.sustainability.power.analysis.ComponentProcessor; |
| 10 | +import net.laprun.sustainability.power.analysis.MeasureProcessor; |
| 11 | +import net.laprun.sustainability.power.analysis.Processors; |
| 12 | +import net.laprun.sustainability.power.analysis.TotalMeasureProcessor; |
| 13 | + |
| 14 | +public class StoppedPowerMeasureTest { |
| 15 | + private final static SensorMetadata metadata = SensorMetadata |
| 16 | + .withNewComponent("cp1", null, true, "mW") |
| 17 | + .withNewComponent("cp2", null, true, "mW") |
| 18 | + .withNewComponent("cp3", null, true, "mW") |
| 19 | + .build(); |
| 20 | + |
| 21 | + @Test |
| 22 | + void shouldUseInitialProcessorsIfAvailable() { |
| 23 | + final var measure = new OngoingPowerMeasure(metadata); |
| 24 | + final var processor = new TotalMeasureProcessor(metadata, SensorUnit.mW, 0); |
| 25 | + measure.registerMeasureProcessor(processor); |
| 26 | + final var compProc = new ComponentProcessor() { |
| 27 | + }; |
| 28 | + measure.registerProcessorFor(0, compProc); |
| 29 | + |
| 30 | + final var stopped = new StoppedPowerMeasure(measure); |
| 31 | + final var stoppedProcs = stopped.processors(); |
| 32 | + assertThat(stoppedProcs).isNotEqualTo(Processors.empty); |
| 33 | + assertThat(stoppedProcs.measureProcessors()).hasSize(1); |
| 34 | + assertThat(stoppedProcs.measureProcessors().getFirst()).isEqualTo(processor); |
| 35 | + assertThat(stoppedProcs.processorsFor(0)).hasSize(1); |
| 36 | + assertThat(stoppedProcs.processorsFor(0).getFirst()).isEqualTo(compProc); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void shouldAlsoUseOwnProcessorsIfAvailable() { |
| 41 | + final var measure = new OngoingPowerMeasure(metadata); |
| 42 | + final var processor = new TotalMeasureProcessor(metadata, SensorUnit.mW, 0); |
| 43 | + measure.registerMeasureProcessor(processor); |
| 44 | + final var ongoingCompProcOutput = "component from ongoing"; |
| 45 | + final var compProc = new ComponentProcessor() { |
| 46 | + @Override |
| 47 | + public String output() { |
| 48 | + return ongoingCompProcOutput; |
| 49 | + } |
| 50 | + }; |
| 51 | + measure.registerProcessorFor(0, compProc); |
| 52 | + |
| 53 | + final var stopped = new StoppedPowerMeasure(measure); |
| 54 | + final var stoppedCompProcName = "stopped component processor"; |
| 55 | + final var stoppedCompProcOutput = "component from stopped"; |
| 56 | + final var stoppedProc = new ComponentProcessor() { |
| 57 | + @Override |
| 58 | + public String name() { |
| 59 | + return stoppedCompProcName; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String output() { |
| 64 | + return stoppedCompProcOutput; |
| 65 | + } |
| 66 | + }; |
| 67 | + stopped.registerProcessorFor(0, stoppedProc); |
| 68 | + final var stoppedMeasureOutput = "measure from stopped"; |
| 69 | + final var stoppedMeasureProcName = "stopped measure processor"; |
| 70 | + final var stoppedMeasureProc = new MeasureProcessor() { |
| 71 | + @Override |
| 72 | + public String name() { |
| 73 | + return stoppedMeasureProcName; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public String output() { |
| 78 | + return stoppedMeasureOutput; |
| 79 | + } |
| 80 | + }; |
| 81 | + stopped.registerMeasureProcessor(stoppedMeasureProc); |
| 82 | + final var stoppedProcs = stopped.processors(); |
| 83 | + assertThat(stoppedProcs).isNotEqualTo(Processors.empty); |
| 84 | + final var measureProcessors = stoppedProcs.measureProcessors(); |
| 85 | + assertThat(measureProcessors).hasSize(2); |
| 86 | + assertThat(measureProcessors.getFirst()).isEqualTo(processor); |
| 87 | + assertThat(measureProcessors.getLast()).isEqualTo(stoppedMeasureProc); |
| 88 | + final var componentProcessors = stoppedProcs.processorsFor(0); |
| 89 | + assertThat(componentProcessors).hasSize(2); |
| 90 | + assertThat(componentProcessors.getFirst()).isEqualTo(compProc); |
| 91 | + assertThat(componentProcessors.getLast()).isEqualTo(stoppedProc); |
| 92 | + |
| 93 | + final var output = PowerMeasure.asString(stopped); |
| 94 | + assertThat(output).contains(stoppedCompProcOutput, stoppedMeasureOutput, ongoingCompProcOutput); |
| 95 | + // second anonymous class |
| 96 | + assertThat(output).contains(stoppedCompProcName, stoppedMeasureProcName, "Aggregated total from (cp1)", |
| 97 | + getClass().getName() + "$2"); |
| 98 | + assertThat(output).contains("0.00mW"); |
| 99 | + assertThat(output).doesNotContain("Infinity"); |
| 100 | + } |
| 101 | +} |
0 commit comments