Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Added PollingConditions to the PIDTest.

Michal Rutkaffa8e9e7ca77
diff --git a/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy b/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy
index afe42b2ec..b3b433850 100644
--- a/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy
+++ b/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy
@@ -4,14 +4,12 @@ import org.kie.api.KieServices
import org.kie.api.io.Resource
import org.openremote.agent.AgentService
import org.openremote.agent.rules.RulesProvider
-import org.openremote.agent.sensor.CustomSensorState
import org.openremote.test.ContainerTrait
-import spock.lang.Ignore
import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
import java.util.stream.Stream
-// @Ignore // TODO: Fix test, use BlockingVariables, PollingConditions, etc.
class PIDTest extends Specification implements ContainerTrait {
def "PID controller basic test"() {
@@ -20,6 +18,7 @@ class PIDTest extends Specification implements ContainerTrait {
def deploymentXml = getClass().getResourceAsStream(
"/org/openremote/test/rules/pid/agent.xml"
)
+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.5, factor: 0.25)
and: "some rules"
def rulesProvider = new RulesProvider() {
@@ -34,7 +33,7 @@ class PIDTest extends Specification implements ContainerTrait {
}
and: "the started server"
- def testCommandBuilder = new TestCommandBuilder();
+ def testCommandBuilder = new TestCommandBuilder()
def agentService = new AgentService(
deploymentXml,
testCommandBuilder,
@@ -51,30 +50,25 @@ class PIDTest extends Specification implements ContainerTrait {
// Send a command linked with the button:
agentService.getContext().getCommands().execute("PID.sp.inc.ON")
- and: "we wait"
- Thread.sleep(2000);
-
then: "set point should be 1.5"
- agentService.getContext().queryValue("GV.PID.Sp") == "1.5"
- agentService.getContext().queryValue("PID.Output") == "1.5000"
+ conditions.eventually {
+ assert agentService.getContext().queryValue("GV.PID.Sp") == "1.5"
+ assert agentService.getContext().queryValue("PID.Output") == "1.5000"
+ }
+
when: "we decrease Sp"
agentService.getContext().getCommands().execute("PID.sp.dec.ON")
- and: "we wait"
- Thread.sleep(2000);
-
then: "the PID output should be 1.0"
- agentService.getContext().queryValue("GV.PID.Sp") == "1.0" // set point
- agentService.getContext().queryValue("PID.Output") == "1.0000" // PID output
+ conditions.eventually {
+ assert agentService.getContext().queryValue("GV.PID.Sp") == "1.0" // set point
+ assert agentService.getContext().queryValue("PID.Output") == "1.0000" // PID output
+ }
// TODO: how can I test if there is no overshot?
// TODO: how can I test if there are no oscillations?
- // Problem: sometimes it passes, sometimes fails...
- // timing is not correct and requires manual tweaking - not desired. Better way would be to wait for desired
- // output (with a timeout).
-
cleanup: "the server should be stopped"
stopContainer(container)
}
diff --git a/agent/src/test/resources/org/openremote/test/rules/pid/PID.drl b/agent/src/test/resources/org/openremote/test/rules/pid/PID.drl
index 5d88a69dc..1bba775cd 100644
--- a/agent/src/test/resources/org/openremote/test/rules/pid/PID.drl
+++ b/agent/src/test/resources/org/openremote/test/rules/pid/PID.drl
@@ -159,7 +159,7 @@ then
insert(pv);
end
-rule "-PID: compute"
+rule "--PID: compute"
timer(int: 0 10) // 10 times faster than the implemented one
when
$pv: PIDVars(active==true)
@@ -193,7 +193,7 @@ then
commands.execute("PID.Se", String.format("%.4f", error) );
commands.execute("PID.Output", String.format("%.4f", output) );
- LOG.fine($pv.toString() + " error: "+String.format("%f", Double.parseDouble($sp.toString() ) - $pv.getOutput()));
+ // LOG.fine($pv.toString() + " error: "+String.format("%f", Double.parseDouble($sp.toString() ) - $pv.getOutput()));
end
// PID end