-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import logging | ||
import asyncio | ||
import unittest | ||
|
||
import mqttools | ||
|
||
|
||
class ClientTest(unittest.TestCase): | ||
|
||
def test_connack_timeout(self): | ||
asyncio.run(self.connack_timeout()) | ||
|
||
async def connack_timeout(self): | ||
def on_client_connected(reader, writer): | ||
pass | ||
|
||
listener = await asyncio.start_server(on_client_connected, 'localhost', 0) | ||
|
||
async def broker_main(): | ||
async with listener: | ||
try: | ||
await listener.serve_forever() | ||
except asyncio.CancelledError: | ||
pass | ||
|
||
async def client_main(): | ||
client = mqttools.Client(*listener.sockets[0].getsockname(), | ||
'connack', | ||
response_timeout=0.1) | ||
|
||
with self.assertRaises(mqttools.TimeoutError): | ||
await client.start() | ||
|
||
listener.close() | ||
|
||
await asyncio.wait_for(asyncio.gather(broker_main(), client_main()), 1) | ||
|
||
def test_subscribe_timeout(self): | ||
asyncio.run(self.subscribe_timeout()) | ||
|
||
async def subscribe_timeout(self): | ||
def on_client_connected(reader, writer): | ||
# CONNACK | ||
writer.write(b'\x20\x03\x00\x00\x00') | ||
|
||
listener = await asyncio.start_server(on_client_connected, 'localhost', 0) | ||
|
||
async def broker_main(): | ||
async with listener: | ||
try: | ||
await listener.serve_forever() | ||
except asyncio.CancelledError: | ||
pass | ||
|
||
async def client_main(): | ||
client = mqttools.Client(*listener.sockets[0].getsockname(), | ||
'suback', | ||
response_timeout=0.1) | ||
await client.start() | ||
|
||
with self.assertRaises(mqttools.TimeoutError): | ||
await client.subscribe('/foo') | ||
|
||
listener.close() | ||
|
||
await asyncio.wait_for(asyncio.gather(broker_main(), client_main()), 1) | ||
|
||
def test_unsubscribe_timeout(self): | ||
asyncio.run(self.unsubscribe_timeout()) | ||
|
||
async def unsubscribe_timeout(self): | ||
def on_client_connected(reader, writer): | ||
# CONNACK | ||
writer.write(b'\x20\x03\x00\x00\x00') | ||
|
||
listener = await asyncio.start_server(on_client_connected, 'localhost', 0) | ||
|
||
async def broker_main(): | ||
async with listener: | ||
try: | ||
await listener.serve_forever() | ||
except asyncio.CancelledError: | ||
pass | ||
|
||
async def client_main(): | ||
client = mqttools.Client(*listener.sockets[0].getsockname(), | ||
'unsuback', | ||
response_timeout=0.1) | ||
await client.start() | ||
|
||
with self.assertRaises(mqttools.TimeoutError): | ||
await client.unsubscribe('/foo') | ||
|
||
listener.close() | ||
|
||
await asyncio.wait_for(asyncio.gather(broker_main(), client_main()), 1) | ||
|
||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |