Example #1
0
 def setUp(self):
     # creates exchange
     self.worker = amqppy.Worker(broker=BROKER_TEST).\
         add_topic(exchange=EXCHANGE_TEST,
                   routing_key="amqppy.test.topic",
                   exclusive=True,
                   on_topic_callback=callback)
     self.worker.run_async()
     self.worker2 = amqppy.Worker(broker=BROKER_TEST)
Example #2
0
    def setUp(self):
        # creates exchange
        self.worker = amqppy.Worker(broker=BROKER_TEST).\
            add_request(exchange=EXCHANGE_TEST,
                        routing_key="amqppy.test.rpc",
                        on_request_callback=callback,
                        exclusive=True).\
            run_async()

        self.worker2 = amqppy.Worker(broker=BROKER_TEST)
    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_rpc_timeout(self):
        def callback_request(exchange, routing_key, headers, body):
            time.sleep(2)
            return json.dumps(dict(value="Hello budy!"))

        worker = amqppy.Worker(broker=BROKER_TEST).\
            add_request(exchange=EXCHANGE_TEST,
                        routing_key="amqppy.test.rpc",
                        on_request_callback=callback_request).run_async()
        try:
            self.assertRaises(amqppy.ResponseTimeout,
                              lambda:
                              amqppy.Rpc(broker=BROKER_TEST).request(exchange=EXCHANGE_TEST,
                                                                     routing_key="amqppy.test.rpc",
                                                                     body=json.dumps({'msg': 'hello world!, नमस्कार संसार'}),
                                                                     timeout=1))
        finally:
            worker.stop()
    def test_rpc_echo(self):
        def callback_request(exchange, routing_key, headers, body):
            return body

        worker = amqppy.Worker(broker=BROKER_TEST).\
            add_request(exchange=EXCHANGE_TEST,
                        routing_key="amqppy.test.rpc",
                        on_request_callback=callback_request).\
            run_async()
        try:
            message = json.dumps({'msg': 'hello world!, नमस्कार संसार'})

            rpc_reply = amqppy.Rpc(broker=BROKER_TEST).request(exchange=EXCHANGE_TEST,
                                                               routing_key="amqppy.test.rpc",
                                                               body=message)
            print("*** rpc_reply: {}".format(rpc_reply))
            self.assertEqual(message, rpc_reply)
        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()
sys.path.insert(0,
                os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

import amqppy

# firstly, run this
# AMQP Exchange should be of type 'topic'

EXCHANGE_TEST = 'amqppy.test'
BROKER_TEST = 'amqp://*****:*****@localhost:5672//'


def on_topic_status(exchange, routing_key, headers, body):
    print('Received message from topic \'amqppy.publisher.topic.status\': {}'.
          format(body))


try:
    # connect to the broker
    worker = amqppy.Worker(broker=BROKER_TEST)
    # subscribe to a topic: 'amqppy.publisher.topic.status'
    worker.add_topic(exchange=EXCHANGE_TEST,
                     routing_key='amqppy.publisher.topic.status',
                     on_topic_callback=on_topic_status)
    # wait until worker is stopped or an uncaught exception
    print('Waiting for topics events, to cancel press ctrl + c')
    worker.run()
except KeyboardInterrupt:
    worker.stop()
    print('Exiting')
    print('Received event \'amqppy.publisher.topic.datetime\': {}'.format(
        json.loads(body)))


def on_topic_status(exchange, routing_key, headers, body):
    print('Received event \'amqppy.publisher.topic.status\': {}'.format(
        json.loads(body)))


def on_topic_all(exchange, routing_key, headers, body):
    print('Received (*) event \'{}\': {}'.format(routing_key,
                                                 json.loads(body)))


try:
    print('Waiting for publieher topics events, to cancel press ctrl + c')
    worker = amqppy.Worker(broker=BROKER_TEST).\
        add_topic(exchange=EXCHANGE_TEST,
                  routing_key="amqppy.publisher.topic.datetime",
                  on_topic_callback=on_topic_datetime).\
        add_topic(exchange=EXCHANGE_TEST,
                  routing_key="amqppy.publisher.topic.status",
                  on_topic_callback=on_topic_status).\
        add_topic(exchange=EXCHANGE_TEST,
                  routing_key="amqppy.publisher.topic.*",
                  on_topic_callback=on_topic_all).\
        run()
except KeyboardInterrupt:
    worker.stop()
    print('Exiting')
Example #10
0
 def test_worker(self):
     self.assertRaises(amqppy.BrokenConnection,
                       lambda: amqppy.Worker(broker=WRONG_BROKER_TEST))