Skip to content

Commit

Permalink
Define shutdown method for NeonMQHandler with unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel committed Jan 14, 2025
1 parent c5bd3b7 commit f6045fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 4 additions & 0 deletions neon_mq_connector/utils/client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def __init__(self, config: dict, service_name: str, vhost: str):
self.connection = pika.BlockingConnection(
parameters=self.get_connection_params(vhost))

def shutdown(self):
MQConnector.stop(self)
self.connection.close()


def send_mq_request(vhost: str, request_data: dict, target_queue: str,
response_queue: str = None, timeout: int = 30,
Expand Down
16 changes: 12 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from neon_mq_connector.utils import RepeatingTimer
from neon_mq_connector.utils.connection_utils import get_timeout, retry, \
wait_for_mq_startup
from neon_mq_connector.utils.client_utils import MQConnector
from neon_mq_connector.utils.client_utils import MQConnector, NeonMQHandler
from neon_mq_connector.utils.network_utils import dict_to_b64, b64_to_dict

from .fixtures import rmq_instance
Expand Down Expand Up @@ -116,15 +116,15 @@ class TestClientUtils(unittest.TestCase):

def setUp(self) -> None:
if self.test_connector is None:
test_conf = {
self.test_conf = {
"server": "localhost",
"port": self.rmq_instance.port,
"users": {"mq_handler": {"user": "test_user",
"password": "test_password"}}}
import neon_mq_connector.utils.client_utils
neon_mq_connector.utils.client_utils._default_mq_config = test_conf
neon_mq_connector.utils.client_utils._default_mq_config = self.test_conf
vhost = "/neon_testing"
self.test_connector = SimpleMQConnector(config=test_conf,
self.test_connector = SimpleMQConnector(config=self.test_conf,
service_name="mq_handler",
vhost=vhost)
self.test_connector.register_consumer("neon_utils_test",
Expand Down Expand Up @@ -199,6 +199,14 @@ def test_send_mq_request_invalid_vhost(self):
with self.assertRaises(ValueError):
send_mq_request("invalid_endpoint", {}, "test", "test", timeout=5)

def test_connector_shutdown(self):
connector = NeonMQHandler(config=self.test_conf,
service_name="mq_handler",
vhost="/neon_testing")
self.assertTrue(connector.connection.is_open)
connector.shutdown()
self.assertTrue(connector.connection.is_closed)


class TestMQConnectionUtils(unittest.TestCase):
counter = 0
Expand Down

0 comments on commit f6045fc

Please sign in to comment.