Skip to content

Commit

Permalink
QoS enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed May 8, 2019
1 parent bfd015b commit 49b584a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
3 changes: 3 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ Functions and classes

.. autoclass:: mqttools.Client
:members:

.. autoclass:: mqttools.QoS
:members:
1 change: 1 addition & 0 deletions mqttools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .client import MalformedPacketError
from .client import ConnectError
from .client import PublishError
from .client import QoS


def main():
Expand Down
15 changes: 11 additions & 4 deletions mqttools/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ class PubcompReasonCode(enum.IntEnum):
SUCCESS = 0
PACKET_IDENTIFIER_NOT_FOUND = 146


class QoS(enum.IntEnum):
AT_MOST_ONCE = 0
AT_LEAST_ONCE = 1
EXACTLY_ONCE = 2


# MQTT 5.0
PROTOCOL_VERSION = 5

Expand Down Expand Up @@ -874,7 +881,7 @@ class Client(object):
client_id='my-client',
will_topic='/my/last/will',
will_message=b'my-last-message',
will_qos=1,
will_qos=QoS.EXACTLY_ONCE,
keep_alive_s=600,
response_timeout=30',
topic_aliases=['/my/topic']',
Expand All @@ -889,7 +896,7 @@ def __init__(self,
client_id=None,
will_topic='',
will_message=b'',
will_qos=0,
will_qos=QoS.AT_MOST_ONCE,
keep_alive_s=0,
response_timeout=5,
topic_aliases=None,
Expand Down Expand Up @@ -1102,7 +1109,7 @@ def disconnect(self):
async def subscribe(self, topic, qos):
"""Subscribe to given topic with given QoS.
>>> await client.subscribe('/my/topic', 0)
>>> await client.subscribe('/my/topic', QoS.AT_MOST_ONCE)
>>> await client.messages.get()
('/my/topic', b'my-message')
Expand Down Expand Up @@ -1178,7 +1185,7 @@ async def publish_qos_2(self, topic, alias, message):
async def publish(self, topic, message, qos):
"""Publish given message to given topic with given QoS.
>>> await client.publish('/my/topic', b'my-message', 0)
>>> await client.publish('/my/topic', b'my-message', QoS.AT_MOST_ONCE)
"""

Expand Down
36 changes: 22 additions & 14 deletions tests/test_mqttools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from io import StringIO

import mqttools
from mqttools import QoS


HOST = 'localhost'
Expand Down Expand Up @@ -137,8 +138,8 @@ def test_subscribe(self):

client = mqttools.Client(*self.broker.address, 'bar')
self.run_until_complete(client.start())
self.run_until_complete(client.subscribe('/a/b', 0))
self.run_until_complete(client.subscribe('/a/c', 0))
self.run_until_complete(client.subscribe('/a/b', QoS.AT_MOST_ONCE))
self.run_until_complete(client.subscribe('/a/c', QoS.AT_MOST_ONCE))
topic, message = self.run_until_complete(client.messages.get())
self.assertEqual(topic, '/a/b')
self.assertEqual(message, b'apa')
Expand Down Expand Up @@ -170,7 +171,7 @@ def test_unsubscribe(self):

client = mqttools.Client(*self.broker.address, 'bar')
self.run_until_complete(client.start())
self.run_until_complete(client.subscribe('/a/b', 0))
self.run_until_complete(client.subscribe('/a/b', QoS.AT_MOST_ONCE))
self.run_until_complete(client.unsubscribe('/a/b'))
self.run_until_complete(client.stop())

Expand All @@ -188,7 +189,7 @@ def test_publish_qos_0(self):

client = mqttools.Client(*self.broker.address, 'bar')
self.run_until_complete(client.start())
self.run_until_complete(client.publish('/a/b', b'apa', 0))
self.run_until_complete(client.publish('/a/b', b'apa', QoS.AT_MOST_ONCE))
self.run_until_complete(client.stop())

def test_publish_qos_1(self):
Expand All @@ -207,7 +208,7 @@ def test_publish_qos_1(self):

client = mqttools.Client(*self.broker.address, 'bar')
self.run_until_complete(client.start())
self.run_until_complete(client.publish('/a/b', b'apa', 1))
self.run_until_complete(client.publish('/a/b', b'apa', QoS.AT_LEAST_ONCE))
self.run_until_complete(client.stop())

def test_publish_qos_2(self):
Expand All @@ -230,7 +231,7 @@ def test_publish_qos_2(self):

client = mqttools.Client(*self.broker.address, 'bar')
self.run_until_complete(client.start())
self.run_until_complete(client.publish('/a/b', b'apa', 2))
self.run_until_complete(client.publish('/a/b', b'apa', QoS.EXACTLY_ONCE))
self.run_until_complete(client.stop())

def test_publish_qos_1_no_matching_subscribers(self):
Expand All @@ -249,7 +250,7 @@ def test_publish_qos_1_no_matching_subscribers(self):

client = mqttools.Client(*self.broker.address, 'bar')
self.run_until_complete(client.start())
self.run_until_complete(client.publish('/a/b', b'apa', 1))
self.run_until_complete(client.publish('/a/b', b'apa', QoS.AT_LEAST_ONCE))
self.run_until_complete(client.stop())

def test_publish_qos_2_no_matching_subscribers(self):
Expand All @@ -272,7 +273,7 @@ def test_publish_qos_2_no_matching_subscribers(self):

client = mqttools.Client(*self.broker.address, 'bar')
self.run_until_complete(client.start())
self.run_until_complete(client.publish('/a/b', b'apa', 2))
self.run_until_complete(client.publish('/a/b', b'apa', QoS.EXACTLY_ONCE))
self.run_until_complete(client.stop())

def test_publish_qos_1_packet_identifier_in_use(self):
Expand All @@ -293,7 +294,9 @@ def test_publish_qos_1_packet_identifier_in_use(self):
self.run_until_complete(client.start())

with self.assertRaises(mqttools.PublishError) as cm:
self.run_until_complete(client.publish('/a/b', b'apa', 1))
self.run_until_complete(client.publish('/a/b',
b'apa',
QoS.AT_LEAST_ONCE))

self.assertEqual(str(cm.exception), 'PACKET_IDENTIFIER_IN_USE(145)')
self.run_until_complete(client.stop())
Expand All @@ -316,7 +319,9 @@ def test_publish_qos_2_packet_identifier_not_found_pubrec(self):
self.run_until_complete(client.start())

with self.assertRaises(mqttools.PublishError) as cm:
self.run_until_complete(client.publish('/a/b', b'apa', 2))
self.run_until_complete(client.publish('/a/b',
b'apa',
QoS.EXACTLY_ONCE))

self.assertEqual(str(cm.exception), 'PACKET_IDENTIFIER_IN_USE(145)')
self.run_until_complete(client.stop())
Expand All @@ -343,7 +348,9 @@ def test_publish_qos_2_packet_identifier_not_found_pubcomp(self):
self.run_until_complete(client.start())

with self.assertRaises(mqttools.PublishError) as cm:
self.run_until_complete(client.publish('/a/b', b'apa', 2))
self.run_until_complete(client.publish('/a/b',
b'apa',
QoS.EXACTLY_ONCE))

self.assertEqual(str(cm.exception), 'PACKET_IDENTIFIER_NOT_FOUND(146)')
self.run_until_complete(client.stop())
Expand Down Expand Up @@ -550,8 +557,8 @@ def test_use_all_topic_aliases(self):
'/foo'
])
self.run_until_complete(client.start())
self.run_until_complete(client.publish('/foo', b'apa', 0))
self.run_until_complete(client.publish('/bar', b'cat', 0))
self.run_until_complete(client.publish('/foo', b'apa', QoS.AT_MOST_ONCE))
self.run_until_complete(client.publish('/bar', b'cat', QoS.AT_MOST_ONCE))
self.run_until_complete(client.stop())

def test_connack_unspecified_error(self):
Expand Down Expand Up @@ -598,7 +605,8 @@ def test_receive_topic_alias(self):
'bar',
topic_alias_maximum=5)
self.run_until_complete(client.start())
self.run_until_complete(client.subscribe('/test/mqttools/foo', 0))
self.run_until_complete(client.subscribe('/test/mqttools/foo',
QoS.AT_MOST_ONCE))
topic, message = self.run_until_complete(client.messages.get())
self.assertEqual(topic, '/test/mqttools/foo')
self.assertEqual(message, b'sets-alias-in-client')
Expand Down

0 comments on commit 49b584a

Please sign in to comment.