def test_many_topics_same_topic_different_exchanges(self): condition = threading.Event() def callback_topic_one(exchange, routing_key, headers, body): print("*** topic ONE callback, exchange: {}, routing_key: {}, body: {}, headers: {}".format( exchange, routing_key, headers, body)) def callback_topic_two(**kargs): print("*** topic TWO callback, {}".format(kargs)) condition.set() # differents exchange, so routing_key can be the same worker = amqppy.Worker(broker=BROKER_TEST).\ add_topic(exchange=EXCHANGE_TEST + ".1", routing_key="amqppy.test.topic", on_topic_callback=callback_topic_one).\ add_topic(exchange=EXCHANGE_TEST + ".2", routing_key="amqppy.test.topic", on_topic_callback=callback_topic_two).\ run_async() try: # differents exchange, so routing_key can be the same topic = amqppy.Topic(broker=BROKER_TEST) topic.publish(exchange=EXCHANGE_TEST + ".1", routing_key="amqppy.test.topic", body=json.dumps({'msg': 'hello world! 1'})) topic.publish(exchange=EXCHANGE_TEST + ".2", routing_key="amqppy.test.topic", body=json.dumps({'msg': 'hello world! 2'})) condition.wait() finally: worker.stop()
def test_fanout(self): condition1 = threading.Event() condition2 = threading.Event() def callback_worker_one(exchange, routing_key, headers, body): print("*** topic at worker ONE callback, exchange: {}, routing_key: {}, body: {}, headers: {}".format( exchange, routing_key, headers, body)) condition1.set() def callback_worker_two(**kargs): print("*** topic at worker TWO callback, {}".format(kargs)) condition2.set() # same exchange, same routing_key, differents queues. It can be # different workers worker = amqppy.Worker(broker=BROKER_TEST).\ add_topic(exchange=EXCHANGE_TEST, routing_key="amqppy.test.topic", on_topic_callback=callback_worker_one, queue='amqppy.test.worker.1').\ add_topic(exchange=EXCHANGE_TEST, routing_key="amqppy.test.topic", on_topic_callback=callback_worker_two, queue='amqppy.test.worker.2').\ run_async() try: amqppy.Topic(broker=BROKER_TEST).publish(exchange=EXCHANGE_TEST, routing_key="amqppy.test.topic", body=json.dumps({'msg': 'hello world!, नमस्कार संसार'})) condition1.wait() condition2.wait() finally: worker.stop()
def test_topic(self): condition = threading.Event() def callback_topic(exchange, routing_key, headers, body): print("*** topic callback, exchange: {}, routing_key: {}, body: {}, headers: {}".format( exchange, routing_key, headers, body)) condition.set() worker = amqppy.Worker(broker=BROKER_TEST).\ add_topic(exchange=EXCHANGE_TEST, routing_key="amqppy.test.topic", on_topic_callback=callback_topic).\ run_async() try: amqppy.Topic(broker=BROKER_TEST).publish(exchange=EXCHANGE_TEST, routing_key="amqppy.test.topic", body=json.dumps({'msg': 'hello world!, नमस्कार संसार'})) condition.wait() finally: worker.stop()
# -*- encoding: utf-8 -*- import sys import os # add amqppy path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) import amqppy # IMPORTANT: firstly, run 'topic_consumer.py' # AMQP Exchange should be of type 'topic' EXCHANGE_TEST = 'amqppy.test' BROKER_TEST = 'amqp://*****:*****@localhost:5672//' try: # publish my current status amqppy.Topic(broker=BROKER_TEST).publish( exchange=EXCHANGE_TEST, routing_key='amqppy.publisher.topic.status', body='RUNNING') print('Topics successfully published.') except (amqppy.ExchangeNotFound, amqppy.PublishNotRouted): print('\'01_topic_consumer.py\' should be running before this.')
def test_exchange_not_found_topic(self): self.assertRaises(amqppy.ExchangeNotFound, lambda: amqppy.Topic(broker=BROKER_TEST).publish(exchange=EXCHANGE_TEST, routing_key="amqppy.test.topic", body=json.dumps({'msg': 'hello world!'})))
def test_topic(self): self.assertRaises(amqppy.BrokenConnection, lambda: amqppy.Topic(broker=WRONG_BROKER_TEST).publish(exchange=EXCHANGE_TEST, routing_key="amqppy.test.topic", body=json.dumps({'msg': 'hello world!'})))
import datetime # add amqppy path sys.path.insert(0, os.path.abspath( os.path.join(os.path.dirname(__file__), '..'))) import amqppy # IMPORTANT: firstly, run 'topic_consumer.py' # AMQP Exchange should be of type 'topic' EXCHANGE_TEST = 'amqppy.test' BROKER_TEST = 'amqp://*****:*****@localhost:5672//' try: # do all with only one connection topic = amqppy.Topic(broker=BROKER_TEST) # publish my current time topic.publish(exchange=EXCHANGE_TEST, routing_key='amqppy.publisher.topic.datetime', body=json.dumps({'datetime': datetime.datetime.now().isoformat()})) # publish my current status topic.publish(exchange=EXCHANGE_TEST, routing_key='amqppy.publisher.topic.status', body=json.dumps({'status': 'working'})) print('Topics successfully published.') except (amqppy.ExchangeNotFound, amqppy.PublishNotRouted): print('\'02_topic_consumer.py\' should be running before this.')