Skip to content

armgit / core

Public Imported from GitHub
0

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

Fix race condition in ChirpStack and TTS tests (#2496)

rainerhitz7b4f7810a8fc
diff --git a/test/src/test/groovy/org/openremote/test/protocol/lorawan/chirpstack/ChirpStackTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/lorawan/chirpstack/ChirpStackTest.groovy
index b48f19f01..c321873a6 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/lorawan/chirpstack/ChirpStackTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/lorawan/chirpstack/ChirpStackTest.groovy
@@ -37,6 +37,9 @@ import io.grpc.stub.StreamObserver
import io.grpc.Status
import io.moquette.broker.Server
import io.moquette.broker.config.MemoryConfig
+import io.moquette.interception.AbstractInterceptHandler
+import io.moquette.interception.messages.InterceptSubscribeMessage
+import io.moquette.interception.messages.InterceptUnsubscribeMessage
import org.openremote.agent.protocol.lorawan.chirpstack.ChirpStackAgent
import org.openremote.agent.protocol.lorawan.chirpstack.ChirpStackProtocol
import org.openremote.manager.agent.AgentService
@@ -55,7 +58,10 @@ import spock.lang.Specification
import spock.util.concurrent.PollingConditions
import java.util.concurrent.CopyOnWriteArrayList
+import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.TimeUnit
+import java.util.logging.Level
+import java.util.logging.Logger
import static org.openremote.agent.protocol.lorawan.LoRaWANConstants.ATTRIBUTE_NAME_DEV_EUI
import static org.openremote.container.security.IdentityProvider.OR_ADMIN_PASSWORD
@@ -89,6 +95,9 @@ class ChirpStackTest extends Specification implements ManagerContainerTrait {
@Shared
Server mqttBroker
+ @Shared
+ SubscriptionTracker subTracker
+
@Shared
int grpcPort
@@ -104,8 +113,9 @@ class ChirpStackTest extends Specification implements ManagerContainerTrait {
props.setProperty('port', mqttBrokerPort.toString())
props.setProperty('persistence_enabled', 'false')
def config = new MemoryConfig(props)
+ subTracker = new SubscriptionTracker()
mqttBroker = new Server()
- mqttBroker.startServer(config)
+ mqttBroker.startServer(config, [subTracker])
grpcPort = findEphemeralPort()
grpcServer = new ChirpStackGrpcServer(grpcPort)
@@ -126,6 +136,10 @@ class ChirpStackTest extends Specification implements ManagerContainerTrait {
grpcServer?.stop()
}
+ def setup() {
+ subTracker?.subscriptions.clear()
+ }
+
def "ChirpStack CSV Import Test"() {
given: "expected conditions"
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
@@ -191,6 +205,11 @@ class ChirpStackTest extends Specification implements ManagerContainerTrait {
assert asset2 != null
}
+ and: "MQTT wildcard subscription should have been registered"
+ conditions.eventually {
+ assert subTracker.containsTopic("application/${APPLICATION_ID}/device/+/event/up")
+ }
+
when: "the device sends a LoRaWAN uplink message"
def json = [
fPort: UPLINK_PORT,
@@ -218,7 +237,9 @@ class ChirpStackTest extends Specification implements ManagerContainerTrait {
.topicFilter("application/${APPLICATION_ID}/device/${DEV_EUI_1}/command/down")
.callback { Mqtt3Publish publish ->
downlinkMessages.add(new String(publish.payloadAsBytes))
- }.send()
+ }
+ .send()
+ .get(5, TimeUnit.SECONDS)
assetProcessingService.sendAttributeEvent(new AttributeEvent(asset1.id, SWITCH, true))
then: "a LoRaWAN downlink message should have been published"
@@ -294,6 +315,11 @@ class ChirpStackTest extends Specification implements ManagerContainerTrait {
assert asset2 != null
}
+ and: "MQTT wildcard subscription should have been registered"
+ conditions.eventually {
+ assert subTracker.containsTopic("application/${APPLICATION_ID}/device/+/event/up")
+ }
+
when: "the device sends a LoRaWAN uplink message"
def json = [
fPort: UPLINK_PORT,
@@ -321,7 +347,9 @@ class ChirpStackTest extends Specification implements ManagerContainerTrait {
.topicFilter("application/${APPLICATION_ID}/device/${DEV_EUI_1}/command/down")
.callback { Mqtt3Publish publish ->
downlinkMessages.add(new String(publish.payloadAsBytes))
- }.send()
+ }
+ .send()
+ .get(5, TimeUnit.SECONDS)
assetProcessingService.sendAttributeEvent(new AttributeEvent(asset1.id, SWITCH, true))
then: "a LoRaWAN downlink message should have been published"
@@ -408,4 +436,31 @@ class ChirpStackTest extends Specification implements ManagerContainerTrait {
}
}
}
+
+ static class SubscriptionTracker extends AbstractInterceptHandler {
+ static final Logger LOG = Logger.getLogger(SubscriptionTracker.class.getName())
+ final Set<String> subscriptions = new CopyOnWriteArraySet<>()
+
+ @Override
+ String getID() { "SubscriptionTracker" }
+
+ @Override
+ void onSubscribe(InterceptSubscribeMessage msg) {
+ subscriptions.add(msg.getTopicFilter())
+ }
+
+ @Override
+ void onUnsubscribe(InterceptUnsubscribeMessage msg) {
+ subscriptions.remove(msg.getTopicFilter())
+ }
+
+ @Override
+ void onSessionLoopError(Throwable throwable) {
+ LOG.log(Level.SEVERE, "MQTT session loop error in SubscriptionTracker", throwable)
+ }
+
+ boolean containsTopic(String topic) {
+ return subscriptions.contains(topic)
+ }
+ }
}
diff --git a/test/src/test/groovy/org/openremote/test/protocol/lorawan/tts/TheThingsStackTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/lorawan/tts/TheThingsStackTest.groovy
index bc9d6d87e..b6a328938 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/lorawan/tts/TheThingsStackTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/lorawan/tts/TheThingsStackTest.groovy
@@ -34,6 +34,9 @@ import io.grpc.StatusRuntimeException
import io.grpc.stub.StreamObserver
import io.moquette.broker.Server
import io.moquette.broker.config.MemoryConfig
+import io.moquette.interception.AbstractInterceptHandler
+import io.moquette.interception.messages.InterceptSubscribeMessage
+import io.moquette.interception.messages.InterceptUnsubscribeMessage
import org.openremote.agent.protocol.lorawan.tts.TheThingsStackAgent
import org.openremote.agent.protocol.lorawan.tts.TheThingsStackProtocol
import org.openremote.manager.agent.AgentService
@@ -53,7 +56,10 @@ import spock.util.concurrent.PollingConditions
import ttn.lorawan.v3.*
import java.util.concurrent.CopyOnWriteArrayList
+import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.TimeUnit
+import java.util.logging.Level
+import java.util.logging.Logger
import static org.openremote.agent.protocol.lorawan.LoRaWANConstants.ATTRIBUTE_NAME_DEV_EUI
import static org.openremote.agent.protocol.lorawan.tts.TheThingsStackProtocol.THE_THINGS_STACK_ASSET_TYPE_TAG
@@ -94,6 +100,9 @@ class TheThingsStackTest extends Specification implements ManagerContainerTrait
@Shared
Server mqttBroker
+ @Shared
+ SubscriptionTracker subTracker
+
@Shared
int grpcPort
@@ -109,8 +118,9 @@ class TheThingsStackTest extends Specification implements ManagerContainerTrait
props.setProperty('port', mqttBrokerPort.toString())
props.setProperty('persistence_enabled', 'false')
def config = new MemoryConfig(props)
+ subTracker = new SubscriptionTracker()
mqttBroker = new Server()
- mqttBroker.startServer(config)
+ mqttBroker.startServer(config, [subTracker])
grpcPort = findEphemeralPort()
grpcServer = new TheThingsStackGrpcServer(grpcPort)
@@ -131,6 +141,10 @@ class TheThingsStackTest extends Specification implements ManagerContainerTrait
grpcServer?.stop()
}
+ def setup() {
+ subTracker?.subscriptions.clear()
+ }
+
def cleanup() {
grpcServer?.shutdownEventStream()
}
@@ -214,6 +228,11 @@ class TheThingsStackTest extends Specification implements ManagerContainerTrait
assert asset2 != null
}
+ and: "MQTT wildcard subscription should have been registered"
+ conditions.eventually {
+ assert subTracker.containsTopic("v3/${APPLICATION_ID}@${TENANT_ID}/devices/+/up")
+ }
+
when: "the device sends a LoRaWAN uplink message"
def json = [
uplink_message: [
@@ -243,7 +262,9 @@ class TheThingsStackTest extends Specification implements ManagerContainerTrait
.topicFilter("v3/${APPLICATION_ID}@${TENANT_ID}/devices/${DEVICE_ID_1}/down/push")
.callback { Mqtt3Publish publish ->
downlinkMessages.add(new String(publish.payloadAsBytes))
- }.send()
+ }
+ .send()
+ .get(5, TimeUnit.SECONDS)
assetProcessingService.sendAttributeEvent(new AttributeEvent(asset1.id, SWITCH, true))
then: "a LoRaWAN downlink message should have been published"
@@ -321,6 +342,11 @@ class TheThingsStackTest extends Specification implements ManagerContainerTrait
assert asset != null
}
+ and: "MQTT wildcard subscription should have been registered"
+ conditions.eventually {
+ assert subTracker.containsTopic("v3/${APPLICATION_ID}@${TENANT_ID}/devices/+/up")
+ }
+
when: "a device sends a LoRaWAN uplink message"
def json = [
uplink_message: [
@@ -350,7 +376,9 @@ class TheThingsStackTest extends Specification implements ManagerContainerTrait
.topicFilter("v3/${APPLICATION_ID}@${TENANT_ID}/devices/${DEVICE_ID_1}/down/push")
.callback { Mqtt3Publish publish ->
downlinkMessages.add(new String(publish.payloadAsBytes))
- }.send()
+ }
+ .send()
+ .get(5, TimeUnit.SECONDS)
assetProcessingService.sendAttributeEvent(new AttributeEvent(asset1.id, SWITCH, true))
then: "a LoRaWAN downlink message should have been published"
@@ -615,4 +643,31 @@ class TheThingsStackTest extends Specification implements ManagerContainerTrait
.build()
}
}
+
+ static class SubscriptionTracker extends AbstractInterceptHandler {
+ static final Logger LOG = Logger.getLogger(SubscriptionTracker.class.getName())
+ final Set<String> subscriptions = new CopyOnWriteArraySet<>()
+
+ @Override
+ String getID() { "SubscriptionTracker" }
+
+ @Override
+ void onSubscribe(InterceptSubscribeMessage msg) {
+ subscriptions.add(msg.getTopicFilter())
+ }
+
+ @Override
+ void onUnsubscribe(InterceptUnsubscribeMessage msg) {
+ subscriptions.remove(msg.getTopicFilter())
+ }
+
+ @Override
+ void onSessionLoopError(Throwable throwable) {
+ LOG.log(Level.SEVERE, "MQTT session loop error in SubscriptionTracker", throwable)
+ }
+
+ boolean containsTopic(String topic) {
+ return subscriptions.contains(topic)
+ }
+ }
}