Skip to content

armgit / core

Public Imported from GitHub
0

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

Fix unstable integration tests (#3111)

Koen25d866199476
diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
index cbcb2b761..f80ce92f0 100644
--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
@@ -1,5 +1,6 @@
package org.openremote.test.mqtt
+import com.google.common.cache.CacheBuilder
import com.hivemq.client.internal.mqtt.mqtt3.Mqtt3AsyncClientView
import com.hivemq.client.internal.mqtt.mqtt3.Mqtt3ClientConfigView
import com.hivemq.client.mqtt.MqttClientConfig
@@ -32,6 +33,7 @@ import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
+import java.time.Duration
import java.util.concurrent.CopyOnWriteArrayList
import java.util.function.Consumer
@@ -127,8 +129,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
- assert subFailures.size() == 1
- assert subFailures[0] == topic
+ // A client re-subscribes its retained consumers whenever it reconnects, so failures can be reported more
+ // than once and the total count is not stable
+ assert subFailures.contains(topic)
assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
@@ -140,8 +143,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
- assert subFailures.size() == 2
- assert subFailures[1] == topic
+ assert subFailures.contains(topic)
assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
@@ -154,8 +156,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
- assert subFailures.size() == 3
- assert subFailures[2] == topic
+ assert subFailures.contains(topic)
assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 1
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
@@ -533,6 +534,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
receivedEvents.clear()
and: "the new client gets abruptly disconnected"
+ widenDisconnectedConnectionWindow(mqttBrokerService)
def existingConnection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
// ((NioSocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)device1Client.client).clientConfig).delegate).connectionConfig.get()).channel).config().setOption(ChannelOption.SO_LINGER, 0I)
((SocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)newClient.client).clientConfig).delegate).connectionConfig.get()).channel).close()
@@ -544,7 +546,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
and: "The last will message should have updated the value of the attribute and the first client should have received the event"
- new PollingConditions(initialDelay: 1, timeout: 10, delay: 1).eventually {
+ // The client reconnecting does not mean the broker has processed the closed session yet, and publishing the
+ // last will of that session is what triggers the update, so allow well beyond the reconnect for it
+ new PollingConditions(initialDelay: 1, timeout: 30, delay: 1).eventually {
assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("motionSensor").get().value.orElse(0) == 1000d
assert receivedEvents.size() == 1
assert receivedEvents.get(0) instanceof AttributeEvent
@@ -617,8 +621,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "the subscription should fail but the consumer should still exist (as setRemoveConsumersOnSubscriptionFailure=false)"
conditions.eventually {
- assert subFailures.size() == 4
- assert subFailures[3] == topic
+ assert subFailures.contains(topic)
assert newClient.topicConsumerMap.get(topic) != null
assert newClient.topicConsumerMap.get(topic).consumers.size() == 1
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
@@ -683,8 +686,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "no subscription should exist"
conditions.eventually {
- assert subFailures.size() == 6
- assert subFailures[5] == topic
+ assert subFailures.contains(topic)
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
}
@@ -866,4 +868,15 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
newClient.disconnect()
}
}
+
+ /**
+ * A last will publish is only accepted while the broker can still resolve the closed connection, which it keeps for
+ * 3s, so a publish consumer that is held up for longer drops the will and no amount of polling recovers it.
+ */
+ private static void widenDisconnectedConnectionWindow(MQTTBrokerService mqttBrokerService) {
+ mqttBrokerService.@disconnectedConnectionCache = CacheBuilder.newBuilder()
+ .maximumSize(10000)
+ .expireAfterWrite(Duration.ofSeconds(60))
+ .build()
+ }
}
diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
index 36712045c..f13d0ee79 100644
--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
@@ -838,6 +838,13 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
then: "the admin console should have been created"
adminConsole.id != null
+ and: "the push notification handler should have picked up the console token"
+ // The token map is updated from the asset persistence event, and sending to a console that is not in it yet
+ // fails the whole notification request
+ conditions.eventually {
+ assert pushNotificationHandler.consoleFCMTokenMap.containsKey(adminConsole.id)
+ }
+
when: "the admin user sends a push notification to the entire MASTER realm (which is only himself)"
notification.targets = [new Notification.Target(Notification.TargetType.REALM, MASTER_REALM)]
adminNotificationResource.sendNotification(null, notification)
@@ -914,8 +921,11 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
["manager"] as String[])
def testuser1Console = testuser1ConsoleResource.register(null, testuser1ConsoleRegistration)
- and: "the console is created"
- testuser1Console.id != null
+ and: "the console is created and its token picked up by the push notification handler"
+ conditions.eventually {
+ assert testuser1Console.id != null
+ assert pushNotificationHandler.consoleFCMTokenMap.containsKey(testuser1Console.id)
+ }
and: "the same notification is sent, now to both users"
adminNotificationResource.sendNotification(null, notification)
diff --git a/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy
index cdf5dec7e..ad58cc306 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy
@@ -209,7 +209,9 @@ class HttpServerProtocolTest extends Specification implements ManagerContainerTr
assetStorageService.delete([agent.id])
then: "the associated protocol instance should be un-deployed"
- conditions.eventually {
+ // Un-deploying takes the agent lock and tears down a servlet deployment, so it can be held up by other agents
+ // being deployed at the same time
+ new PollingConditions(timeout: 30, initialDelay: 1).eventually {
assert agentService.getProtocolInstance(agent.id) == null
assert Servlets.defaultContainer().getDeployment(AbstractHTTPServerProtocol.getDeploymentName(TestHTTPServerProtocol.class, agent.id)) == null
}
diff --git a/test/src/test/groovy/org/openremote/test/protocol/openweathermap/OpenWeatherMapProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/openweathermap/OpenWeatherMapProtocolTest.groovy
index b480e6abd..b966d6feb 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/openweathermap/OpenWeatherMapProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/openweathermap/OpenWeatherMapProtocolTest.groovy
@@ -263,6 +263,9 @@ class OpenWeatherMapProtocolTest extends Specification implements ManagerContain
when: "a weather asset is provisioned"
def protocol = (OpenWeatherMapProtocol) agentService.getProtocolInstance(agent.id)
+ // Attributes are linked asynchronously and only linked attributes are updated, so linking must be awaited
+ // before each update is triggered
+ def linkedAttributeCount = { assetId -> protocol.getLinkedAttributes().keySet().count { it.id == assetId } }
def weatherAsset = protocol.provisionWeatherAsset()
then: "the weather asset should be provisioned"
@@ -287,6 +290,7 @@ class OpenWeatherMapProtocolTest extends Specification implements ManagerContain
assert weatherAsset.getAttribute(WeatherAsset.PROBABILITY_OF_PRECIPITATION).get().hasMeta(AGENT_LINK)
assert weatherAsset.getAttribute(WeatherAsset.RAINFALL).get().hasMeta(AGENT_LINK)
assert weatherAsset.getAttribute(WeatherAsset.UV_INDEX).get().hasMeta(AGENT_LINK)
+ assert linkedAttributeCount(weatherAsset.id) == 10
}
@@ -320,7 +324,10 @@ class OpenWeatherMapProtocolTest extends Specification implements ManagerContain
assert weatherAsset.getLocation().map{it.y}.orElse(null) == 52.3676d
}
- when: "a weather update is triggered"
+ when: "a weather update is triggered once the attributes are linked again after the merge"
+ conditions.eventually {
+ assert linkedAttributeCount(weatherAsset.id) == 10
+ }
protocol.updateAllLinkedAttributes()
then: "the weather data should be updated with current values according to the asset's location"
@@ -368,6 +375,7 @@ class OpenWeatherMapProtocolTest extends Specification implements ManagerContain
assert weatherAsset2.parentId == agent.id
assert weatherAsset2.getLocation().map{it.x}.orElse(null) == -0.1276d
assert weatherAsset2.getLocation().map{it.y}.orElse(null) == 51.5072d
+ assert linkedAttributeCount(weatherAsset2.id) == 10
}
when: "a weather update is triggered"
diff --git a/test/src/test/groovy/org/openremote/test/protocol/simulator/SimulatorProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/simulator/SimulatorProtocolTest.groovy
index 032e0c219..b37312f22 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/simulator/SimulatorProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/simulator/SimulatorProtocolTest.groovy
@@ -46,6 +46,7 @@ import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.temporal.ChronoUnit
+import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
@@ -82,21 +83,14 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
@Shared
SimulatorAgent agent
- @Shared
ThingAsset asset
- @Shared
- ScheduledFuture<?> future = Mock(ScheduledFuture)
-
- // Mock executor and rely on the delay argument to determine schedule
- @Shared
- ScheduledExecutorService executor = Mock(ScheduledExecutorService)
-
- @Shared
SimulatorProtocol protocol
- @Shared
- Long delay
+ ScheduledExecutorService originalExecutor
+
+ // Delay of every schedule, so the schedule of an attribute can be resolved through the protocol's replay map
+ final Map<ScheduledFuture<?>, Long> delayByFuture = new ConcurrentHashMap<>()
def setupSpec() {
given: "environment is setup"
@@ -122,26 +116,28 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
stopPseudoClock()
setPseudoClock("1970-01-01T00:00:00.000Z")
- future = Mock(ScheduledFuture)
- executor = Mock(ScheduledExecutorService)
+ delayByFuture.clear()
+ // Mock executor and rely on the delay argument to determine schedule
+ def executor = Mock(ScheduledExecutorService)
asset = new ThingAsset("Test asset").setRealm(Constants.MASTER_REALM)
asset = assetStorageService.merge(asset)
// Must use boxed Long type in Spock closures, so avoiding parameter expansion so it doesn't silently fail.
executor.schedule(_ as Runnable, _ as Long, _ as TimeUnit) >> { args ->
- delay = args[1] as Long
-
// Create a fresh future *per invocation*
- future = Mock(ScheduledFuture)
- future.get() >> {
+ def scheduledFuture = Mock(ScheduledFuture)
+ scheduledFuture.get() >> {
(args[0] as Runnable).run()
return true
}
- return future
+ // Recorded before the future is returned, so the protocol cannot publish it before its delay is known
+ delayByFuture.put(scheduledFuture, args[1] as Long)
+ return scheduledFuture
}
protocol = (SimulatorProtocol) agentService.protocolInstanceMap.get(agent.getId())
+ originalExecutor = protocol.scheduledExecutorService
protocol.scheduledExecutorService = executor
}
@@ -150,6 +146,8 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
conditions.eventually {
protocol.linkedAttributes.size() == 0
}
+ // The mock only lives as long as the feature that created it, so the protocol must not keep it afterwards
+ protocol.scheduledExecutorService = originalExecutor
}
private getDataPoints = {
@@ -160,12 +158,24 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
now.plus(1, ChronoUnit.HOURS),
) }
+ /**
+ * The delay and future the protocol currently has scheduled for an attribute, or null while it has none. The
+ * executor is not told which attribute it schedules for, so the future is matched through the protocol's replay map.
+ */
+ private Map<String, ?> scheduledFor(AttributeRef attributeRef) {
+ def future = protocol.replayMap.get(attributeRef)
+ def delay = future != null ? delayByFuture.get(future) : null
+ return delay != null ? [delay: delay, future: future] : null
+ }
+
private long getDatapointTimestamp(Attribute attribute) {
- return assetDatapointService.queryDatapoints(
+ def datapoints = assetDatapointService.queryDatapoints(
asset.getId(),
attribute.getName(),
getDataPoints.call()
- ).get(0).getTimestamp()
+ )
+ assert !datapoints.isEmpty()
+ return datapoints.get(0).getTimestamp()
}
def "Check Simulator Agent protocol without replay"() {
@@ -251,12 +261,12 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
then: "the delay is 1 hour"
conditions.eventually {
- delay == HOUR_IN_MILLIS
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS
}
when: "fast forward 1 hour"
advancePseudoClock(1, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "datapoint is present"
conditions.eventually {
@@ -265,12 +275,12 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
and: "the delay is 11 hours"
conditions.eventually {
- delay == HOUR_IN_MILLIS * 11
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS * 11
}
when: "fast forward 11 hours"
advancePseudoClock(11, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "datapoint is present"
conditions.eventually {
@@ -279,12 +289,12 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
and: "the delay is 12 hours"
conditions.eventually {
- delay == HOUR_IN_MILLIS * 12
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS * 12
}
when: "fast forward 12 hour"
advancePseudoClock(12, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "datapoints are present"
conditions.eventually {
@@ -293,65 +303,75 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
}
}
- // Won't work because the mocks will overwrite each other
-// def "Check Simulator Agent protocol adds predicted datapoints for multiple attributes"() {
-// when: "replayData is configured for both attributes to add a datapoint in 1hr every day"
-// def attribute1 = asset.getAttribute(ThingAsset.NOTES).get()
-// attribute1.addOrReplaceMeta(
-// new MetaItem<>(AGENT_LINK, new SimulatorAgentLink(agent.getId()).setReplayData(
-// new SimulatorReplayDatapoint(HOUR_IN_SECONDS, "test"),
-// ))
-// )
-// def attribute2 = asset.getAttribute("test").get()
-// attribute2.addOrReplaceMeta(
-// new MetaItem<>(AGENT_LINK, new SimulatorAgentLink(agent.getId()).setReplayData(
-// new SimulatorReplayDatapoint(HOUR_IN_SECONDS * 2, "test"),
-// ))
-// )
-// assetStorageService.merge(asset)
-//
-// then: "the agent status should become CONNECTED and the attribute linked to the protocol"
-// conditions.eventually {
-// assetStorageService.find(agent.getId(), Agent.class).getAgentStatus().orElse(null) == ConnectionStatus.CONNECTED
-// protocol.linkedAttributes.size() == 1
-// }
-//
-// (0..1).each { i ->
-// def days = i * HOUR * 24 // Starts at 0 days, increments with 1 days
-//
-// then: "the delay is 1 hour for attribute 1"
-// conditions.eventually {
-// delay == HOUR
-// }
-//
-// when: "fast forward 1 hour"
-// advancePseudoClock(1, HOURS, container)
-// future.get() // resolve future manually, because we surpassed the delay
-//
-// then: "datapoint is present"
-// conditions.eventually {
-// getDatapointTimestamp(attribute1) == days + HOUR
-// }
-//
-// and: "the delay is 1 more hour for attribute 2"
-// conditions.eventually {
-// delay == HOUR
-// }
-//
-// when: "fast forward 1 hour"
-// advancePseudoClock(1, HOURS, container)
-// future.get() // resolve future manually, because we surpassed the delay
-//
-// then: "datapoint is present"
-// conditions.eventually {
-// getDatapointTimestamp(attribute2) == days + HOUR * 2
-// }
-//
-// when: "fast forward 22 hours"
-// advancePseudoClock(22, HOURS, container)
-// }
-// then: ""
-// }
+ def "Check Simulator Agent protocol replays multiple attributes independently"() {
+ when: "one attribute replays daily from the epoch and another one once from the next day"
+ asset.addOrReplaceAttributes(
+ new Attribute<>("test8", ValueType.TEXT).addMeta(
+ new MetaItem<>(AGENT_LINK, new SimulatorAgentLink(agent.getId()).setReplayData(
+ new SimulatorReplayDatapoint(HOUR_IN_SECONDS, "test"),
+ new SimulatorReplayDatapoint(HOUR_IN_SECONDS * 2, "test")
+ ).setSchedule(new SimulatorProtocol.Schedule(
+ LocalDateTime.ofInstant(Instant.parse("1970-01-01T00:00:00.000Z"), ZoneOffset.UTC), null, "FREQ=DAILY"
+ )))
+ ),
+ new Attribute<>("test9", ValueType.TEXT).addMeta(
+ new MetaItem<>(AGENT_LINK, new SimulatorAgentLink(agent.getId()).setReplayData(
+ new SimulatorReplayDatapoint(HOUR_IN_SECONDS, "test"),
+ new SimulatorReplayDatapoint(HOUR_IN_SECONDS * 25, "test"),
+ new SimulatorReplayDatapoint(HOUR_IN_SECONDS * 49, "test")
+ ).setSchedule(new SimulatorProtocol.Schedule(
+ LocalDateTime.ofInstant(Instant.parse("1970-01-02T00:00:00.000Z"), ZoneOffset.UTC), null, null
+ )))
+ )
+ )
+ asset = assetStorageService.merge(asset)
+ def attribute1 = asset.getAttribute("test8").get()
+ def attribute2 = asset.getAttribute("test9").get()
+ def attributeRef1 = new AttributeRef(asset.getId(), attribute1.getName())
+ def attributeRef2 = new AttributeRef(asset.getId(), attribute2.getName())
+
+ then: "both attributes should be linked to the protocol"
+ conditions.eventually {
+ protocol.linkedAttributes.get(attributeRef1) == attribute1
+ protocol.linkedAttributes.get(attributeRef2) == attribute2
+ }
+
+ and: "each attribute has its own delay"
+ conditions.eventually {
+ scheduledFor(attributeRef1)?.delay == HOUR_IN_MILLIS
+ scheduledFor(attributeRef2)?.delay == DAY_IN_MILLIS + HOUR_IN_MILLIS
+ }
+
+ when: "fast forward 1 hour and only the first attribute is resolved"
+ advancePseudoClock(1, HOURS, container)
+ scheduledFor(attributeRef1).future.get()
+
+ then: "only the first attribute has a datapoint"
+ conditions.eventually {
+ getDatapointTimestamp(attribute1) == HOUR_IN_MILLIS
+ }
+
+ and: "the second attribute is untouched"
+ assetDatapointService.getDatapoints(attributeRef2).size() == 0
+
+ and: "the first attribute is scheduled for its second datapoint and the second attribute is unchanged"
+ conditions.eventually {
+ scheduledFor(attributeRef1)?.delay == HOUR_IN_MILLIS
+ scheduledFor(attributeRef2)?.delay == DAY_IN_MILLIS + HOUR_IN_MILLIS
+ }
+
+ when: "fast forward to the second attribute's first datapoint and only that attribute is resolved"
+ advancePseudoClock(1, DAYS, container)
+ scheduledFor(attributeRef2).future.get()
+
+ then: "the second attribute has a datapoint"
+ conditions.eventually {
+ getDatapointTimestamp(attribute2) == DAY_IN_MILLIS + HOUR_IN_MILLIS
+ }
+
+ and: "the first attribute still has only its own datapoint"
+ assetDatapointService.getDatapoints(attributeRef1).size() == 1
+ }
def "Check Simulator Agent protocol with replay startDate"() {
when: "replayData is configured to replay in 1day and 1hr"
@@ -376,13 +396,13 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
and: "the delay is 25 hours"
conditions.eventually {
- delay == DAY_IN_MILLIS + HOUR_IN_MILLIS
+ scheduledFor(attributeRef)?.delay == DAY_IN_MILLIS + HOUR_IN_MILLIS
}
when: "fast forward 1 day and 1 hour"
advancePseudoClock(1, DAYS, container)
advancePseudoClock(1, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "datapoint is present"
conditions.eventually {
@@ -395,12 +415,12 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
and: "the delay is 24 hours"
conditions.eventually {
- delay == HOUR_IN_MILLIS * 24
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS * 24
}
when: "fast forward 24 hours"
advancePseudoClock(24, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "datapoint is present"
conditions.eventually {
@@ -441,13 +461,13 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
then: "the delay is 4 days and 1 hour"
conditions.eventually {
Instant.ofEpochMilli(getClockTimeOf(container)) == Instant.parse("1970-01-01T00:00:00.000Z")
- delay == 4 * DAY_IN_MILLIS + HOUR_IN_MILLIS
+ scheduledFor(attributeRef)?.delay == 4 * DAY_IN_MILLIS + HOUR_IN_MILLIS
}
when: "fast forward 4 days and 1 hour"
advancePseudoClock(4, DAYS, container)
advancePseudoClock(1, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "datapoint is present"
conditions.eventually {
@@ -461,12 +481,12 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
and: "the delay is 1 hour"
conditions.eventually {
Instant.ofEpochMilli(getClockTimeOf(container)) == Instant.parse("1970-01-05T01:00:00.000Z").plusMillis(weeks)
- delay == HOUR_IN_MILLIS
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS
}
when: "fast forward 1 hour"
advancePseudoClock(1, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "datapoint is present"
conditions.eventually {
@@ -476,13 +496,13 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
and: "the delay is 1 week and -1 hour"
conditions.eventually {
Instant.ofEpochMilli(getClockTimeOf(container)) == Instant.parse("1970-01-05T02:00:00.000Z").plusMillis(weeks)
- delay == WEEK_IN_MILLIS - HOUR_IN_MILLIS
+ scheduledFor(attributeRef)?.delay == WEEK_IN_MILLIS - HOUR_IN_MILLIS
}
when: "fast forward 1 week and -1 hour"
advancePseudoClock(7, DAYS, container)
advancePseudoClock(-1, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "datapoint is present"
conditions.eventually {
@@ -491,7 +511,7 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
}
when: "fast forward past the end date"
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "the attributeRef should be present"
conditions.eventually {
@@ -502,7 +522,7 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
when: "fast forward 5 days and -1 hour"
advancePseudoClock(5, DAYS, container)
advancePseudoClock(-1, HOURS, container)
- future.get() // resolve future manually
+ scheduledFor(attributeRef).future.get() // resolve future manually
then: "the attributeRef is removed from the replayMap"
conditions.eventually {
@@ -536,7 +556,7 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
(1..2).each { i ->
and: "the delay is 1 hour"
conditions.eventually {
- delay == HOUR_IN_MILLIS
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS
}
and: "the predicted datapoints are present"
@@ -551,7 +571,7 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
when: "fast forward 1 hour"
advancePseudoClock(1, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "datapoint is present"
conditions.eventually {
@@ -568,13 +588,13 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
and: "the delay is 1 day and -1 hour"
conditions.eventually {
- delay == DAY_IN_MILLIS - HOUR_IN_MILLIS
+ scheduledFor(attributeRef)?.delay == DAY_IN_MILLIS - HOUR_IN_MILLIS
}
when: "fast forward 1 day and 1 hour"
advancePseudoClock(1, DAYS, container)
advancePseudoClock(-1, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "datapoint is present"
conditions.eventually {
@@ -622,7 +642,7 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
and: "the delay is 1 hour"
conditions.eventually {
- delay == HOUR_IN_MILLIS
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS
}
and: "the predicted datapoints are present"
@@ -634,7 +654,7 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
when: "fast forward 1 hour"
advancePseudoClock(1, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "the predicted datapoints are present"
conditions.eventually {
@@ -671,7 +691,7 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
and: "the delay is 1 hour"
conditions.eventually {
- delay == HOUR_IN_MILLIS
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS
}
and: "the predicted datapoints are present"
@@ -710,7 +730,7 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
and: "the delay is 11 hours"
conditions.eventually {
- delay == HOUR_IN_MILLIS * 11
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS * 11
}
and: "the predicted datapoints are present"
@@ -724,11 +744,11 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
when: "fast forward 11 hours"
advancePseudoClock(11, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "the delay is 12 hours"
conditions.eventually {
- delay == HOUR_IN_MILLIS * 12
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS * 12
}
and: "the predicted datapoints are present"
@@ -742,11 +762,11 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
when: "fast forward 12 hours"
advancePseudoClock(12, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "the delay is 1 hour"
conditions.eventually {
- delay == HOUR_IN_MILLIS
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS
}
and: "the predicted datapoints are present"
@@ -759,11 +779,11 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
when: "fast forward 1 hours"
advancePseudoClock(1, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
then: "the delay is 11 hours"
conditions.eventually {
- delay == HOUR_IN_MILLIS * 11
+ scheduledFor(attributeRef)?.delay == HOUR_IN_MILLIS * 11
}
and: "the predicted datapoints are present"
@@ -775,11 +795,11 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra
when: "fast forward 11 hours"
advancePseudoClock(11, HOURS, container)
- future.get() // resolve future manually, because we surpassed the delay
+ scheduledFor(attributeRef).future.get() // resolve future manually, because we surpassed the delay
- then: "the delay is 11 hours"
+ then: "nothing is scheduled anymore because the recurrence ended"
conditions.eventually {
- delay == HOUR_IN_MILLIS * 11 // delay hasn't changed as the recurrence ended
+ scheduledFor(attributeRef) == null
}
and: "the predicted datapoints are present"
diff --git a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
index 101557ebc..f2331a8d9 100644
--- a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
@@ -93,6 +93,13 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
def mqttHost = mqttBrokerService.host
def mqttPort = mqttBrokerService.port
+ // A consumer in the client topic map only means the client asked for the subscription; the broker registers it
+ // asynchronously and a response published before then is lost, so requests must await the broker side
+ def responseSubscribed = { String clientId, String responseTopic ->
+ def brokerConnection = mqttBrokerService.getConnectionFromClientID(clientId)
+ brokerConnection != null && userAssetProvisioningMQTTHandler.responseSubscribedConnections.get(responseTopic) === brokerConnection
+ }
+
and: "a realm is created with some custom realm roles"
def realm = new Realm()
realm.setName("test")
@@ -193,6 +200,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
conditions.eventually {
assert device1Client.topicConsumerMap.get(device1ResponseTopic) != null
assert device1Client.topicConsumerMap.get(device1ResponseTopic).consumers.size() == 1
+ assert responseSubscribed(mqttDevice1ClientId, device1ResponseTopic)
}
when: "an eavesdropping client connects"
@@ -220,6 +228,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
assert device1Client.getConnectionStatus() == ConnectionStatus.CONNECTED
assert device1Client.topicConsumerMap.get(device1ResponseTopic) != null
assert device1Client.topicConsumerMap.get(device1ResponseTopic).consumers.size() == 1
+ assert responseSubscribed(mqttDevice1ClientId, device1ResponseTopic)
}
when: "the client publishes a valid x509 certificate that has been signed by the CA stored in the provisioning config"
@@ -266,6 +275,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
conditions.eventually {
assert device1Client.topicConsumerMap.get(device1ResponseTopic) != null
assert device1Client.topicConsumerMap.get(device1ResponseTopic).consumers.size() == 1
+ assert responseSubscribed(mqttDevice1ClientId, device1ResponseTopic)
assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null
}
@@ -422,6 +432,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null
connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)
assert connection != null
+ assert responseSubscribed(mqttDevice1ClientId, device1ResponseTopic)
assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
assert subscribeFailures.size() == 2
assert subscribeFailures.contains(assetSubscriptionTopic)
@@ -495,6 +506,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
conditions.eventually {
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != null
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).consumers.size() == 1
+ assert responseSubscribed(mqttDeviceNClientId, deviceNResponseTopic)
assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null
}
@@ -544,6 +556,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
conditions.eventually {
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != null
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).consumers.size() == 1
+ assert responseSubscribed(mqttDeviceNClientId, deviceNResponseTopic)
assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null
}
@@ -593,6 +606,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
conditions.eventually {
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != null
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).consumers.size() == 1
+ assert responseSubscribed(mqttDeviceNClientId, deviceNResponseTopic)
assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null
}
@@ -641,6 +655,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)
assert connection != null
subscribeFailures.size() == 2
+ assert responseSubscribed(mqttDevice1ClientId, device1ResponseTopic)
}
when: "the re-connected client re-authenticates"
@@ -650,7 +665,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
new X509ProvisioningMessage(getClass().getResource("/org/openremote/test/provisioning/device1.pem").text)
).orElse(null))
)
-
+
then: "the broker should have published to the response topic an error message"
conditions.eventually {
assert device1Responses.size() == 1
@@ -677,6 +692,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
conditions.eventually {
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != null
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).consumers.size() == 1
+ assert responseSubscribed(mqttDeviceNClientId, deviceNResponseTopic)
assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null
}
@@ -710,6 +726,9 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
and: "the already connected device publishes its' valid client certificate"
device1Responses.clear()
+ conditions.eventually {
+ assert responseSubscribed(mqttDevice1ClientId, device1ResponseTopic)
+ }
device1Client.sendMessage(
new MQTTMessage<String>(device1RequestTopic, ValueUtil.asJSON(
new X509ProvisioningMessage(getClass().getResource("/org/openremote/test/provisioning/device1.pem").text)
@@ -741,6 +760,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null
assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != existingConnection
assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(existingConnection))
+ assert responseSubscribed(mqttDevice1ClientId, device1ResponseTopic)
}
when: "the re-connected client re-authenticates"
@@ -777,6 +797,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
conditions.eventually {
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != null
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).consumers.size() == 1
+ assert responseSubscribed(mqttDeviceNClientId, deviceNResponseTopic)
assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null
}
diff --git a/test/src/test/groovy/org/openremote/test/rules/AlarmRuleTest.groovy b/test/src/test/groovy/org/openremote/test/rules/AlarmRuleTest.groovy
index ab9e5166d..ca3871d0f 100644
--- a/test/src/test/groovy/org/openremote/test/rules/AlarmRuleTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/AlarmRuleTest.groovy
@@ -27,8 +27,6 @@ import spock.lang.Shared
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
-import java.time.Instant
-
class AlarmRuleTest extends Specification implements ManagerContainerTrait {
@Shared
@@ -157,6 +155,9 @@ getLOG()
}
and: "the created alarm matches the rule action configuration"
+ // Timestamps are written from the container clock, which the test setup puts ahead of the wall clock, so the
+ // wall clock cannot be used as the upper bound
+ def alarmClockNow = getInstantTimeOf(container)
def alarm = alarms.get(0)
alarm.id >= 0L
alarm.realm == managerTestSetup.realmBuildingName
@@ -171,21 +172,27 @@ Value: 6000
alarm.status == Alarm.Status.OPEN
alarm.source == Alarm.Source.REALM_RULESET
alarm.sourceId == Long.toString(realmRuleset.id)
- alarm.createdOn.isAfter(Instant.now().minusSeconds(30))
- alarm.createdOn.isBefore(Instant.now())
+ alarm.createdOn.isAfter(alarmClockNow.minusSeconds(30))
+ !alarm.createdOn.isAfter(alarmClockNow)
alarm.acknowledgedOn == null
alarm.createdOn == alarm.lastModified
alarm.assigneeId == assigneeId
and: "the asset is linked to the alarm"
- def assetLinks = alarmService.getAssetLinks(alarm.id, managerTestSetup.realmBuildingName)
- assetLinks.size() == 1
+ def assetLinks = null
+ conditions.eventually {
+ // The link is stored in its own transaction after the alarm, so it can still be missing once the alarm is
+ // readable
+ assetLinks = alarmService.getAssetLinks(alarm.id, managerTestSetup.realmBuildingName)
+ assert assetLinks.size() == 1
+ }
+ def linkClockNow = getInstantTimeOf(container)
def assetLink = assetLinks.get(0)
assetLink.id.alarmId == alarm.id
assetLink.id.realm == managerTestSetup.realmBuildingName
assetLink.id.assetId == managerTestSetup.apartment2LivingroomId
- assetLink.createdOn.isAfter(Instant.now().minusSeconds(30))
- assetLink.createdOn.isBefore(Instant.now())
+ assetLink.createdOn.isAfter(linkClockNow.minusSeconds(30))
+ !assetLink.createdOn.isAfter(linkClockNow)
assetLink.assetName == "Living Room 2"
assetLink.parentAssetName == "Apartment 2"
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
index 65a59303a..eca7e2f70 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
@@ -484,6 +484,9 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp
}
+ and: "the engine should have fired with the console inside the geofence, resetting the rule for the console"
+ awaitInsideGeofenceEvaluated(realmBuildingEngine, consoleRegistration.id, conditions)
+
when: "the console device moves outside the home geofence again (as defined in the rule)"
emailMessages.clear()
assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, new GeoJSONPoint(-10d, -4d)))
@@ -537,6 +540,9 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp
}
+ and: "the engine should have fired with the console inside the geofence, resetting the rule for the console"
+ awaitInsideGeofenceEvaluated(realmBuildingEngine, consoleRegistration.id, conditions)
+
when: "the console device moves outside the home geofence again (as defined in the rule)"
attributeEvent = new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, new GeoJSONPoint(0d, 0d))
assetProcessingService.sendAttributeEvent(attributeEvent)
@@ -555,6 +561,9 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp
}
+ and: "the engine should have fired with the console inside the geofence, resetting the rule for the console"
+ awaitInsideGeofenceEvaluated(realmBuildingEngine, consoleRegistration.id, conditions)
+
when: "when time advances 5 hours"
advancePseudoClock(5, HOURS, container)
@@ -1762,4 +1771,18 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
cleanup: "disable mock filter"
mockServer.finished = true
}
+
+ /**
+ * A rule only fires for the console again once it has stopped matching it, so the engine must complete an execution
+ * with the inside location before the console can be moved out again.
+ */
+ private static void awaitInsideGeofenceEvaluated(RulesEngine engine, String consoleId, PollingConditions conditions) {
+ conditions.eventually {
+ def assetState = engine.facts.assetStates.find {it.id == consoleId && it.name == Asset.LOCATION.name}
+ assert assetState != null
+ assert assetState.getValue(GeoJSONPoint.class).map{it.x == ManagerTestSetup.SMART_BUILDING_LOCATION.x}.orElse(false)
+ assert assetState.getValue(GeoJSONPoint.class).map{it.y == ManagerTestSetup.SMART_BUILDING_LOCATION.y}.orElse(false)
+ assert engine.lastFireTimestamp > assetState.timestamp
+ }
+ }
}