Commit 47b8af00 authored by René Schöne's avatar René Schöne
Browse files

Extend ExampleTest with counter of intermediate calls.

parent fa38ca9e
Pipeline #7109 passed with stages
in 6 minutes and 8 seconds
aspect GrammarTypes {
public class TestCounter {
public static TestCounter INSTANCE = new TestCounter();
public int numberParseLinkState = 0;
public int numberSerializeRobotConfig = 0;
public int numberLinkStateToIntPosition = 0;
public int numberCreateSpeedMessage = 0;
public int numberInSafetyZone = 0;
public static void reset() {
TestCounter.INSTANCE = new TestCounter();
}
}
public class IntPosition {
private final int x, y, z;
......@@ -53,6 +65,7 @@ aspect GrammarTypes {
eq RobotArm.getEndEffector().containingRobotArm() = this;
syn boolean RobotArm.isInSafetyZone() {
TestCounter.INSTANCE.numberInSafetyZone += 1;
for (Joint joint : getJointList()) {
if (model().getZoneModel().isInSafetyZone(joint.getCurrentPosition())) {
return true;
......
......@@ -8,19 +8,23 @@ RobotArm.AppropriateSpeed canDependOn Joint.CurrentPosition as dependency1 ;
// --- mapping definitions ---
ParseLinkState maps byte[] bytes to panda.Linkstate.PandaLinkState {:
TestCounter.INSTANCE.numberParseLinkState += 1;
return panda.Linkstate.PandaLinkState.parseFrom(bytes);
:}
SerializeRobotConfig maps config.Robotconfig.RobotConfig rc to byte[] {:
TestCounter.INSTANCE.numberSerializeRobotConfig += 1;
return rc.toByteArray();
:}
LinkStateToIntPosition maps panda.Linkstate.PandaLinkState pls to IntPosition {:
TestCounter.INSTANCE.numberLinkStateToIntPosition += 1;
panda.Linkstate.PandaLinkState.Position p = pls.getPos();
return IntPosition.of((int) (10 * p.getPositionX()), (int) (10 * p.getPositionY()), (int) (10 * p.getPositionZ()));
:}
CreateSpeedMessage maps double speed to config.Robotconfig.RobotConfig {:
TestCounter.INSTANCE.numberCreateSpeedMessage += 1;
return config.Robotconfig.RobotConfig.newBuilder()
.setSpeed(speed)
.build();
......
......@@ -4,6 +4,7 @@ import com.google.protobuf.InvalidProtocolBufferException;
import config.Robotconfig.RobotConfig;
import example.ast.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import panda.Linkstate;
......@@ -30,6 +31,11 @@ public class ExampleTest extends AbstractMqttTest {
private MqttUpdater handler;
private ReceiverData data;
@BeforeEach
public void resetTestCounter() {
TestCounter.reset();
}
@AfterEach
public void closeConnections() {
if (handler != null) {
......@@ -52,6 +58,11 @@ public class ExampleTest extends AbstractMqttTest {
// joint is currently within the safety zone, so speed should be low
TestUtils.waitForMqtt();
assertEquals(0, TestCounter.INSTANCE.numberParseLinkState);
assertEquals(0, TestCounter.INSTANCE.numberLinkStateToIntPosition);
assertEquals(1, TestCounter.INSTANCE.numberInSafetyZone);
assertEquals(1, TestCounter.INSTANCE.numberCreateSpeedMessage);
assertEquals(1, TestCounter.INSTANCE.numberSerializeRobotConfig);
assertEquals(1, data.numberOfConfigs);
assertFalse(data.failedLastConversion);
assertEquals(robotArm.speedLow(), data.lastConfig.getSpeed(), TestUtils.DELTA);
......@@ -62,6 +73,11 @@ public class ExampleTest extends AbstractMqttTest {
// still in safety zone, so no update should have been sent
TestUtils.waitForMqtt();
assertEquals(makePosition(2, 2, 2), joint1.getCurrentPosition());
assertEquals(1, TestCounter.INSTANCE.numberParseLinkState);
assertEquals(1, TestCounter.INSTANCE.numberLinkStateToIntPosition);
assertEquals(2, TestCounter.INSTANCE.numberInSafetyZone);
assertEquals(2, TestCounter.INSTANCE.numberCreateSpeedMessage);
assertEquals(2, TestCounter.INSTANCE.numberSerializeRobotConfig);
assertEquals(1, data.numberOfConfigs);
assertFalse(data.failedLastConversion);
assertEquals(robotArm.speedLow(), data.lastConfig.getSpeed(), TestUtils.DELTA);
......@@ -71,16 +87,40 @@ public class ExampleTest extends AbstractMqttTest {
TestUtils.waitForMqtt();
assertEquals(makePosition(3, 4, 5), joint2.getCurrentPosition());
assertEquals(2, TestCounter.INSTANCE.numberParseLinkState);
assertEquals(2, TestCounter.INSTANCE.numberLinkStateToIntPosition);
assertEquals(3, TestCounter.INSTANCE.numberInSafetyZone);
assertEquals(3, TestCounter.INSTANCE.numberCreateSpeedMessage);
assertEquals(3, TestCounter.INSTANCE.numberSerializeRobotConfig);
assertEquals(2, data.numberOfConfigs);
assertFalse(data.failedLastConversion);
assertEquals(robotArm.speedHigh(), data.lastConfig.getSpeed(), TestUtils.DELTA);
// change position of second joint, no change after mapping
sendData(TOPIC_JOINT2, 0.33f, 0.42f, 0.51f);
TestUtils.waitForMqtt();
assertEquals(makePosition(3, 4, 5), joint2.getCurrentPosition());
assertEquals(3, TestCounter.INSTANCE.numberParseLinkState);
assertEquals(3, TestCounter.INSTANCE.numberLinkStateToIntPosition);
assertEquals(3, TestCounter.INSTANCE.numberInSafetyZone);
assertEquals(3, TestCounter.INSTANCE.numberCreateSpeedMessage);
assertEquals(3, TestCounter.INSTANCE.numberSerializeRobotConfig);
assertEquals(2, data.numberOfConfigs);
assertFalse(data.failedLastConversion);
// change position of second joint, still out of the safety zone, no update should be sent
sendData(TOPIC_JOINT2, 1.3f, 2.4f, 3.5f);
TestUtils.waitForMqtt();
assertEquals(makePosition(13, 24, 35), joint2.getCurrentPosition());
assertEquals(4, TestCounter.INSTANCE.numberParseLinkState);
assertEquals(4, TestCounter.INSTANCE.numberLinkStateToIntPosition);
assertEquals(4, TestCounter.INSTANCE.numberInSafetyZone);
assertEquals(4, TestCounter.INSTANCE.numberCreateSpeedMessage);
assertEquals(4, TestCounter.INSTANCE.numberSerializeRobotConfig);
assertEquals(2, data.numberOfConfigs);
assertFalse(data.failedLastConversion);
}
@Test
......@@ -90,14 +130,24 @@ public class ExampleTest extends AbstractMqttTest {
// no value should have been sent
TestUtils.waitForMqtt();
assertEquals(0, TestCounter.INSTANCE.numberParseLinkState);
assertEquals(0, TestCounter.INSTANCE.numberLinkStateToIntPosition);
assertEquals(1, TestCounter.INSTANCE.numberInSafetyZone);
assertEquals(1, TestCounter.INSTANCE.numberCreateSpeedMessage);
assertEquals(1, TestCounter.INSTANCE.numberSerializeRobotConfig);
assertEquals(0, data.numberOfConfigs);
// change position of the first joint out of the safety zone, second still in
sendData(TOPIC_JOINT1, 0.2f, 0.2f, 0.2f);
// still in safety zone, first update should have been sent
// still in safety zone, hence, no value should have been sent
TestUtils.waitForMqtt();
assertEquals(makePosition(2, 2, 2), joint1.getCurrentPosition());
assertEquals(1, TestCounter.INSTANCE.numberParseLinkState);
assertEquals(1, TestCounter.INSTANCE.numberLinkStateToIntPosition);
assertEquals(2, TestCounter.INSTANCE.numberInSafetyZone);
assertEquals(2, TestCounter.INSTANCE.numberCreateSpeedMessage);
assertEquals(2, TestCounter.INSTANCE.numberSerializeRobotConfig);
assertEquals(0, data.numberOfConfigs);
// change position of second joint also out of the safety zone, now speed must be high
......@@ -105,16 +155,50 @@ public class ExampleTest extends AbstractMqttTest {
TestUtils.waitForMqtt();
assertEquals(makePosition(3, 4, 5), joint2.getCurrentPosition());
assertEquals(2, TestCounter.INSTANCE.numberParseLinkState);
assertEquals(2, TestCounter.INSTANCE.numberLinkStateToIntPosition);
assertEquals(3, TestCounter.INSTANCE.numberInSafetyZone);
assertEquals(3, TestCounter.INSTANCE.numberCreateSpeedMessage);
assertEquals(3, TestCounter.INSTANCE.numberSerializeRobotConfig);
assertEquals(1, data.numberOfConfigs);
assertFalse(data.failedLastConversion);
assertEquals(robotArm.speedHigh(), data.lastConfig.getSpeed(), TestUtils.DELTA);
// change position of second joint, no change after mapping
sendData(TOPIC_JOINT2, 0.33f, 0.42f, 0.51f);
TestUtils.waitForMqtt();
assertEquals(makePosition(3, 4, 5), joint2.getCurrentPosition());
assertEquals(3, TestCounter.INSTANCE.numberParseLinkState);
assertEquals(3, TestCounter.INSTANCE.numberLinkStateToIntPosition);
assertEquals(3, TestCounter.INSTANCE.numberInSafetyZone);
assertEquals(3, TestCounter.INSTANCE.numberCreateSpeedMessage);
assertEquals(3, TestCounter.INSTANCE.numberSerializeRobotConfig);
assertEquals(1, data.numberOfConfigs);
assertFalse(data.failedLastConversion);
// change position of second joint, still out of the safety zone, no update should be sent
sendData(TOPIC_JOINT2, 1.3f, 2.4f, 3.5f);
TestUtils.waitForMqtt();
assertEquals(makePosition(13, 24, 35), joint2.getCurrentPosition());
assertEquals(4, TestCounter.INSTANCE.numberParseLinkState);
assertEquals(4, TestCounter.INSTANCE.numberLinkStateToIntPosition);
assertEquals(4, TestCounter.INSTANCE.numberInSafetyZone);
assertEquals(4, TestCounter.INSTANCE.numberCreateSpeedMessage);
assertEquals(4, TestCounter.INSTANCE.numberSerializeRobotConfig);
assertEquals(1, data.numberOfConfigs);
assertFalse(data.failedLastConversion);
}
@Test
public void testFailedConversion() throws IOException {
createModel();
setupReceiverAndConnect(false);
handler.publish(TOPIC_JOINT1, "not-a-pandaLinkState".getBytes());
assertEquals(0, data.numberOfConfigs);
assertTrue(data.failedLastConversion);
}
private void sendData(String topic, float x, float y, float z) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment