Skip to content
Snippets Groups Projects
Commit d7a43929 authored by Manuel Krombholz's avatar Manuel Krombholz
Browse files

Added test for single ItemPerformance

parent e0e9ab2b
No related branches found
No related tags found
2 merge requests!12Resolve "Cleanup parser",!11Resolve "Add processing frequency option for items"
Pipeline #9591 passed
...@@ -294,7 +294,6 @@ aspect ItemHandling { ...@@ -294,7 +294,6 @@ aspect ItemHandling {
* @param shouldSendState whether to send the new state (currently affects MQTT and Influx) * @param shouldSendState whether to send the new state (currently affects MQTT and Influx)
*/ */
protected void Item.stateUpdated(boolean shouldSendState) { protected void Item.stateUpdated(boolean shouldSendState) {
this.getStateData().afterStateChangeApplied();
if (shouldSendState) { if (shouldSendState) {
try { try {
// sendState() refined in MQTT and Influx aspect // sendState() refined in MQTT and Influx aspect
...@@ -303,7 +302,8 @@ aspect ItemHandling { ...@@ -303,7 +302,8 @@ aspect ItemHandling {
logger.catching(e); logger.catching(e);
} }
} }
if (hasItemObserver() && this.getStateData().checkStateTransitionTime()) { if (hasItemObserver() && this.getStateData().checkStateProcessingTime()) {
this.getStateData().afterStateChangeProcessed();
getItemObserver().apply(); getItemObserver().apply();
} }
} }
......
aspect StateData { aspect StateData {
public void StateData.afterStateChangeApplied() { public void StateData.afterStateChangeProcessed() {
this.setLastChangeDate(Instant.now()); this.setLastChangeDate(Instant.now());
} }
public boolean StateData.checkStateTransitionTime() { public boolean StateData.checkStateProcessingTime() {
if (this.getItemPerformance()==null) { if (this.getItemPerformance()==null) {
return true; return true;
} }
double frequency = this.getItemPerformance().getEventProcessingFrequency(); double frequency = this.getItemPerformance().getEventProcessingFrequency();
Instant lastStateChange = this.getLastChangeDate(); Instant lastStateChange = this.getLastChangeDate();
if (lastStateChange==null) {
return true;
}
return lastStateChange.toEpochMilli() + (1/frequency)*1000 < Instant.now().toEpochMilli(); return lastStateChange.toEpochMilli() + (1/frequency)*1000 < Instant.now().toEpochMilli();
......
...@@ -2,6 +2,8 @@ package de.tudresden.inf.st.eraser.util; ...@@ -2,6 +2,8 @@ package de.tudresden.inf.st.eraser.util;
import de.tudresden.inf.st.eraser.jastadd.model.*; import de.tudresden.inf.st.eraser.jastadd.model.*;
import java.util.ArrayList;
/** /**
* Helper class to create models used in tests. * Helper class to create models used in tests.
* *
...@@ -35,6 +37,13 @@ public class TestUtils { ...@@ -35,6 +37,13 @@ public class TestUtils {
return ModelAndItem.of(root.getSmartHomeEntityModel(), item); return ModelAndItem.of(root.getSmartHomeEntityModel(), item);
} }
public static SmartHomeEntityModel createModelWithGroup() {
Root root = Root.createEmptyRoot();
ParserUtils.createUnknownGroup(root.getSmartHomeEntityModel(),new ArrayList<>());
return root.getSmartHomeEntityModel();
}
public static NumberItem addItemTo(SmartHomeEntityModel model, double initialValue) { public static NumberItem addItemTo(SmartHomeEntityModel model, double initialValue) {
return addItemTo(model, initialValue, false); return addItemTo(model, initialValue, false);
} }
......
...@@ -15,6 +15,7 @@ import java.util.concurrent.TimeUnit; ...@@ -15,6 +15,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import static de.tudresden.inf.st.eraser.util.TestUtils.getDefaultGroup;
import static org.assertj.core.api.Assertions.fail; import static org.assertj.core.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
...@@ -245,6 +246,11 @@ public class RulesTest { ...@@ -245,6 +246,11 @@ public class RulesTest {
assertEquals(2, counter.get(item), "Change of item to 7 should not trigger the rule, check2 violated"); assertEquals(2, counter.get(item), "Change of item to 7 should not trigger the rule, check2 violated");
} }
private static void addItemToModel(SmartHomeEntityModel model, Item item) {
getDefaultGroup(model).addItem(item);
}
@Test @Test
public void testTwoActions() { public void testTwoActions() {
TestUtils.ModelAndItem modelAndItem = createModelAndItem(2); TestUtils.ModelAndItem modelAndItem = createModelAndItem(2);
...@@ -662,6 +668,35 @@ public class RulesTest { ...@@ -662,6 +668,35 @@ public class RulesTest {
assertEquals(2, counter.get(null), "Rule was not executed two times"); assertEquals(2, counter.get(null), "Rule was not executed two times");
} }
@Test
public void testItemPerformance() {
TestUtils.ModelAndItem mai = createModelAndItem(0);
NumberItem numberItem = mai.item;
ItemPerformance itemPerformance = new ItemPerformance();
itemPerformance.setEventProcessingFrequency(10);
numberItem.getStateData().setItemPerformance(itemPerformance);
Rule rule = new Rule();
CountingAction counter = new CountingAction();
rule.addAction(counter);
rule.activateFor(numberItem);
numberItem.setState(1);
numberItem.setState(2);
assertEquals(1, counter.get(numberItem), "Action was triggered although ItemPerformance too small");
counter.reset();
waitMillis(100);
numberItem.setState(3);
assertEquals(1, counter.get(numberItem), "Action wasn't triggered although frequency ItemPerformance is small enough");
counter.reset();
numberItem.setState(4);
numberItem.setState(5);
assertEquals(0, counter.get(numberItem), "Action was triggered although ItemPerformance too small");
counter.reset();
}
private static void waitMillis(int millis) { private static void waitMillis(int millis) {
try { try {
Thread.sleep(millis); Thread.sleep(millis);
...@@ -673,7 +708,7 @@ public class RulesTest { ...@@ -673,7 +708,7 @@ public class RulesTest {
private StringItem addStringItem(SmartHomeEntityModel model, String initialValue) { private StringItem addStringItem(SmartHomeEntityModel model, String initialValue) {
StringItem item = new StringItem(); StringItem item = new StringItem();
Group group = TestUtils.getDefaultGroup(model); Group group = getDefaultGroup(model);
item.setID("item" + group.getNumItem()); item.setID("item" + group.getNumItem());
item.setState(initialValue, false); item.setState(initialValue, false);
group.addItem(item); group.addItem(item);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment