def test_pub_sub(): """ Test publish-subscribe Create a blocking consumer and a producer. Produce a message and read it from the topic. """ topic_name = "test" consumer = mb.MbConsumer([mb.ConnectionPath(mb.MbChannelType.TOPIC, topic_name)]) producer = mb.MbProducer(mb.ConnectionPath(mb.MbChannelType.TOPIC, topic_name)) time.sleep(1.0) msg = "hello, world!" producer.publish(msg) received_msg = consumer.next_message().content assert received_msg == msg
def test_push_pull(): """ Test push-pull This time create a queue. In a loop push a list of numbers into the queue and on the other side just read them all. """ queue_name = "test" consumer = mb.MbConsumer([mb.ConnectionPath(mb.MbChannelType.QUEUE, queue_name)]) time.sleep(1) producer = mb.MbProducer(mb.ConnectionPath(mb.MbChannelType.QUEUE, queue_name)) for i in range(10): producer.publish(str(i)) for i in range(10): assert i == int(consumer.next_message().content)
def test_multiple_pub_single_sub(): """Test publish-subscribe for multiple publishers and a single consumer.""" producers = [mb.MbProducer(mb.ConnectionPath(mb.MbChannelType.TOPIC, "mpss1")), mb.MbProducer(mb.ConnectionPath(mb.MbChannelType.TOPIC, "mpss2")), mb.MbProducer(mb.ConnectionPath(mb.MbChannelType.TOPIC, "mpss3")), mb.MbProducer(mb.ConnectionPath(mb.MbChannelType.TOPIC, "mpss4"))] time.sleep(1.0) consumer = mb.MbConsumer([mb.ConnectionPath(mb.MbChannelType.TOPIC, "mpss1"), mb.ConnectionPath(mb.MbChannelType.TOPIC, "mpss2"), mb.ConnectionPath(mb.MbChannelType.TOPIC, "mpss3"), mb.ConnectionPath(mb.MbChannelType.TOPIC, "mpss4")]) time.sleep(1.0) for i, p in enumerate(producers): p.publish(str(i+1)) for i in range(4): msg = consumer.next_message() path = msg.path content = msg.content # what is that? the path ends with a number and the message is that number -1 assert int(path[-1]) == int(content)
import mb import time import logging import os logger = logging.getLogger(__file__) LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper() logging.basicConfig(level=LOGLEVEL) time.sleep(20) logger.info("collector I") topic_name = "release-monitoring" consumer = mb.MbConsumer([]) statistics = {"npm-updates": 0} logger.info("collector II") while True: try: consumer.next_message() statistics["npm-updates"] += 1 logger.info("Report: Detected {} NPM updates".format( statistics["npm-updates"])) except KeyboardInterrupt: consumer.disconnect() exit(0)
def create_consumer(): nonlocal topic_name consumer = mb.MbConsumer([mb.ConnectionPath(mb.MbChannelType.TOPIC, topic_name)], durable_subscription_name="durability_test_123456") return consumer
def create_consumer(): consumer = mb.MbConsumer([mb.ConnectionPath(mb.MbChannelType.TOPIC, topic_name)], durable_subscription_name=durable_subscription_name) return consumer