Ejemplo n.º 1
0
    def test_with(self):
        with Connection(**settings.connect_args) as conn:
            self.assertEqual(conn.transport is None, False)

            with conn.channel(1) as ch:
                self.assertEqual(1 in conn.channels, True)

                #
                # Do something with the channel
                #
                ch.access_request('/data', active=True, write=True)
                ch.exchange_declare('unittest.fanout', 'fanout', auto_delete=True)

                msg = Message('unittest message',
                    content_type='text/plain',
                    application_headers={'foo': 7, 'bar': 'baz'})

                ch.basic_publish(msg, 'unittest.fanout')

            #
            # check that the channel was closed
            #
            self.assertEqual(1 in conn.channels, False)
            self.assertEqual(ch.is_open, False)

        #
        # Check that the connection was closed
        #
        self.assertEqual(conn.transport, None)
Ejemplo n.º 2
0
def _consume(n, Connection, name):
    conn = Connection()
    chan = conn.channel()
    chan = _declare(chan, name)

    i = [0]

    def callback(message):
        assert len(message.body) > 10
        i[0] += 1
        if not i[0] % 10000:
            print(i[0])
        deserialize(message.body)
        #chan.basic_ack(message.delivery_info["delivery_tag"])

    chan.basic_consume(queue=name, no_ack=True, callback=callback)

    if hasattr(conn, "drain_events"):
        wait = conn.drain_events
    else:
        wait = chan.wait

    start = time()
    while i[0] < n:
        wait()
    print(time() - start)

    chan.close()
    conn.close()
Ejemplo n.º 3
0
def _publish(n, Connection, Message, name):
    props = {"delivery_mode": DM}
    kwargs = {"properties": props}
    if "amqplib" in repr(Message):
        kwargs = props
    _cleanup(Connection, name)
    conn = Connection()
    chan = conn.channel()
    chan = _declare(chan, name)

    if name == 'librabbitmq':
        _Message = lambda: (serialize(MESSAGE), props)
    else:
        _Message = lambda: Message(serialize(MESSAGE), **kwargs)

    start = time()
    for i in xrange(n + 2000):
        if not i % 10000:
            print(i)
        message = _Message()
        chan.basic_publish(message, exchange=name, routing_key=name)
    print(time() - start)
Ejemplo n.º 4
0
 def setUp(self):
     self.conn = Connection(**settings.connect_args)
     self.ch = self.conn.channel()
Ejemplo n.º 5
0
 def setUp(self):
     self.conn = Connection(**settings.connect_args)