Example #1
0
    def test_basic_publish(self):
        message = str(uuid.uuid4())
        exchange = 'test'
        routing_key = 'hello'
        properties = {'headers': {'key': 'value'}}

        connection = FakeConnection()
        channel = Channel(9, connection, 0.0001)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)
        basic.publish(body=message,
                      routing_key=routing_key,
                      exchange=exchange,
                      properties=properties,
                      mandatory=True,
                      immediate=True)

        channel_id, payload = connection.frames_out.pop()
        basic_publish, content_header, content_body = payload

        # Verify Channel ID
        self.assertEqual(channel_id, 9)

        # Verify Classes
        self.assertIsInstance(basic_publish, spec_basic.Publish)
        self.assertIsInstance(content_header, ContentHeader)
        self.assertIsInstance(content_body, ContentBody)

        # Verify Content
        self.assertEqual(message, content_body.value.decode('utf-8'))
        self.assertEqual(exchange, basic_publish.exchange)
        self.assertEqual(routing_key, basic_publish.routing_key)
        self.assertTrue(basic_publish.immediate)
        self.assertTrue(basic_publish.mandatory)
        self.assertIn('key', dict(content_header.properties)['headers'])
Example #2
0
    def channel(self, rpc_timeout=60, lazy=False):
        """Open a Channel.

        :param int rpc_timeout: Timeout before we give up waiting for an RPC
                                response from the server.

        :raises AMQPInvalidArgument: Invalid Parameters
        :raises AMQPChannelError: Raises if the channel encountered an error.
        :raises AMQPConnectionError: Raises if the connection
                                     encountered an error.

        :rtype: amqpstorm.Channel
        """
        LOGGER.debug('Opening a new Channel')
        if not compatibility.is_integer(rpc_timeout):
            raise AMQPInvalidArgument('rpc_timeout should be an integer')
        elif self.is_closed:
            raise AMQPConnectionError('socket/connection closed')

        with self.lock:
            channel_id = self._get_next_available_channel_id()
            channel = Channel(channel_id,
                              self,
                              rpc_timeout,
                              on_close_impl=self._cleanup_channel)
            self._channels[channel_id] = channel
            if not lazy:
                channel.open()
        LOGGER.debug('Channel #%d Opened', channel_id)
        return self._channels[channel_id]
Example #3
0
    def test_basic_consume_invalid_parameter(self):
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'queue should be a string', basic.consume,
                                None, 1)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'consumer_tag should be a string',
                                basic.consume, None, '', 1)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'exclusive should be a boolean', basic.consume,
                                None, '', '', None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'no_ack should be a boolean', basic.consume,
                                None, '', '', True, None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'no_local should be a boolean', basic.consume,
                                None, '', '', True, True, None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'arguments should be a dict or None',
                                basic.consume, None, '', '', True, True, True,
                                [])
Example #4
0
    def test_basic_publish_invalid_parameter(self):
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'body should be a string', basic.publish, None,
                                '')

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'routing_key should be a string',
                                basic.publish, '', None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'exchange should be a string', basic.publish,
                                '', '', None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'properties should be a dict or None',
                                basic.publish, '', '', '', [])

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'properties should be a dict or None',
                                basic.publish, '', '', '', 1)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'mandatory should be a boolean', basic.publish,
                                '', '', '', {}, None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'immediate should be a boolean', basic.publish,
                                '', '', '', {}, True, None)
Example #5
0
    def channel(self, rpc_timeout=60, lazy=False):
        """Open Channel.

        :param int rpc_timeout: Timeout before we give up waiting for an RPC
                                response from the server.

        :raises AMQPInvalidArgument: Invalid Parameters
        :raises AMQPChannelError: Raises if the channel encountered an error.
        :raises AMQPConnectionError: Raises if the connection
                                     encountered an error.
        """
        LOGGER.debug('Opening a new Channel')
        if not compatibility.is_integer(rpc_timeout):
            raise AMQPInvalidArgument('rpc_timeout should be an integer')
        elif self.is_closed:
            raise AMQPConnectionError('socket/connection closed')

        with self.lock:
            channel_id = self._get_next_available_channel_id()
            channel = Channel(channel_id, self, rpc_timeout,
                              on_close_impl=self._cleanup_channel)
            self._channels[channel_id] = channel
            if not lazy:
                channel.open()
        LOGGER.debug('Channel #%d Opened', channel_id)
        return self._channels[channel_id]
Example #6
0
    def test_tx_with_statement_when_failing(self):
        self._active_transaction = False

        def on_tx(*_):
            if not self._active_transaction:
                channel.rpc.on_frame(specification.Tx.SelectOk())
                self._active_transaction = True
                return
            self._active_transaction = False
            channel.rpc.on_frame(specification.Tx.RollbackOk())

        connection = FakeConnection(on_write=on_tx)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        tx = Tx(channel)

        try:
            with tx:
                self.assertTrue(tx._tx_active)
                raise Exception('error')
        except Exception as why:
            self.assertEqual('error', str(why))

        self.assertFalse(tx._tx_active)
        self.assertEqual(self.get_last_log(),
                         'Leaving Transaction on exception: error')
Example #7
0
    def test_queue_purge_invalid_parameter(self):
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        queue = Queue(channel)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'queue should be a string', queue.purge, None)
Example #8
0
    def test_queue_declare_invalid_parameter(self):
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        queue = Queue(channel)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'queue should be a string', queue.declare,
                                None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'passive should be a boolean', queue.declare,
                                'unittest', None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'durable should be a boolean', queue.declare,
                                'unittest', True, None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'exclusive should be a boolean', queue.declare,
                                'unittest', True, True, None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'auto_delete should be a boolean',
                                queue.declare, 'unittest', True, True, True,
                                None)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'arguments should be a dict or None',
                                queue.declare, 'unittest', True, True, True,
                                True, [])
    def test_exchange_delete_invalid_parameter(self):
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        exchange = Exchange(channel)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'exchange should be a string',
                                exchange.delete, None)
Example #10
0
    def test_basic_cancel_invalid_parameter(self):
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'consumer_tag should be a string',
                                basic.cancel, None)
Example #11
0
    def test_basic_recover_invalid_parameter(self):
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'requeue should be a boolean', basic.recover,
                                None)
Example #12
0
 def test_basic_get_content_body(self):
     message = b'Hello World!'
     body = ContentBody(value=message)
     channel = Channel(0, FakeConnection(), 360)
     channel.set_state(Channel.OPEN)
     basic = Basic(channel)
     uuid = channel.rpc.register_request([body.name])
     channel.rpc.on_frame(body)
     self.assertEqual(basic._get_content_body(uuid, len(message)), message)
Example #13
0
 def test_basic_get_content_body_timeout_error(self):
     message = b'Hello World!'
     body = ContentBody(value=message)
     channel = Channel(0, FakeConnection(), 0.0001)
     channel.set_state(Channel.OPEN)
     basic = Basic(channel)
     uuid = channel.rpc.register_request([body.name])
     self.assertRaises(exception.AMQPChannelError, basic._get_content_body,
                       uuid, len(message))
Example #14
0
 def test_get_content_body_timeout_error(self):
     message = b'Hello World!'
     body = ContentBody(value=message)
     channel = Channel(0, FakeConnection(), 0.0001)
     channel.set_state(Channel.OPEN)
     basic = Basic(channel)
     uuid = channel.rpc.register_request([body.name])
     self.assertRaises(exception.AMQPChannelError, basic._get_content_body,
                       uuid, len(message))
Example #15
0
    def test_basic_get_content_body_break_on_none_value(self):
        body = ContentBody(value=None)
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)
        uuid = channel.rpc.register_request([body.name])
        channel.rpc.on_frame(body)

        self.assertEqual(basic._get_content_body(uuid, 10), b'')
Example #16
0
    def test_basic_get_content_body_break_on_none_value(self):
        body = ContentBody(value=None)
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)
        uuid = channel.rpc.register_request([body.name])
        channel.rpc.on_frame(body)

        self.assertEqual(basic._get_content_body(uuid, 10), b'')
Example #17
0
    def test_exchange_unbind(self):
        def on_unbind(*_):
            channel.rpc.on_frame(pamqp_exchange.UnbindOk())

        connection = FakeConnection(on_write=on_unbind)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        exchange = Exchange(channel)
        self.assertFalse(exchange.unbind())
Example #18
0
    def test_exchange_declare(self):
        def on_declare(*_):
            channel.rpc.on_frame(pamqp_exchange.DeclareOk())

        connection = FakeConnection(on_write=on_declare)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        exchange = Exchange(channel)
        self.assertFalse(exchange.declare())
Example #19
0
    def test_exchange_declare(self):
        def on_declare(*_):
            channel.rpc.on_frame(pamqp_exchange.DeclareOk())

        connection = FakeConnection(on_write=on_declare)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        exchange = Exchange(channel)
        self.assertFalse(exchange.declare())
Example #20
0
    def test_exchange_unbind(self):
        def on_unbind(*_):
            channel.rpc.on_frame(pamqp_exchange.UnbindOk())

        connection = FakeConnection(on_write=on_unbind)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        exchange = Exchange(channel)
        self.assertFalse(exchange.unbind())
Example #21
0
    def test_basic_recover(self):
        def on_recover_frame(*_):
            channel.rpc.on_frame(commands.Basic.RecoverOk())

        connection = FakeConnection(on_write=on_recover_frame)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertEqual(basic.recover(), {})
Example #22
0
    def test_basic_qos(self):
        def on_qos_frame(*_):
            channel.rpc.on_frame(specification.Basic.QosOk())

        connection = FakeConnection(on_write=on_qos_frame)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertEqual(basic.qos(), {})
Example #23
0
    def test_basic_get_content_body(self):
        body = ContentBody(value=self.message.encode('utf-8'))
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)
        uuid = channel.rpc.register_request([body.name])
        channel.rpc.on_frame(body)

        self.assertEqual(basic._get_content_body(uuid, len(self.message)),
                         self.message.encode('utf-8'))
Example #24
0
 def test_get_content_body(self):
     message = b'Hello World!'
     body = ContentBody(value=message)
     channel = Channel(0, FakeConnection(), 360)
     channel.set_state(Channel.OPEN)
     basic = Basic(channel)
     uuid = channel.rpc.register_request([body.name])
     channel.rpc.on_frame(body)
     self.assertEqual(basic._get_content_body(uuid, len(message)),
                      message)
Example #25
0
    def test_basic_get_content_body(self):
        body = ContentBody(value=self.message.encode('utf-8'))
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)
        uuid = channel.rpc.register_request([body.name])
        channel.rpc.on_frame(body)

        self.assertEqual(basic._get_content_body(uuid, len(self.message)),
                         self.message.encode('utf-8'))
Example #26
0
    def test_queue_bind(self):
        def on_bind(*_):
            channel.rpc.on_frame(pamqp_queue.BindOk())

        connection = FakeConnection(on_write=on_bind)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        exchange = Queue(channel)

        self.assertFalse(exchange.bind())
Example #27
0
    def test_queue_bind(self):
        def on_bind(*_):
            channel.rpc.on_frame(pamqp_queue.BindOk())

        connection = FakeConnection(on_write=on_bind)
        channel = Channel(0, connection, 0.1)
        channel.set_state(Channel.OPEN)
        exchange = Queue(channel)

        self.assertFalse(exchange.bind())
Example #28
0
    def test_queue_purge(self):
        def on_purge(*_):
            channel.rpc.on_frame(pamqp_queue.PurgeOk())

        connection = FakeConnection(on_write=on_purge)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        exchange = Queue(channel)

        self.assertEqual(exchange.purge('travis_ci'), {'message_count': 0})
Example #29
0
    def test_queue_purge(self):
        def on_purge(*_):
            channel.rpc.on_frame(pamqp_queue.PurgeOk())

        connection = FakeConnection(on_write=on_purge)
        channel = Channel(0, connection, 0.1)
        channel.set_state(Channel.OPEN)
        exchange = Queue(channel)

        self.assertEqual(exchange.purge(), {'message_count': 0})
Example #30
0
    def test_basic_qos(self):
        def on_qos_frame(*_):
            channel.rpc.on_frame(specification.Basic.QosOk())

        connection = FakeConnection(on_write=on_qos_frame)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertEqual(basic.qos(), {})
Example #31
0
    def test_tx_commit(self):
        def on_tx_commit(*_):
            channel.rpc.on_frame(pamqp_spec.Tx.CommitOk())

        connection = FakeConnection(on_write=on_tx_commit)
        channel = Channel(0, connection, 0.1)
        channel.set_state(Channel.OPEN)
        tx = Tx(channel)

        self.assertIsInstance(tx.commit(), dict)
        self.assertFalse(tx._tx_active)
Example #32
0
    def test_basic_reject(self):
        def on_write(channel, frame):
            self.assertEqual(channel, 9)
            self.assertIsInstance(frame, specification.Basic.Reject)

        connection = FakeConnection(on_write=on_write)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertEqual(basic.reject(), None)
Example #33
0
    def test_tx_select(self):
        def on_tx_select(*_):
            channel.rpc.on_frame(specification.Tx.SelectOk())

        connection = FakeConnection(on_write=on_tx_select)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        tx = Tx(channel)

        self.assertIsInstance(tx.select(), dict)
        self.assertTrue(tx._tx_active)
Example #34
0
    def test_basic_nack(self):
        def on_write(channel, frame):
            self.assertEqual(channel, 9)
            self.assertIsInstance(frame, commands.Basic.Nack)

        connection = FakeConnection(on_write=on_write)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertEqual(basic.nack(), None)
Example #35
0
    def test_tx_rollback(self):
        def on_tx_rollback(*_):
            channel.rpc.on_frame(specification.Tx.RollbackOk())

        connection = FakeConnection(on_write=on_tx_rollback)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        tx = Tx(channel)

        self.assertIsInstance(tx.rollback(), dict)
        self.assertFalse(tx._tx_active)
Example #36
0
    def test_basic_reject(self):
        def on_write(channel, frame):
            self.assertEqual(channel, 9)
            self.assertIsInstance(frame, specification.Basic.Reject)

        connection = FakeConnection(on_write=on_write)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertEqual(basic.reject(), None)
Example #37
0
 def channel(self, rpc_timeout=360):
     """Open Channel."""
     LOGGER.debug('Opening new Channel.')
     if not compatibility.is_integer(rpc_timeout):
         raise AMQPInvalidArgument('rpc_timeout should be an integer')
     with self.io.lock:
         channel_id = len(self._channels) + 1
         channel = Channel(channel_id, self, rpc_timeout)
         self._channels[channel_id] = channel
         channel.open()
     LOGGER.debug('Channel #%d Opened.', channel_id)
     return self._channels[channel_id]
Example #38
0
    def test_basic_reject_invalid_parameter(self):
        channel = Channel(0, FakeConnection(), 360)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'delivery_tag should be an integer or None',
                                basic.reject, 'unittest')

        self.assertRaisesRegexp(exception.AMQPInvalidArgument,
                                'requeue should be a boolean', basic.reject, 1,
                                None)
Example #39
0
    def test_basic_consume(self):
        tag = 'travis-ci'

        def on_consume_frame(*_):
            channel.rpc.on_frame(specification.Basic.ConsumeOk(tag))

        connection = FakeConnection(on_write=on_consume_frame)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertEqual(basic.consume(), tag)
Example #40
0
    def test_basic_consume(self):
        tag = 'travis-ci'

        def on_consume_frame(*_):
            channel.rpc.on_frame(specification.Basic.ConsumeOk(tag))

        connection = FakeConnection(on_write=on_consume_frame)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertEqual(basic.consume(), tag)
Example #41
0
    def test_basic_publish_confirms_nack(self):
        def on_publish_return_nack(*_):
            channel.rpc.on_frame(specification.Basic.Nack())

        connection = FakeConnection(on_write=on_publish_return_nack)
        channel = Channel(9, connection, 1)
        channel._confirming_deliveries = True
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertFalse(
            basic.publish(body=self.message, routing_key='travis-ci'))
Example #42
0
    def test_basic_publish_confirms_nack(self):
        def on_publish_return_nack(*_):
            channel.rpc.on_frame(specification.Basic.Nack())

        connection = FakeConnection(on_write=on_publish_return_nack)
        channel = Channel(9, connection, 1)
        channel._confirming_deliveries = True
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertFalse(basic.publish(body=self.message,
                                       routing_key='travis-ci'))
Example #43
0
    def test_basic_publish_confirms_nack(self):
        message = str(uuid.uuid4())

        def on_publish_return_nack(*_):
            channel.rpc.on_frame(spec_basic.Nack())

        connection = FakeConnection(on_write=on_publish_return_nack)
        channel = Channel(9, connection, 1)
        channel.confirming_deliveries = True
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertFalse(basic.publish(body=message, routing_key='unittest'))
Example #44
0
    def test_basic_get_empty(self):
        def on_get_frame(*_):
            channel.rpc.on_frame(specification.Basic.GetEmpty())

        connection = FakeConnection(on_write=on_get_frame)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        result = basic.get(queue='travis-ci')

        self.assertFalse(channel.rpc._request)
        self.assertFalse(channel.rpc._response)
        self.assertIsNone(result)
Example #45
0
    def test_queue_declare(self):
        def on_declare(*_):
            channel.rpc.on_frame(pamqp_queue.DeclareOk())

        connection = FakeConnection(on_write=on_declare)
        channel = Channel(0, connection, 0.1)
        channel.set_state(Channel.OPEN)
        queue = Queue(channel)

        self.assertEqual(queue.declare(), {
            'queue': '',
            'message_count': 0,
            'consumer_count': 0
        })
Example #46
0
    def test_basic_get_message_empty_queue(self):
        get_frame = specification.Basic.Get(queue='travis-ci',
                                            no_ack=False)

        def on_get_frame(*_):
            channel.rpc.on_frame(specification.Basic.GetEmpty())

        connection = FakeConnection(on_write=on_get_frame)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        result = basic._get_message(get_frame, auto_decode=False)

        self.assertEqual(result, None)
Example #47
0
    def test_queue_declare(self):
        def on_declare(*_):
            channel.rpc.on_frame(pamqp_queue.DeclareOk())

        connection = FakeConnection(on_write=on_declare)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        queue = Queue(channel)

        expected_result = {
            'queue': '',
            'message_count': 0,
            'consumer_count': 0
        }
        self.assertEqual(queue.declare(), expected_result)
Example #48
0
    def test_basic_get_fails(self):
        def on_get_frame(*_):
            pass

        connection = FakeConnection(on_write=on_get_frame)
        channel = Channel(9, connection, 0.1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        self.assertRaisesRegexp(
            AMQPChannelError,
            'rpc requests .* \(.*\) took too long',
            basic.get, 'travis-ci'
        )

        self.assertFalse(channel.rpc._request)
        self.assertFalse(channel.rpc._response)
Example #49
0
    def test_basic_get_to_dict(self):
        message = self.message.encode('utf-8')
        message_len = len(message)

        def on_get_frame(*_):
            channel.rpc.on_frame(specification.Basic.GetOk())
            channel.rpc.on_frame(ContentHeader(body_size=message_len))
            channel.rpc.on_frame(ContentBody(value=message))

        connection = FakeConnection(on_write=on_get_frame)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        result = basic.get(queue='travis-ci', to_dict=True)

        self.assertFalse(channel.rpc._request)
        self.assertFalse(channel.rpc._response)
        self.assertEqual(result['body'], message)
Example #50
0
    def test_tx_with_statement(self):
        self._active_transaction = False

        def on_tx(*_):
            if not self._active_transaction:
                channel.rpc.on_frame(specification.Tx.SelectOk())
                self._active_transaction = True
                return
            self._active_transaction = False
            channel.rpc.on_frame(specification.Tx.CommitOk())

        connection = FakeConnection(on_write=on_tx)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        tx = Tx(channel)

        with tx:
            self.assertTrue(tx._tx_active)
        self.assertFalse(tx._tx_active)
Example #51
0
    def test_basic_get_message_auto_decode(self):
        message = self.message.encode('utf-8')
        message_len = len(message)

        get_frame = specification.Basic.Get(queue='travis-ci',
                                            no_ack=False)

        def on_get_frame(*_):
            channel.rpc.on_frame(specification.Basic.GetOk())
            channel.rpc.on_frame(ContentHeader(body_size=message_len))
            channel.rpc.on_frame(ContentBody(value=message))

        connection = FakeConnection(on_write=on_get_frame)
        channel = Channel(9, connection, 1)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)

        result = basic._get_message(get_frame, auto_decode=True)

        self.assertEqual(result.body.encode('utf-8'), message)
Example #52
0
    def test_basic_consume_rpc(self):
        tag = 'travis-ci'

        def on_publish_return_ack(_, frame):
            self.assertIsInstance(frame, specification.Basic.Consume)
            self.assertEqual(frame.arguments, {})
            self.assertEqual(frame.consumer_tag, tag)
            self.assertEqual(frame.exclusive, True)
            self.assertEqual(frame.no_ack, True)
            self.assertEqual(frame.exclusive, True)
            self.assertEqual(frame.queue, '')
            channel.rpc.on_frame(specification.Basic.ConsumeOk(tag))

        connection = FakeConnection(on_write=on_publish_return_ack)
        channel = Channel(9, connection, 1)
        channel.set_state(channel.OPEN)
        basic = Basic(channel)

        self.assertEqual(
            basic._consume_rpc_request({}, tag, True, True, True, ''),
            {'consumer_tag': 'travis-ci'})
Example #53
0
    def test_tx_with_statement_when_raises(self):
        def on_tx(_, frame):
            if isinstance(frame, specification.Tx.Select):
                channel.rpc.on_frame(specification.Tx.SelectOk())
                return
            channel.rpc.on_frame(specification.Tx.CommitOk())

        connection = FakeConnection(on_write=on_tx)
        channel = Channel(0, connection, 0.01)
        channel.set_state(Channel.OPEN)
        tx = Tx(channel)

        try:
            with tx:
                tx.commit()
                raise Exception('travis-ci')
        except Exception:
            self.assertEqual(self.get_last_log(),
                             'Leaving Transaction on exception: travis-ci')

        self.assertFalse(tx._tx_active)
Example #54
0
    def test_basic_publish(self):
        message = str(uuid.uuid4())
        exchange = 'test'
        routing_key = 'hello'
        properties = {'headers': {
            'key': 'value'
        }}

        connection = FakeConnection()
        channel = Channel(9, connection, 0.0001)
        channel.set_state(Channel.OPEN)
        basic = Basic(channel)
        basic.publish(body=message,
                      routing_key=routing_key,
                      exchange=exchange,
                      properties=properties,
                      mandatory=True,
                      immediate=True)

        channel_id, payload = connection.frames_out.pop()
        basic_publish, content_header, content_body = payload

        # Verify Channel ID
        self.assertEqual(channel_id, 9)

        # Verify Classes
        self.assertIsInstance(basic_publish, spec_basic.Publish)
        self.assertIsInstance(content_header, ContentHeader)
        self.assertIsInstance(content_body, ContentBody)

        # Verify Content
        self.assertEqual(message, content_body.value.decode('utf-8'))
        self.assertEqual(exchange, basic_publish.exchange)
        self.assertEqual(routing_key, basic_publish.routing_key)
        self.assertTrue(basic_publish.immediate)
        self.assertTrue(basic_publish.mandatory)
        self.assertIn('key', dict(content_header.properties)['headers'])
Example #55
0
    def channel(self, rpc_timeout=120):
        """Open Channel.

        :param int rpc_timeout: Timeout before we give up waiting for an RPC
                                response from the server.

        :raises AMQPInvalidArgument: Raises on invalid arguments.
        :raises AMQPChannelError: Raises if the channel cannot be opened
                                  before the rpc_timeout is reached.
        :raises AMQPConnectionError: Raises if the Connection is closed.
        """
        LOGGER.debug('Opening a new Channel')
        if not compatibility.is_integer(rpc_timeout):
            raise AMQPInvalidArgument('rpc_timeout should be an integer')
        elif self.is_closed:
            raise AMQPConnectionError('socket/connection closed')

        with self.lock:
            channel_id = len(self._channels) + 1
            channel = Channel(channel_id, self, rpc_timeout)
            self._channels[channel_id] = channel
            channel.open()
        LOGGER.debug('Channel #%d Opened', channel_id)
        return self._channels[channel_id]