Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 94 lines (67 sloc) 2.869 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
from __future__ import with_statement

import os
import puka
import random

import base


class TestExchange(base.TestCase):
    def test_exchange_redeclare(self):
        client = puka.Client(self.amqp_url)
        promise = client.connect()
        client.wait(promise)

        promise = client.exchange_declare(exchange=self.name)
        r = client.wait(promise)

        promise = client.exchange_declare(exchange=self.name, type='fanout')
        with self.assertRaises(puka.PreconditionFailed):
            client.wait(promise)

        promise = client.exchange_delete(exchange=self.name)
        client.wait(promise)

    def test_exchange_delete_not_found(self):
        client = puka.Client(self.amqp_url)
        promise = client.connect()
        client.wait(promise)

        promise = client.exchange_delete(exchange='not_existing_exchange')

        with self.assertRaises(puka.NotFound):
            client.wait(promise)

    def test_bind(self):
        client = puka.Client(self.amqp_url)
        promise = client.connect()
        client.wait(promise)

        promise = client.exchange_declare(exchange=self.name1, type='fanout')
        client.wait(promise)

        promise = client.exchange_declare(exchange=self.name2, type='fanout')
        client.wait(promise)

        promise = client.queue_declare()
        qname = client.wait(promise)['queue']

        promise = client.queue_bind(queue=qname, exchange=self.name2)
        client.wait(promise)

        promise = client.basic_publish(exchange=self.name1, routing_key='',
                                      body='a')
        client.wait(promise)

        promise = client.exchange_bind(source=self.name1, destination=self.name2)
        client.wait(promise)

        promise = client.basic_publish(exchange=self.name1, routing_key='',
                                      body='b')
        client.wait(promise)

        promise = client.exchange_unbind(source=self.name1,
                                        destination=self.name2)
        client.wait(promise)

        promise = client.basic_publish(exchange=self.name1, routing_key='',
                                      body='c')
        client.wait(promise)

        promise = client.basic_get(queue=qname, no_ack=True)
        r = client.wait(promise)
        self.assertEquals(r['body'], 'b')

        promise = client.basic_get(queue=qname)
        r = client.wait(promise)
        self.assertTrue('empty' in r)

        promise = client.exchange_delete(exchange=self.name1)
        client.wait(promise)
        promise = client.exchange_delete(exchange=self.name2)
        client.wait(promise)
        promise = client.queue_delete(queue=qname)
        client.wait(promise)

        promise = client.close()
        client.wait(promise)


if __name__ == '__main__':
    import tests
    tests.run_unittests(globals())
Something went wrong with that request. Please try again.