Skip to content

Commit

Permalink
Address review suggestions and fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vfreex committed Aug 2, 2023
1 parent 9e5a955 commit 5fd3223
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pyartcd/pyartcd/umb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ def on_disconnected(self):
# notify UMB client
self._client.on_disconnected()
# notify all pending futures of the disconnection
for id in self._futures.keys():
for future_id in self._futures.keys():
if id == "on_disconnected":
self._complete_future("on_disconnected", None)
else:
self._err_future(id, IOError("Connection lost"))
self._err_future(future_id, IOError("Connection lost"))

def on_heartbeat_timeout(self):
self.__print("on_heartbeat_timeout")
Expand Down
23 changes: 22 additions & 1 deletion pyartcd/tests/test_umb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ def test_create_connection(self, StompConnection11: MagicMock):

async def test_call_in_sender_thread(self):
client = AsyncUMBClient("stomp+ssl://stomp1.example.com:12345", cert_file="/path/to/client.crt", key_file="/path/to/client.key")
client._sender_loop = asyncio.get_event_loop()
actual = await client._call_in_sender_thread(lambda: "foo")
self.assertEqual(actual, "foo")

async def test_call_in_sender_thread_with_exception(self):
def func():
raise ValueError("Test error")
client = AsyncUMBClient("stomp+ssl://stomp1.example.com:12345", cert_file="/path/to/client.crt", key_file="/path/to/client.key")
client._sender_loop = asyncio.get_event_loop()
with self.assertRaises(ValueError):
await client._call_in_sender_thread(func)

@patch("pyartcd.umb_client.AsyncUMBClient._create_connection", autospec=True)
async def test_connect(self, _create_connection: MagicMock):
stomp_conn = _create_connection.return_value
stomp_conn.is_connected.return_value = False
client = AsyncUMBClient("stomp+ssl://stomp1.example.com:12345", cert_file="/path/to/client.crt", key_file="/path/to/client.key")
loop = asyncio.get_event_loop()
loop.call_soon(lambda: client._listener._complete_future("on_connected", None))
Expand All @@ -73,7 +77,9 @@ async def test_connect(self, _create_connection: MagicMock):
@patch("pyartcd.umb_client.AsyncUMBClient._create_connection", autospec=True)
async def test_disconnect(self, _create_connection: MagicMock):
client = AsyncUMBClient("stomp+ssl://stomp1.example.com:12345", cert_file="/path/to/client.crt", key_file="/path/to/client.key")
client.connected = True
stomp_conn = _create_connection.return_value
stomp_conn.is_connected.return_value = True
client._sender_loop = asyncio.get_event_loop()
loop = asyncio.get_event_loop()
loop.call_soon(lambda: client._listener._complete_future("on_disconnected", None))
await client.disconnect()
Expand All @@ -83,6 +89,9 @@ async def test_disconnect(self, _create_connection: MagicMock):
@patch("pyartcd.umb_client.AsyncUMBClient._create_connection", autospec=True)
async def test_subscribe(self, _create_connection: MagicMock):
client = AsyncUMBClient("stomp+ssl://stomp1.example.com:12345", cert_file="/path/to/client.crt", key_file="/path/to/client.key")
stomp_conn = _create_connection.return_value
stomp_conn.is_connected.return_value = True
client._sender_loop = asyncio.get_event_loop()
receiver = await client.subscribe(destination="/topic/foo.bar", id="fake-subscription")
conn = _create_connection.return_value
conn.subscribe.assert_called_once_with(destination="/topic/foo.bar", id="fake-subscription", ack="client-individual")
Expand All @@ -91,6 +100,9 @@ async def test_subscribe(self, _create_connection: MagicMock):
@patch("pyartcd.umb_client.AsyncUMBClient._create_connection", autospec=True)
async def test_unsubscribe(self, _create_connection: MagicMock):
client = AsyncUMBClient("stomp+ssl://stomp1.example.com:12345", cert_file="/path/to/client.crt", key_file="/path/to/client.key")
stomp_conn = _create_connection.return_value
stomp_conn.is_connected.return_value = True
client._sender_loop = asyncio.get_event_loop()
client._listener._receivers["fake-subscription"] = MagicMock()
await client.unsubscribe(id="fake-subscription")
conn = _create_connection.return_value
Expand All @@ -100,6 +112,9 @@ async def test_unsubscribe(self, _create_connection: MagicMock):
@patch("pyartcd.umb_client.AsyncUMBClient._create_connection", autospec=True)
async def test_send(self, _create_connection: MagicMock, uuid4: MagicMock):
client = AsyncUMBClient("stomp+ssl://stomp1.example.com:12345", cert_file="/path/to/client.crt", key_file="/path/to/client.key")
stomp_conn = _create_connection.return_value
stomp_conn.is_connected.return_value = True
client._sender_loop = asyncio.get_event_loop()
uuid4.return_value = "fake-uuid"
loop = asyncio.get_event_loop()
loop.call_soon(lambda: client._listener._complete_future(uuid4.return_value, None))
Expand All @@ -111,6 +126,9 @@ async def test_send(self, _create_connection: MagicMock, uuid4: MagicMock):
@patch("pyartcd.umb_client.AsyncUMBClient._create_connection", autospec=True)
async def test_ack(self, _create_connection: MagicMock, uuid4: MagicMock):
client = AsyncUMBClient("stomp+ssl://stomp1.example.com:12345", cert_file="/path/to/client.crt", key_file="/path/to/client.key")
stomp_conn = _create_connection.return_value
stomp_conn.is_connected.return_value = True
client._sender_loop = asyncio.get_event_loop()
uuid4.return_value = "fake-uuid"
loop = asyncio.get_event_loop()
loop.call_soon(lambda: client._listener._complete_future(uuid4.return_value, None))
Expand All @@ -122,6 +140,9 @@ async def test_ack(self, _create_connection: MagicMock, uuid4: MagicMock):
@patch("pyartcd.umb_client.AsyncUMBClient._create_connection", autospec=True)
async def test_nack(self, _create_connection: MagicMock, uuid4: MagicMock):
client = AsyncUMBClient("stomp+ssl://stomp1.example.com:12345", cert_file="/path/to/client.crt", key_file="/path/to/client.key")
stomp_conn = _create_connection.return_value
stomp_conn.is_connected.return_value = True
client._sender_loop = asyncio.get_event_loop()
uuid4.return_value = "fake-uuid"
loop = asyncio.get_event_loop()
loop.call_soon(lambda: client._listener._complete_future(uuid4.return_value, None))
Expand Down

0 comments on commit 5fd3223

Please sign in to comment.