def test_producer_unread_count(self): """ Test unread message count from producer side """ producer = MessageProducer(producer_id="p1", \ message_type=TestMessage._msg_type, method="sync") messages = [] for i in range(0, TestMessage._msg_count): messages.append("This is message" + str(i)) self.assertEqual(len(messages), TestMessage._msg_count) self.assertIsInstance(messages, list) producer.send(messages) unread_count = producer.get_unread_count( consumer_group=TestMessage._consumer_group) self.assertEqual(unread_count, TestMessage._msg_count) self.assertFalse(unread_count != 100)
class S3CortxMsgBus: def __init__(self): """Init.""" self._producer = None self._consumer = None def setup_producer(self, prod_id, msg_type, method): """Setup producer.""" try: self._producer = MessageProducer(producer_id=prod_id, message_type=msg_type, method=method) except Exception as exception: msg = ("msg_bus setup producer except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, None def send(self, messages): """Send the constructed message.""" try: self._producer.send(messages) except Exception as exception: msg = ("msg_bus send except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, None def purge(self): """Purge/Delete all the messages.""" self._producer.delete() def setup_consumer(self, cons_id, group, msg_type, auto_ack, offset): """Setup the consumer.""" try: self._consumer = MessageConsumer(consumer_id=cons_id, \ consumer_group=group, message_types=[msg_type], auto_ack=auto_ack, offset=offset) except Exception as exception: msg = ("msg_bus setup_consumer except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, None def receive(self, daemon_mode): """Receive the incoming message.""" try: if daemon_mode: #timeout=0 makes it as blocking indefinitely message = self._consumer.receive(timeout=0) else: #timeout = 0.5 sec, by default, which is non-blocking message = self._consumer.receive() except Exception as exception: msg = ("msg_bus receive except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, message def ack(self): """Ack the received message.""" try: self._consumer.ack() except Exception as exception: msg = ("msg_bus ack except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, None def count(self, consumer_group): """Get the count of unread messages.""" unread_count = 0 try: unread_count = self._producer.get_unread_count(consumer_group) except: return 0 return unread_count @staticmethod def create_topic(admin_id: str, message_types: list, partitions: int): """create topic.""" mbadmin = MessageBusAdmin(admin_id = admin_id) try: mbadmin.register_message_type(message_types = message_types, partitions = partitions) except Exception as e: if "TOPIC_ALREADY_EXISTS" not in str(e): raise(e) @staticmethod def add_concurrency(admin_id: str, message_type: str, concurrency_count: int): """Increase partition count for given topic.""" mbadmin = MessageBusAdmin(admin_id = admin_id) mbadmin.add_concurrency(message_type = message_type, concurrency_count = concurrency_count) @staticmethod def delete_topic(admin_id: str, message_types: list): """Delete given topic""" mbadmin = MessageBusAdmin(admin_id = admin_id) mbadmin.deregister_message_type(message_types = message_types) @staticmethod def list_topics(admin_id: str): """list all available topics""" mbadmin = MessageBusAdmin(admin_id = admin_id) return mbadmin.list_message_types() @staticmethod def is_topic_exist(admin_id: str, topic_name: str): """retuns true if topic exist else false""" mbadmin = MessageBusAdmin(admin_id = admin_id) if topic_name in mbadmin.list_message_types(): return True return False