Skip to content
Snippets Groups Projects
Commit 1abc43ee authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

SW-516: added new class TemporaryQueue which can be used when a...

SW-516: added new class TemporaryQueue which can be used when a temporary-auto-delete-queue is needed, like for example in the tests.
parent f1111853
Branches
Tags
No related merge requests found
...@@ -205,9 +205,9 @@ class FromBus(object): ...@@ -205,9 +205,9 @@ class FromBus(object):
# t.daemon = True # t.daemon = True
# t.start() # t.start()
except proton.ProtonException: except proton.ProtonException as pe:
raise_exception(MessageBusError, raise_exception(MessageBusError,
"[FromBus] Failed to create %s" % (what,)) "[FromBus] Failed to create %s: %s" % (what, pe))
logger.debug("[FromBus] Created %s", what) logger.debug("[FromBus] Created %s", what)
def receive(self, timeout=DEFAULT_TIMEOUT, logDebugMessages=True): def receive(self, timeout=DEFAULT_TIMEOUT, logDebugMessages=True):
...@@ -556,6 +556,85 @@ class ToBus(object): ...@@ -556,6 +556,85 @@ class ToBus(object):
logger.debug("[ToBus] Message sent to: %s subject: %s" % (self.address, qmsg.subject)) logger.debug("[ToBus] Message sent to: %s subject: %s" % (self.address, qmsg.subject))
class TemporaryQueue(object):
"""
A TemporaryQueue instance can be used to setup a dynamic temporary queue which is closed and deleted automagically when leaving context.
Together with the factory methods create_frombus and/or create_tobus it gives us to following simple but often used use case:
with TemporaryQueue("MyTestQueue") as tmp_queue:
with tmp_queue.create_tobus() as tobus, tmp_queue.create_frombus() as frombus:
# send a message...
original_msg = EventMessage(content="foobar")
tobus.send(original_msg)
# ...receive the message.
received_msg = frombus.receive()
Alternative use cases with only a tobus or only a frombus on the tmp_queue are also possible.
"""
def __init__(self, name=None, broker="localhost"):
"""
Create a TemporaryQueue instance with an optional name on the given broker.
:param name: Optional name, which is part of the final address which also includes a uuid.
:param broker: the qpid broker to connect to.
"""
self.name = name
self.broker = broker
self._dynamic_receiver = None
self.address = None
def __enter__(self):
"""
Opens/creates the temporary queue. It is automatically closed when leaving context in __exit__.
:return: self.
"""
self.open()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""
Close/remove the temporary queue.
"""
self.close()
def open(self):
"""
Open/create the temporary queue.
It is advised to use the TemporaryQueue instance in a 'with' context, which guarantees the close call.
"""
logger.info("Creating TemporaryQueue...")
connection = proton.utils.BlockingConnection(self.broker)
self._dynamic_receiver = connection.create_receiver(address=None, dynamic=True, name=self.name)
self.address = self._dynamic_receiver.link.remote_source.address
logger.info("Created TemporaryQueue at %s", self.address)
def close(self):
"""
Close/remove the temporary queue.
It is advised to use the TemporaryQueue instance in a 'with' context, which guarantees the close call.
"""
logger.debug("Closing TemporaryQueue at %s", self.address)
self._dynamic_receiver.close()
self._dynamic_receiver.connection.close()
self._dynamic_receiver = None
logger.info("Closed TemporaryQueue at %s", self.address)
self.address = None
def create_frombus(self):
"""
Factory method to create a FromBus instance which is connected to this TemporaryQueue
:return: FromBus
"""
return FromBus(broker=self.broker, address=self.address)
def create_tobus(self):
"""
Factory method to create a ToBus instance which is connected to this TemporaryQueue
:return: ToBus
"""
return ToBus(broker=self.broker, address=self.address)
class AbstractBusListener(object): class AbstractBusListener(object):
""" """
AbstractBusListener class for handling messages which are received on a message bus. AbstractBusListener class for handling messages which are received on a message bus.
...@@ -768,4 +847,4 @@ class AbstractBusListener(object): ...@@ -768,4 +847,4 @@ class AbstractBusListener(object):
logger.error("finalize_loop() failed with %s", e) logger.error("finalize_loop() failed with %s", e)
__all__ = ["FromBus", "ToBus", "AbstractBusListener"] __all__ = ["FromBus", "ToBus", "TemporaryQueue", "AbstractBusListener"]
This diff is collapsed.
#!/bin/bash -e #!/bin/bash -e
# Cleanup on normal exit and on SIGHUP, SIGINT, SIGQUIT, and SIGTERM
trap 'qpid-config del queue --force $queue' 0 1 2 3 15
# Generate randome queue name
queue=$(< /dev/urandom tr -dc [:alnum:] | head -c16)
# Create the queue
qpid-config add queue $queue
# Run the unit test # Run the unit test
source python-coverage.sh source python-coverage.sh
python_coverage_test "Messaging/python" t_messagebus.py $queue python_coverage_test "Messaging/python" t_messagebus.py
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment