Skip to content
Snippets Groups Projects
Commit 519cd508 authored by René Schöne's avatar René Schöne
Browse files

WIP: working on correct connect and disconnect

- move IntList to TestUtils
- make resolve-element for wildcard connections a method (instead of an attribute). would never be a cache-hit anyway
- added Lock in MqttHandler to avoid simultaneous access/modification of AST
- extend base.clean to remove generated aspects as well
parent b1301aee
No related branches found
No related tags found
1 merge request!12Ensure correct connect and disconnect functionality
Pipeline #10701 passed
This commit is part of merge request !12. Comments created here will be created in the context of that merge request.
Showing
with 88 additions and 106 deletions
......@@ -108,6 +108,14 @@ task relast(type: JavaExec) {
'./src/gen/jastadd/RagConnectResolverStubs.jrag')
}
clean {
delete "src/gen/jastadd/Coverage.jrag"
delete "src/gen/jastadd/RagConnect.ast"
delete "src/gen/jastadd/RagConnect.jadd"
delete "src/gen/jastadd/RagConnectRefResolver.jadd"
delete "src/gen/jastadd/RagConnectResolverStubs.jrag"
}
jastadd {
configureModuleBuild()
modules {
......
......@@ -165,7 +165,7 @@ aspect AttributesForMustache {
eq MTypeReceiveDefinition.updateMethod() = null;
eq MTypeReceiveDefinition.writeMethod() = null;
syn String MTypeReceiveDefinition.resolveInListAttributeName() = "resolve" + entityName() + "InList";
syn String MTypeReceiveDefinition.resolveInListMethodName() = "_ragconnect_resolve" + entityName() + "InList";
syn String MTypeReceiveDefinition.idTokenName() = endpointDef().idTokenName();
// MTypeSendDefinition
......
......@@ -99,6 +99,7 @@ public class MqttHandler {
private org.fusesource.mqtt.client.CallbackConnection connection;
/** Whether we are connected yet */
private final java.util.concurrent.CountDownLatch readyLatch;
private final java.util.concurrent.locks.Lock astLock;
private boolean sendWelcomeMessage = true;
private org.fusesource.mqtt.client.QoS qos;
/** Dispatch knowledge */
......@@ -116,6 +117,7 @@ public class MqttHandler {
this.wildcardCallbacks = new java.util.ArrayList<>();
this.readyLatch = new java.util.concurrent.CountDownLatch(1);
this.qos = org.fusesource.mqtt.client.QoS.AT_LEAST_ONCE;
this.astLock = new java.util.concurrent.locks.ReentrantLock();
}
public MqttHandler dontSendWelcomeMessage() {
......@@ -178,9 +180,12 @@ public class MqttHandler {
byte[] message = body.toByteArray();
for (java.util.function.BiConsumer<String, byte[]> callback : callbackList) {
try {
astLock.lock();
callback.accept(topicString, message);
} catch (Exception e) {
logger.catching(e);
} finally {
astLock.unlock();
}
}
}
......@@ -479,6 +484,8 @@ public class MqttHandler {
}
public void publish(String topic, byte[] bytes, org.fusesource.mqtt.client.QoS qos, boolean retain) {
try {
astLock.lock();
connection.getDispatchQueue().execute(() -> {
connection.publish(topic, bytes, qos, retain, new org.fusesource.mqtt.client.Callback<>() {
@Override
......@@ -492,6 +499,9 @@ public class MqttHandler {
}
});
});
} finally {
astLock.unlock();
}
}
}
}
{{#typeIsList}}
{{^UseList}}
/* first try with resolve to type
syn {{typeName}} {{parentTypeName}}.{{resolveInListAttributeName}}(String topic) {
for ({{typeName}} element : get{{entityName}}()) {
if (element.get{{idTokenName}}().equals(topic)) {
return element;
}
}
return null;
}
*/
syn int {{parentTypeName}}.{{resolveInListAttributeName}}(String topic) {
private int {{parentTypeName}}.{{resolveInListMethodName}}(String topic) {
for (int index = 0; index < getNum{{entityName}}(); index++) {
if (get{{entityName}}(index).get{{idTokenName}}().equals(topic)) {
return index;
......@@ -85,7 +75,7 @@ public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterNam
System.out.println("[Receive] " + {{connectParameterName}} + " (" + topic + ") -> {{entityName}} = " + {{lastResult}});
{{/loggingEnabledForReads}}
{{lastResult}}.set{{idTokenName}}(topic);
int resolvedIndex = {{resolveInListAttributeName}}(topic);
int resolvedIndex = {{resolveInListMethodName}}(topic);
if (resolvedIndex == -1) {
add{{entityName}}({{lastResult}});
} else {
......
......@@ -14,8 +14,12 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static java.lang.Math.abs;
import static java.util.Collections.addAll;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Lists.newArrayList;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
......@@ -136,6 +140,25 @@ public class TestUtils {
TimeUnit.MILLISECONDS.sleep(1500);
}
public static class IntList {
private final List<Integer> integers = newArrayList();
public IntList(Integer... values) {
addAll(integers, values);
}
public List<Integer> toList() {
return integers;
}
public List<Integer> toAbsList() {
return integers.stream().map(Math::abs).collect(Collectors.toList());
}
public static IntList list(Integer... values) {
return new IntList(values);
}
}
@SuppressWarnings({"unused", "rawtypes"})
public static class DefaultMappings {
static class ReadNode extends defaultOnlyRead.ast.ASTNode {
......
......@@ -2,6 +2,7 @@ package org.jastadd.ragconnect.tests.list;
import org.jastadd.ragconnect.tests.AbstractMqttTest;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.TestUtils.IntList;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
......@@ -10,9 +11,7 @@ import java.nio.file.Paths;
import java.util.List;
import java.util.function.Function;
import static java.util.Collections.addAll;
import static org.assertj.core.util.Lists.newArrayList;
import static org.jastadd.ragconnect.tests.list.AbstractListTest.IntList.list;
import static org.jastadd.ragconnect.tests.TestUtils.IntList.list;
import static org.jastadd.ragconnect.tests.TestUtils.testJaddContainReferenceToJackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
......@@ -181,19 +180,4 @@ public abstract class AbstractListTest extends AbstractMqttTest {
int numberOfElements = 0;
}
protected static class IntList {
private final List<Integer> integers = newArrayList();
public IntList(Integer... values) {
addAll(integers, values);
}
public List<Integer> toList() {
return integers;
}
public static IntList list(Integer... values) {
return new IntList(values);
}
}
}
......@@ -2,6 +2,7 @@ package org.jastadd.ragconnect.tests.singleList;
import org.jastadd.ragconnect.tests.AbstractMqttTest;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.TestUtils.IntList;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import singleList.ast.MqttHandler;
......@@ -16,11 +17,9 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import static java.util.Collections.addAll;
import static org.assertj.core.util.Lists.newArrayList;
import static org.jastadd.ragconnect.tests.TestUtils.IntList.list;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.TestUtils.testJaddContainReferenceToJackson;
import static org.jastadd.ragconnect.tests.singleList.AbstractSingleListTest.IntList.list;
import static org.junit.jupiter.api.Assertions.*;
/**
......@@ -30,7 +29,6 @@ import static org.junit.jupiter.api.Assertions.*;
*/
@Tag("List")
@Tag("SingleList")
@Tag("New")
public abstract class AbstractSingleListTest extends AbstractMqttTest {
public interface TestWrapperJastAddList<T> extends Iterable<T> {
......@@ -341,19 +339,4 @@ public abstract class AbstractSingleListTest extends AbstractMqttTest {
int numberOfElements = 0;
}
protected static class IntList {
private final List<Integer> integers = newArrayList();
public IntList(Integer... values) {
addAll(integers, values);
}
public List<Integer> toList() {
return integers;
}
public static IntList list(Integer... values) {
return new IntList(values);
}
}
}
......@@ -11,7 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test case "list incremental".
* Test case "single list incremental".
*
* @author rschoene - Initial contribution
*/
......
......@@ -10,7 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test case "list manual".
* Test case "single list manual".
*
* @author rschoene - Initial contribution
*/
......
package org.jastadd.ragconnect.tests.singleListVariant;
import org.assertj.core.api.Assertions;
import org.jastadd.ragconnect.tests.AbstractMqttTest;
import org.jastadd.ragconnect.tests.TestUtils;
import org.jastadd.ragconnect.tests.TestUtils.IntList;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
......@@ -11,16 +13,14 @@ import java.util.List;
import java.util.function.BiConsumer;
import static java.lang.Math.abs;
import static java.util.Collections.addAll;
import static org.assertj.core.util.Lists.newArrayList;
import static org.jastadd.ragconnect.tests.TestUtils.IntList.list;
import static org.jastadd.ragconnect.tests.TestUtils.mqttUri;
import static org.jastadd.ragconnect.tests.TestUtils.testJaddContainReferenceToJackson;
import static org.jastadd.ragconnect.tests.singleListVariant.AbstractSingleListVariantTest.IntList.list;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Base class for test cases "singleList manual" and "singleList incremental".
* Base class for test cases "singleList variant manual" and "singleList variant incremental".
*
* @author rschoene - Initial contribution
*/
......@@ -373,31 +373,31 @@ public abstract class AbstractSingleListVariantTest extends AbstractMqttTest {
assertEquals(expectedTransmissions, data.numberOfElements, "transmissions for any element");
// check unnamed
checkList(expectedList.toList(), receiverRoot.getT_EmptyList(), (e, n) -> {});
checkList(expectedList.toList(), receiverRoot.getT_TokenList(),
checkList(expectedList, receiverRoot.getT_EmptyList(), (e, n) -> {});
checkList(expectedList, receiverRoot.getT_TokenList(),
(e, n) -> assertEquals(Integer.toString(abs(e)), n.getValue()));
checkList(expectedList.toList(), receiverRoot.getT_OneChildList(),
checkList(expectedList, receiverRoot.getT_OneChildList(),
(e, n) -> assertEquals(abs(e) + 1, n.getOther().getID()));
checkList(expectedList.toList(), receiverRoot.getT_OneOptList(),
checkList(expectedList, receiverRoot.getT_OneOptList(),
(e, n) -> {
assertEquals(e > 0, n.hasOther());
if (n.hasOther()) {
assertEquals(abs(e) + 1, n.getOther().getID());
}
});
checkList(expectedList.toList(), receiverRoot.getT_OneListList(),
checkList(expectedList, receiverRoot.getT_OneListList(),
(e, n) -> {
assertEquals(e > 0 ? 1 : 0, n.getNumOther());
if (n.getNumOther() > 0) {
assertEquals(abs(e) + 1, n.getOther(0).getID());
}
});
checkList(expectedList.toList(), receiverRoot.getT_TwoChildrenList(),
checkList(expectedList, receiverRoot.getT_TwoChildrenList(),
(e, n) -> {
assertEquals(abs(e) + 1, n.getLeft().getID());
assertEquals(abs(e) + 1, n.getRight().getID());
});
checkList(expectedList.toList(), receiverRoot.getT_OneOfEachList(),
checkList(expectedList, receiverRoot.getT_OneOfEachList(),
(e, n) -> {
assertEquals(abs(e) + 1, n.getFirst().getID());
assertEquals(e > 0, n.hasSecond());
......@@ -410,39 +410,39 @@ public abstract class AbstractSingleListVariantTest extends AbstractMqttTest {
}
assertEquals(Integer.toString(abs(e)), n.getFourth());
});
checkList(expectedList.toList(), receiverRoot.getT_AbstractList(),
checkList(expectedList, receiverRoot.getT_AbstractList(),
(e, n) -> {
assertEquals(Integer.toString(abs(e)), n.getValueAbstract());
assertEquals(Integer.toString(abs(e)), n.getValueSub());
});
// check named
checkList(expectedList.toList(), receiverRoot.getMyEmptyList(), (e, n) -> {});
checkList(expectedList, receiverRoot.getMyEmptyList(), (e, n) -> {});
// check with add
checkList(expectedWithAddList.toList(), receiverRoot.getEmptyWithAddList(), (e, n) -> {});
checkList(expectedWithAddList.toList(), receiverRoot.getTokenWithAddList(),
checkList(expectedWithAddList, receiverRoot.getEmptyWithAddList(), (e, n) -> {});
checkList(expectedWithAddList, receiverRoot.getTokenWithAddList(),
(e, n) -> assertEquals(Integer.toString(abs(e)), n.getValue()));
checkList(expectedWithAddList.toList(), receiverRoot.getOneChildWithAddList(),
checkList(expectedWithAddList, receiverRoot.getOneChildWithAddList(),
(e, n) -> assertEquals(abs(e) + 1, n.getOther().getID()));
checkList(expectedWithAddListForOptAndList.toList(), receiverRoot.getOneOptWithAddList(),
checkList(expectedWithAddListForOptAndList, receiverRoot.getOneOptWithAddList(),
(e, n) -> {
if (n.hasOther()) {
assertEquals(abs(e) + 1, n.getOther().getID());
}
});
checkList(expectedWithAddListForOptAndList.toList(), receiverRoot.getOneListWithAddList(),
checkList(expectedWithAddListForOptAndList, receiverRoot.getOneListWithAddList(),
(e, n) -> {
if (n.getNumOther() > 0) {
assertEquals(abs(e) + 1, n.getOther(0).getID());
}
});
checkList(expectedWithAddList.toList(), receiverRoot.getTwoChildrenWithAddList(),
checkList(expectedWithAddList, receiverRoot.getTwoChildrenWithAddList(),
(e, n) -> {
assertEquals(abs(e) + 1, n.getLeft().getID());
assertEquals(abs(e) + 1, n.getRight().getID());
});
checkList(expectedWithAddListForOptAndList.toList(), receiverRoot.getOneOfEachWithAddList(),
checkList(expectedWithAddListForOptAndList, receiverRoot.getOneOfEachWithAddList(),
(e, n) -> {
assertEquals(abs(e) + 1, n.getFirst().getID());
if (n.hasSecond()) {
......@@ -453,19 +453,19 @@ public abstract class AbstractSingleListVariantTest extends AbstractMqttTest {
}
assertEquals(Integer.toString(abs(e)), n.getFourth());
});
checkList(expectedWithAddList.toList(), receiverRoot.getAbstractWithAddList(),
checkList(expectedWithAddList, receiverRoot.getAbstractWithAddList(),
(e, n) -> {
assertEquals(Integer.toString(abs(e)), n.getValueAbstract());
assertEquals(Integer.toString(abs(e)), n.getValueSub());
});
}
private <T extends TestWrapperNameable> void checkList(List<Integer> expectedList, TestWrapperJastAddList<T> actualList, BiConsumer<Integer, T> additionalTest) {
assertEquals(expectedList.size(), actualList.getNumChild(), "same list size");
private <T extends TestWrapperNameable> void checkList(IntList expectedList, TestWrapperJastAddList<T> actualList, BiConsumer<Integer, T> additionalTest) {
Assertions.assertThat(actualList).extracting("ID").containsExactlyElementsOf(expectedList.toAbsList());
List<Integer> normalExpectedList = expectedList.toList();
int index = 0;
for (T element : actualList) {
assertEquals(abs(expectedList.get(index)), element.getID(), "correct ID for A");
additionalTest.accept(expectedList.get(index), element);
additionalTest.accept(normalExpectedList.get(index), element);
index++;
}
}
......@@ -474,19 +474,4 @@ public abstract class AbstractSingleListVariantTest extends AbstractMqttTest {
int numberOfElements = 0;
}
protected static class IntList {
private final List<Integer> integers = newArrayList();
public IntList(Integer... values) {
addAll(integers, values);
}
public List<Integer> toList() {
return integers;
}
public static IntList list(Integer... values) {
return new IntList(values);
}
}
}
......@@ -11,12 +11,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test case "list incremental".
* Test case "singleList variant incremental".
*
* @author rschoene - Initial contribution
*/
@Tag("Incremental")
@Tag("New")
public class SingleListVariantIncrementalVariantTest extends AbstractSingleListVariantTest {
private Root model;
......
......@@ -10,7 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test case "list manual".
* Test case "singleList variant manual".
*
* @author rschoene - Initial contribution
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment