test(mqtt): assert QoS-0 delivery is fire-and-forget (no packet id, no PUBACK)

A subscription to a QoS-0 topic (grant capped from the client's QoS-1 request) is
delivered at QoS 0 and the client sends no PUBACK. Expose the received QoS from the
subscriber to lock the QoS-0/QoS-1 segregation in place.
This commit is contained in:
ArnabChatterjee20k committed 2026-09-18 11:38:55 +05:30
1 parent be501b4656
commit 655f3c8e6a
2 files changed
+38 -2

No files matched your search

@@ -378,6 +378,42 @@ final class MqttServerTest extends Scope
$this->assertEquals(['matchId' => '42'], $payload['data']);
}
/**
* QoS 0 is fire-and-forget and must stay segregated from QoS 1: a subscription to a
* QoS-0 topic is delivered at QoS 0, so the PUBLISH carries no packet id and the client
* sends no PUBACK (the broker tracks nothing and advances no replay cursor). The client
* requests QoS 1 but the topic caps the grant to 0.
*/
public function testQos0DeliveryHasNoPuback(): void
{
$projectId = $this->getProject()['$id'];
['userId' => $userId, 'jwt' => $jwt] = $this->createUser();
$server = [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $this->getProject()['apiKey'],
];
$topicId = $this->setupPushTopic($server, $userId, 'appwrite-mqtt-qos0', qos: 0);
$subscriber = new MqttSubscriber(self::BROKER_HOST, self::BROKER_PORT);
$this->assertSame(0, $subscriber->connect($projectId, $jwt, 'e2e-qos0-' . $userId, cleanStart: true));
$this->assertSame([0], $subscriber->subscribe([$topicId], Packet::QOS_1), 'the QoS-0 topic must cap the grant to 0');
try {
$this->publishCampaign($server, $topicId, 'No ack', 'delivered at qos 0', ['k' => 'v']);
$received = $subscriber->consume(limit: 1, timeout: 20.0);
} finally {
$subscriber->disconnect();
}
// Test for SUCCESS: delivered once, at QoS 0 (no packet id, so the client never PUBACKs).
$this->assertCount(1, $received, 'qos 0 subscriber did not receive the campaign');
$this->assertSame($topicId, $received[0]['topic']);
$this->assertSame(0, $received[0]['qos'], 'delivery to a QoS-0 subscription must be QoS 0');
$this->assertFalse($received[0]['dup'], 'a QoS-0 delivery is never marked DUP');
}
/**
* MQTT over WebSocket: browser clients reach the broker through its WebSocket listener
* (appwrite-mqtt:8083, routed as ws(s)://<host>/push) instead of raw TCP. The same
+2 -2
View File
@@ -104,7 +104,7 @@ final class MqttSubscriber
* its PUBACK — used to simulate non-contiguous acknowledgement.
*
* @param (callable(int): bool)|null $shouldAck
* @return array<int, array{topic: string, payload: string, dup: bool}>
* @return array<int, array{topic: string, payload: string, dup: bool, qos: int}>
*/
public function consume(int $limit, float $timeout, ?callable $shouldAck = null): array
{
@@ -135,7 +135,7 @@ final class MqttSubscriber
$offset = Properties::skip($body, $offset);
$payload = substr($body, $offset);
$received[] = ['topic' => $topic, 'payload' => $payload, 'dup' => $packet->dup()];
$received[] = ['topic' => $topic, 'payload' => $payload, 'dup' => $packet->dup(), 'qos' => $packet->qos()];
$ack = $shouldAck === null || $shouldAck(\count($received) - 1);
if ($ack && $packet->qos() === 1 && $packetIdBytes !== '') {