Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(iot): deliver data-plane publishes to MQTT subscribers (#6630)

Mario Toffiac81cf8a6304a
diff --git a/docs/services/iot.md b/docs/services/iot.md
index 146160ef9e..4914722c5c 100644
--- a/docs/services/iot.md
+++ b/docs/services/iot.md
@@ -41,7 +41,9 @@ Supported MVP 2 behavior:
Current MVP 2 limitations:
- `DeleteConnection.preventWillMessage` is accepted for SDK request compatibility, but the embedded broker does not expose selective Last Will suppression.
-- HTTP IoT Data `Publish` still treats QoS and MQTT5 metadata as compatibility inputs only; those properties are not fully forwarded or persisted yet.
+- HTTP IoT Data `Publish` reaches MQTT subscribers as an MQTT publish does, whatever account and region they connected in: the broker has neither, where AWS runs one per account and region. It refuses a `qos` other than 0 or 1 with AWS's `InvalidRequestException`. Its MQTT 5 metadata (`contentType`, `correlationData`, `messageExpiry`, `payloadFormatIndicator`, `responseTopic`, `userProperties`) is accepted but not forwarded.
+- A retained message is also sent on a subscription to a wildcard filter that matches its topic. AWS sends it only to a subscription naming its exact topic.
+- A QoS 1 copy sent to a subscriber is never retried, and no limit applies to how many stay unacknowledged. AWS holds further copies once a client has 100 unacknowledged and retries them for up to an hour (16 minutes for MQTT 5).
- `SendDirectMessage` publishes to the requested MQTT topic through the embedded broker. Unlike AWS IoT Core, it does not yet bypass subscription matching to deliver to a client that is not subscribed to that topic.
- `GetConnection` and `ListSubscriptions` report live in-memory broker state only; offline persistent session subscription reporting is not modeled yet.
- Jobs reserved MQTT topics remain follow-up scope; Jobs Data HTTP APIs are implemented first.
@@ -180,7 +182,7 @@ Broker scope:
- Target real AWS IoT/device SDK style MQTT clients, not only handcrafted packet tests.
- Support MQTT v3 and MQTT 5 CONNECT handling used by local compatibility tests.
-- Support QoS 0 and QoS 1 publish/subscribe behavior for the local AWS IoT slice.
+- Support QoS 0 and QoS 1 publish/subscribe behavior for the local AWS IoT slice. A publish, over MQTT or through IoT Data `Publish`, reaches each matching subscriber at the lower of its own and the subscription's QoS, with RETAIN 0, as on AWS. A retained message sent on subscribe carries RETAIN 1 at the lower of its stored and the subscription's QoS.
- Accept PUBLISH payloads up to 128 KB on 1883, 8883 and `/mqtt`, the AWS IoT Core quota (packets up to 146 KB including the variable header). A larger publish disconnects the client without acknowledgement, delivery or rule evaluation, as on AWS.
- Serve MQTT over TLS on 8883 next to plaintext 1883 when TLS is enabled, and verify the device certificate and its `iot:Connect` permission there.
- Keep the plaintext listener permissive except for a `CONNECT` that names a [custom authorizer](#authorizers); topic-level authorization (`Publish`, `Subscribe`, `Receive`) is follow-up scope.
diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java
index e822a5051f..346e271c6f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java
@@ -50,6 +50,7 @@ import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Base64;
+import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -374,7 +375,11 @@ public class IotMqttBrokerService implements Resettable {
}
void publish(String topic, byte[] payload) {
- fanOut(topic, payload, false);
+ publish(topic, payload, 0);
+ }
+
+ void publish(String topic, byte[] payload, int qos) {
+ fanOut(topic, payload, qos);
}
boolean disconnectClient(String clientId, boolean cleanSession) {
@@ -544,7 +549,8 @@ public class IotMqttBrokerService implements Resettable {
device == null ? config.defaultAccountId() : device.accountId(),
device == null ? config.defaultRegion() : device.region(),
System.currentTimeMillis(),
- endpoint.keepAliveTimeSeconds());
+ endpoint.keepAliveTimeSeconds(),
+ new AtomicInteger());
ClientSession previous = sessionsByClient.put(clientId, session);
if (previous != null && previous.endpoint() != endpoint) {
endSession(previous, DUPLICATE_CLIENTID);
@@ -729,7 +735,7 @@ public class IotMqttBrokerService implements Resettable {
iotService.get().publish(topic, payload, message.isRetain(), message.qosLevel().value(), null,
session.clientId(), rules -> evaluateRulesOnWorker(topic, rules));
- fanOut(topic, payload, false);
+ fanOut(topic, payload, message.qosLevel().value());
}
/**
@@ -787,26 +793,34 @@ public class IotMqttBrokerService implements Resettable {
}
}
- private void fanOut(String topic, byte[] payload, boolean retained) {
+ /**
+ * Delivers a live copy, RETAIN 0 as on AWS, to each subscriber with a matching subscription, at the
+ * lower of {@code qos} and the highest QoS among its matching subscriptions.
+ */
+ private void fanOut(String topic, byte[] payload, int qos) {
byte[] safePayload = payload == null ? new byte[0] : payload.clone();
for (ClientSession session : sessionsByClient.values()) {
- if (!session.endpoint().isConnected() || !hasMatchingSubscription(session.clientId(), topic)) {
+ Map<String, Subscription> subscriptions = subscriptionsByClient.get(session.clientId());
+ int granted = subscriptions == null ? -1 : grantedQos(subscriptions.values(), topic);
+ if (!session.endpoint().isConnected() || granted < 0) {
continue;
}
+ MqttQoS delivered = MqttQoS.valueOf(Math.min(qos, granted));
try {
- session.endpoint().publish(topic, Buffer.buffer(safePayload), MqttQoS.AT_MOST_ONCE, false, retained);
+ session.endpoint().publish(topic, Buffer.buffer(safePayload), delivered, false, false, session.packetId(delivered));
} catch (IllegalStateException e) {
LOG.debugv("IoT MQTT client {0} closed before the publish on {1} reached it", session.clientId(), topic);
}
}
}
- private boolean hasMatchingSubscription(String clientId, String topic) {
- Map<String, Subscription> subscriptions = subscriptionsByClient.get(clientId);
- if (subscriptions == null) {
- return false;
- }
- return subscriptions.values().stream().anyMatch(subscription -> topicMatches(subscription.topicFilter(), topic));
+ /** The highest QoS among the subscriptions matching {@code topic}, or -1 when none does. */
+ private int grantedQos(Collection<Subscription> subscriptions, String topic) {
+ return subscriptions.stream()
+ .filter(subscription -> topicMatches(subscription.topicFilter(), topic))
+ .mapToInt(Subscription::qos)
+ .max()
+ .orElse(-1);
}
private void deliverRetained(ClientSession session, List<Subscription> subscriptions) {
@@ -818,13 +832,14 @@ public class IotMqttBrokerService implements Resettable {
if (!deliveredTopics.add(retained.getTopic())) {
continue;
}
- boolean matches = subscriptions.stream()
- .anyMatch(subscription -> topicMatches(subscription.topicFilter(), retained.getTopic()));
- if (!matches) {
+ int granted = grantedQos(subscriptions, retained.getTopic());
+ if (granted < 0) {
continue;
}
byte[] payload = Base64.getDecoder().decode(retained.getPayload());
- session.endpoint().publish(retained.getTopic(), Buffer.buffer(payload), MqttQoS.AT_MOST_ONCE, false, true);
+ // Publish kept a qos outside 0 and 1 as sent before it refused one, so a stored message may carry one.
+ MqttQoS delivered = MqttQoS.valueOf(Math.clamp(retained.getQos(), 0, granted));
+ session.endpoint().publish(retained.getTopic(), Buffer.buffer(payload), delivered, false, true, session.packetId(delivered));
}
}
@@ -886,7 +901,7 @@ public class IotMqttBrokerService implements Resettable {
vertx.runOnContext(ignored -> {
iotService.get().publish(topic, payload, false, 0, null, session.clientId(),
rules -> evaluateRulesOnWorker(topic, rules, admitted));
- fanOut(topic, payload, false);
+ fanOut(topic, payload, 0);
});
}
@@ -958,7 +973,18 @@ public class IotMqttBrokerService implements Resettable {
String accountId,
String region,
long connectedAt,
- int keepAliveDuration) {
+ int keepAliveDuration,
+ AtomicInteger packetIds) {
+
+ /**
+ * The packet identifier of a copy sent at {@code qos}: none at QoS 0, otherwise the session's
+ * next, from 1 to 65535. Vert.x draws its own from an unsynchronized counter, which two threads
+ * fanning out to the same client race on.
+ */
+ int packetId(MqttQoS qos) {
+ // ponytail: no in-flight tracking, so an identifier repeats after 65535 QoS 1 copies to one client; track until PUBACK if Floci ever redelivers.
+ return qos == MqttQoS.AT_MOST_ONCE ? 0 : packetIds.updateAndGet(id -> id % 65535 + 1);
+ }
HistoryKey historyKey() {
return new HistoryKey(accountId, region, clientId);
diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java
index 32663d1231..a0bebff1ea 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java
@@ -1135,9 +1135,20 @@ public class IotService {
publish(topic, payload, false, 0, null, null);
}
- /** {@code clientId} is the MQTT client that published, or null for a message that did not come over MQTT. */
+ /**
+ * A publish from outside the broker, such as the data plane Publish API: retained, recorded and
+ * run through the topic rules before it returns, then delivered to the broker's matching
+ * subscribers. {@code clientId} is what a rule's clientid() reports, null for the data plane.
+ */
public void publish(String topic, byte[] payload, boolean retain, int qos, String region, String clientId) {
+ if (qos == 2) {
+ throw new AwsException("InvalidRequestException", "QoS 2 is currently not supported", 400);
+ }
+ if (qos != 0 && qos != 1) {
+ throw new AwsException("InvalidRequestException", "Invalid QoS value: " + qos, 400);
+ }
publish(topic, payload, retain, qos, region, clientId, Runnable::run);
+ mqttBrokerService.publish(topic, payload, qos);
}
/**
diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotDataIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotDataIntegrationTest.java
index 50848ba7b2..01cb0c68f3 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iot/IotDataIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotDataIntegrationTest.java
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.iot;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
import java.util.List;
@@ -177,6 +179,29 @@ class IotDataIntegrationTest {
.body("__type", equalTo("ResourceNotFoundException"));
}
+ /** Measured on AWS IoT: a QoS other than 0 or 1 is refused before the message is retained. */
+ @ParameterizedTest
+ @CsvSource({"2,QoS 2 is currently not supported", "-1,Invalid QoS value: -1", "7,Invalid QoS value: 7"})
+ void publishRefusesAQosOtherThanZeroOrOne(int qos, String message) {
+ given()
+ .contentType("text/plain")
+ .queryParam("retain", true)
+ .queryParam("qos", qos)
+ .body("refused")
+ .when()
+ .post("/topics/devices/qos/refused")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("InvalidRequestException"))
+ .body("message", equalTo(message));
+
+ given()
+ .when()
+ .get("/retainedMessage/devices/qos/refused")
+ .then()
+ .statusCode(404);
+ }
+
@Test
void shadowNullDeletesPropertiesAndVersionConflicts() {
given()
diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java
index 1175b3c3d9..faa5636d13 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java
@@ -69,6 +69,7 @@ import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509ExtendedKeyManager;
import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetSocketAddress;
@@ -83,6 +84,7 @@ import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -493,7 +495,7 @@ class IotMqttBrokerServiceTest {
subscribeHandler.set(invocation.getArgument(0));
return closing;
});
- when(closing.publish(anyString(), any(), any(), anyBoolean(), anyBoolean()))
+ when(closing.publish(anyString(), any(), any(), anyBoolean(), anyBoolean(), anyInt()))
.thenThrow(new IllegalStateException("the connection closed"));
MqttTopicSubscription requested = mock(MqttTopicSubscription.class);
when(requested.topicName()).thenReturn("devices/+/metrics");
@@ -511,7 +513,7 @@ class IotMqttBrokerServiceTest {
broker.publish("devices/d1/metrics", "{\"t\":1}".getBytes(StandardCharsets.UTF_8));
- verify(closing).publish(eq("devices/d1/metrics"), any(), any(), anyBoolean(), anyBoolean());
+ verify(closing).publish(eq("devices/d1/metrics"), any(), any(), anyBoolean(), anyBoolean(), anyInt());
assertEquals("{\"t\":1}", fannedOut.poll(10, TimeUnit.SECONDS), "the other subscriber got its copy");
} finally {
disconnect(subscriber);
@@ -892,6 +894,74 @@ class IotMqttBrokerServiceTest {
assertFalse(disconnected.get(0).get("clientInitiatedDisconnect").asBoolean());
}
+ /**
+ * A retained message whose stored QoS is negative, which Publish kept as sent before it refused
+ * a qos outside 0 and 1, is still sent on subscribe, at QoS 0.
+ */
+ @Test
+ void aRetainedMessageStoredWithANegativeQosIsStillSentOnSubscribe() throws Exception {
+ startBrokerRecordingPublishes();
+ iotService.get().publish("legacy/status", "{}".getBytes(StandardCharsets.UTF_8), true, -1, null, null, Runnable::run);
+ BlockingQueue<String> topics = new LinkedBlockingQueue<>();
+
+ MqttClient subscriber = subscribedPlain("legacy", "legacy/status", topics);
+ try {
+ assertEquals("legacy/status", topics.poll(10, TimeUnit.SECONDS));
+ } finally {
+ disconnect(subscriber);
+ }
+ }
+
+ /**
+ * QoS 1 copies fanned out to one client from several threads, none acknowledged yet, each carry
+ * their own packet identifier, as MQTT requires.
+ */
+ @Test
+ void concurrentQos1CopiesToOneClientCarryDistinctPacketIdentifiers() throws Exception {
+ startBrokerRecordingPublishes();
+ int threads = 8;
+ int perThread = 2000;
+ ExecutorService publishers = Executors.newFixedThreadPool(threads);
+ try (Socket socket = rawConnect("packet-ids")) {
+ DataInputStream in = rawSubscribeQos1(socket, "ids/#");
+
+ for (int thread = 0; thread < threads; thread++) {
+ publishers.execute(() -> {
+ for (int i = 0; i < perThread; i++) {
+ broker.publish("ids/x", new byte[] {1}, 1);
+ }
+ });
+ }
+
+ Set<Integer> packetIds = new HashSet<>();
+ for (int i = 0; i < threads * perThread; i++) {
+ packetIds.add(readQos1PublishPacketId(in));
+ }
+ assertEquals(threads * perThread, packetIds.size(), "no two unacknowledged copies share a packet identifier");
+ } finally {
+ publishers.shutdownNow();
+ }
+ }
+
+ /** QoS 1 copies sent to other clients do not use up the packet identifiers of a client whose QoS 1 copy is unacknowledged. */
+ @Test
+ void anotherClientsTrafficDoesNotRecycleAPacketIdentifierStillInFlight() throws Exception {
+ startBrokerRecordingPublishes();
+ try (Socket busy = rawConnect("busy"); Socket socket = rawConnect("slow")) {
+ rawSubscribeQos1(busy, "busy/#");
+ DataInputStream in = rawSubscribeQos1(socket, "slow/#");
+ broker.publish("slow/first", new byte[] {1}, 1);
+ int first = readQos1PublishPacketId(in);
+
+ for (int i = 0; i < 65_534; i++) {
+ broker.publish("busy/x", new byte[] {1}, 1);
+ }
+ broker.publish("slow/second", new byte[] {2}, 1);
+
+ assertNotEquals(first, readQos1PublishPacketId(in), "the first copy was never acknowledged");
+ }
+ }
+
@Test
void aQos2PublishEndsTheSessionWithAClientError() throws Exception {
startBrokerRecordingPublishes();
@@ -1052,7 +1122,7 @@ class IotMqttBrokerServiceTest {
});
broker.handleEndpoint(departing, false);
verify(subscriber, timeout(10_000))
- .publish(eq("$aws/events/presence/connected/departing"), any(), any(), anyBoolean(), anyBoolean());
+ .publish(eq("$aws/events/presence/connected/departing"), any(), any(), anyBoolean(), anyBoolean(), anyInt());
ExecutorService departingEventLoop = Executors.newSingleThreadExecutor();
subscriberMonitor.lock();
@@ -1066,7 +1136,7 @@ class IotMqttBrokerServiceTest {
departingEventLoop.shutdown();
}
verify(subscriber, timeout(10_000))
- .publish(eq("$aws/events/presence/disconnected/departing"), any(), any(), anyBoolean(), anyBoolean());
+ .publish(eq("$aws/events/presence/disconnected/departing"), any(), any(), anyBoolean(), anyBoolean(), anyInt());
}
/** A PUBLISH, SUBSCRIBE, UNSUBSCRIBE or PUBACK counts as traffic for the keep-alive, as a PINGREQ does. */
@@ -1190,8 +1260,8 @@ class IotMqttBrokerServiceTest {
broker.publish("stale/topic", "{}".getBytes(StandardCharsets.UTF_8));
broker.publish("kept/topic", "{}".getBytes(StandardCharsets.UTF_8));
- verify(current, timeout(10_000)).publish(eq("kept/topic"), any(), any(), anyBoolean(), anyBoolean());
- verify(current, never()).publish(eq("stale/topic"), any(), any(), anyBoolean(), anyBoolean());
+ verify(current, timeout(10_000)).publish(eq("kept/topic"), any(), any(), anyBoolean(), anyBoolean(), anyInt());
+ verify(current, never()).publish(eq("stale/topic"), any(), any(), anyBoolean(), anyBoolean(), anyInt());
}
/**
@@ -1893,6 +1963,37 @@ class IotMqttBrokerServiceTest {
socket.getOutputStream().write(packet(0x30 | (qos << 1), body.toByteArray()));
}
+ /** Subscribes a raw client to one filter at QoS 1 and returns its stream once the broker granted it. */
+ private static DataInputStream rawSubscribeQos1(Socket socket, String topicFilter) throws IOException {
+ ByteArrayOutputStream subscribe = new ByteArrayOutputStream();
+ subscribe.write(0x00);
+ subscribe.write(0x01);
+ writeUtf8(subscribe, topicFilter);
+ subscribe.write(0x01);
+ socket.getOutputStream().write(packet(0x82, subscribe.toByteArray()));
+ DataInputStream in = new DataInputStream(socket.getInputStream());
+ assertArrayEquals(new byte[] {(byte) 0x90, 0x03, 0x00, 0x01, 0x01}, in.readNBytes(5), "SUBACK granting QoS 1");
+ return in;
+ }
+
+ /** Reads one QoS 1 PUBLISH and returns its packet identifier. */
+ private static int readQos1PublishPacketId(DataInputStream in) throws IOException {
+ assertEquals(0x32, in.readUnsignedByte(), "a QoS 1 PUBLISH without DUP or RETAIN");
+ int remaining = 0;
+ int multiplier = 1;
+ int encoded;
+ do {
+ encoded = in.readUnsignedByte();
+ remaining += (encoded & 127) * multiplier;
+ multiplier *= 128;
+ } while ((encoded & 128) != 0);
+ int topicLength = in.readUnsignedShort();
+ in.skipNBytes(topicLength);
+ int packetId = in.readUnsignedShort();
+ in.skipNBytes(remaining - topicLength - 4);
+ return packetId;
+ }
+
private static byte[] packet(int type, byte[] body) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(type);
diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttEnabledIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttEnabledIntegrationTest.java
index 2327dfb456..80e1fed1a9 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttEnabledIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttEnabledIntegrationTest.java
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.iot;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.github.hectorvent.floci.testing.IotDataRequests;
import io.github.hectorvent.floci.testing.IotMqttTlsProfile;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.quarkus.test.junit.QuarkusTest;
@@ -19,6 +20,8 @@ import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
+import software.amazon.awssdk.core.SdkBytes;
+import software.amazon.awssdk.services.iotdataplane.IotDataPlaneClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -194,11 +197,71 @@ class IotMqttEnabledIntegrationTest {
MqttPublish received = subscriber.takePublish();
assertEquals(topic, received.topic());
assertArrayEquals(payload, received.payload());
+ assertEquals(1, received.qos(), "measured on AWS IoT: the lower of the publish and subscription QoS");
}
awaitPublishedEvent(topic, payload);
}
+ /** Measured on AWS IoT: a data plane Publish reaches every matching subscriber and runs the matching rule once. */
+ @Test
+ void dataPlanePublishReachesAWildcardSubscriberAndFiresItsRuleOnce() throws Exception {
+ String prefix = "dataplane/" + System.nanoTime();
+ String queueUrl = createQueue("dataplane-rule-" + System.nanoTime());
+ String rule = "dataPlaneRule" + System.nanoTime();
+ createSqsRule(rule, "SELECT * FROM '" + prefix + "/b'", queueUrl);
+ try (MqttTestClient subscriber = connectMqtt("dataplane-sub");
+ IotDataPlaneClient iotData = IotDataRequests.iotDataClient("test")) {
+ subscriber.subscribe(prefix + "/#", 1);
+
+ iotData.publish(request -> request.topic(prefix + "/b").qos(1).payload(SdkBytes.fromUtf8String("{\"cmd\":\"on\"}")));
+
+ MqttPublish received = subscriber.takePublish();
+ assertEquals(prefix + "/b", received.topic());
+ assertEquals("{\"cmd\":\"on\"}", new String(received.payload(), StandardCharsets.UTF_8));
+ assertEquals(1, received.qos());
+ assertFalse(received.retained(), "a live copy carries RETAIN 0");
+ List<JsonNode> fired = awaitMessages(queueUrl, 1);
+ assertEquals(1, fired.size(), "the rule fired once: " + fired);
+ assertEquals("on", fired.get(0).path("cmd").asText());
+ } finally {
+ deleteRule(rule);
+ }
+ }
+
+ /**
+ * Measured on AWS IoT: a retained data plane Publish reaches a current subscriber as a live copy, and a later
+ * subscriber of its exact topic as the retained message, each at the lower of the publish and subscription QoS.
+ */
+ @Test
+ void retainedDataPlanePublishReachesACurrentAndALaterSubscriber() throws Exception {
+ String prefix = "dataplane/" + System.nanoTime();
+ String topic = prefix + "/config";
+ try (MqttTestClient current = connectMqtt("dataplane-current");
+ IotDataPlaneClient iotData = IotDataRequests.iotDataClient("test")) {
+ current.subscribe(prefix + "/#", 0);
+
+ iotData.publish(request -> request.topic(topic).qos(1).retain(true).payload(SdkBytes.fromUtf8String("config-v1")));
+ cleanup.register(() -> given().contentType("application/octet-stream").queryParam("retain", true)
+ .body(new byte[0]).when().post("/topics/" + topic).then().statusCode(200));
+
+ MqttPublish live = current.takePublish();
+ assertEquals("config-v1", new String(live.payload(), StandardCharsets.UTF_8));
+ assertEquals(0, live.qos());
+ assertFalse(live.retained(), "a live copy carries RETAIN 0");
+
+ try (MqttTestClient later = connectMqtt("dataplane-later")) {
+ later.subscribe(topic, 1);
+
+ MqttPublish retained = later.takePublish();
+ assertEquals(topic, retained.topic());
+ assertEquals("config-v1", new String(retained.payload(), StandardCharsets.UTF_8));
+ assertEquals(1, retained.qos());
+ assertTrue(retained.retained());
+ }
+ }
+ }
+
@Test
void publishOfTheAwsMaximumPayloadIsDelivered() throws Exception {
String topic = "phase6/limit/" + System.nanoTime();
@@ -891,7 +954,7 @@ class IotMqttEnabledIntegrationTest {
out.writeBytes(bytes);
}
- private record MqttPublish(String topic, byte[] payload) {
+ private record MqttPublish(String topic, byte[] payload, int qos, boolean retained) {
}
private static final class MqttTestClient implements AutoCloseable {
@@ -913,7 +976,7 @@ class IotMqttEnabledIntegrationTest {
@Override
public void messageArrived(String topic, MqttMessage message) {
- testClient.publishes.add(new MqttPublish(topic, message.getPayload()));
+ testClient.publishes.add(new MqttPublish(topic, message.getPayload(), message.getQos(), message.isRetained()));
}
@Override
@@ -1007,7 +1070,7 @@ class IotMqttEnabledIntegrationTest {
@Override
public void messageArrived(String topic, org.eclipse.paho.mqttv5.common.MqttMessage message) {
- testClient.publishes.add(new MqttPublish(topic, message.getPayload()));
+ testClient.publishes.add(new MqttPublish(topic, message.getPayload(), message.getQos(), message.isRetained()));
}
@Override
diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotRetainedTimestampIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotRetainedTimestampIntegrationTest.java
index a384c4d273..5dc2c6575c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iot/IotRetainedTimestampIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotRetainedTimestampIntegrationTest.java
@@ -4,20 +4,15 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.iot.model.IotRetainedMessage;
+import io.github.hectorvent.floci.testing.IotDataRequests;
import io.quarkus.test.junit.QuarkusTest;
-import io.restassured.RestAssured;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
-import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
-import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.SdkBytes;
-import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
-import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.iotdataplane.IotDataPlaneClient;
-import java.net.URI;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
@@ -71,11 +66,7 @@ class IotRetainedTimestampIntegrationTest {
String authorization = "AWS4-HMAC-SHA256 Credential=" + account
+ "/20261023/us-east-1/iotdata/aws4_request, SignedHeaders=host, Signature=test";
ObjectMapper mapper = new ObjectMapper();
- try (IotDataPlaneClient client = IotDataPlaneClient.builder()
- .endpointOverride(URI.create("http://localhost:" + RestAssured.port))
- .httpClientBuilder(UrlConnectionHttpClient.builder()).region(Region.US_EAST_1)
- .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test")))
- .build()) {
+ try (IotDataPlaneClient client = IotDataRequests.iotDataClient(account)) {
long before = System.currentTimeMillis();
client.publish(request -> request.topic(topic).qos(qos).retain(true).payload(SdkBytes.fromUtf8String("online")));
long after = System.currentTimeMillis();
diff --git a/src/test/java/io/github/hectorvent/floci/testing/IotDataRequests.java b/src/test/java/io/github/hectorvent/floci/testing/IotDataRequests.java
new file mode 100644
index 0000000000..970cfa2b5b
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/testing/IotDataRequests.java
@@ -0,0 +1,24 @@
+package io.github.hectorvent.floci.testing;
+
+import io.restassured.RestAssured;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.iotdataplane.IotDataPlaneClient;
+
+import java.net.URI;
+
+public final class IotDataRequests {
+ private IotDataRequests() {}
+
+ /** An IoT Data client on the test instance in us-east-1, signing with {@code accessKey}, which Floci reads as the account when it is one. */
+ public static IotDataPlaneClient iotDataClient(String accessKey) {
+ return IotDataPlaneClient.builder()
+ .endpointOverride(URI.create("http://localhost:" + RestAssured.port))
+ .httpClientBuilder(UrlConnectionHttpClient.builder())
+ .region(Region.US_EAST_1)
+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, "test")))
+ .build();
+ }
+}