Пример #1
0
    def test_connection_cleanup_one_channel(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection._channels[1] = Channel(1, connection, 0.1)

        connection._cleanup_channel(1)

        self.assertFalse(connection._channels)
Пример #2
0
    def test_connection_cleanup_channel_does_not_exist(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection._channels[1] = Channel(1, connection, 0.1)

        connection._cleanup_channel(2)

        self.assertEqual(len(connection._channels), 1)
Пример #3
0
def consumer():
    connection = Connection(HOST, USERNAME, PASSWORD)
    channel = connection.channel()

    # Declare a queue.
    channel.queue.declare(QUEUE_NAME)

    # Publish something we can get.
    channel.basic.publish(body='Hello World!', routing_key=QUEUE_NAME,
                          properties={'content_type': 'text/plain'})

    # Retrieve a single message.
    result = channel.basic.get(queue=QUEUE_NAME, no_ack=False)
    if result:
        # If we got a message, handle it.
        print('Message:', result['body'])

        # Mark the message as handle.
        channel.basic.ack(delivery_tag=result['method']['delivery_tag'])
    else:
        # The queue was empty.
        print("Queue '{0}' Empty.".format(QUEUE_NAME))

    channel.close()
    connection.close()
Пример #4
0
class GetAndRedeliverTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.declare('test.get.redeliver')
        self.channel.queue.purge('test.get.redeliver')
        self.channel.confirm_deliveries()
        self.message = str(uuid.uuid4())
        self.channel.basic.publish(body=self.message,
                                   routing_key='test.get.redeliver')
        message = self.channel.basic.get('test.get.redeliver', no_ack=False,
                                         to_dict=False)
        message.reject()
        # Sleep for 0.5s to make sure RabbitMQ has time to catch up.
        time.sleep(0.5)

    def test_get_and_redeliver(self):
        message = self.channel.basic.get('test.get.redeliver', no_ack=False,
                                         to_dict=False)
        self.assertEqual(message.body, self.message)

    def tearDown(self):
        self.channel.queue.delete('test.get.redeliver')
        self.channel.close()
        self.connection.close()
Пример #5
0
    def test_api_connection_close(self):
        reason = 'travis-ci'
        api = ManagementApi(HTTP_URL, USERNAME, PASSWORD, timeout=1)

        self.assertEqual(len(api.connection.list()), 0,
                         'found an open connection, test will fail')

        connection = Connection(HOST, USERNAME, PASSWORD, timeout=1)

        connections = retry_function_wrapper(api.connection.list)
        self.assertIsNotNone(connections)

        self.assertEqual(len(connections), 1)

        for conn in api.connection.list():
            self.assertEqual(api.connection.close(conn['name'],
                                                  reason=reason), None)

        time.sleep(1)

        self.assertRaisesRegexp(
            AMQPConnectionError,
            'Connection was closed by remote server: '
            'CONNECTION_FORCED - %s' % reason,
            connection.check_for_errors
        )

        self.assertEqual(len(api.connection.list()), 0)

        connection.close()
Пример #6
0
class PublishAndGetLargeMessageTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.confirm_deliveries()
        self.channel.queue.declare('test.basic.get_large')
        self.channel.queue.purge('test.basic.get_large')

    def test_publish_and_get_large_message(self):
        try:
            body = str(uuid.uuid4()) * 65536

            # Publish a single large message
            self.channel.basic.publish(body=body,
                                       routing_key='test.basic.get_large')

            payload = self.channel.basic.get('test.basic.get_large',
                                             to_dict=False)
            self.assertEqual(body, payload.body)
        finally:
            self.channel.queue.purge('test.basic.get_large')

    def tearDown(self):
        self.channel.queue.delete('test.basic.get_large')
        self.channel.close()
        self.connection.close()
Пример #7
0
 def test_connection_get_first_channel_id(self):
     connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
     self.assertEqual(connection._last_channel_id, None)
     self.assertEqual(
         connection._get_next_available_channel_id(), 1
     )
     self.assertEqual(connection._last_channel_id, 1)
Пример #8
0
class Publish50kTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.declare('test.basic.50k')
        self.channel.queue.purge('test.basic.50k')

    def test_publish_50k_messages(self):
        body = str(uuid.uuid4())
        # Publish 50k Messages.
        start_time = time.time()
        for _ in range(50000):
            self.channel.basic.publish(body=body,
                                       routing_key='test.basic.50k')
        end_time = time.time() - start_time

        # Sleep for 2.5s to make sure RabbitMQ has time to catch up.
        time.sleep(2.5)

        result = self.channel.queue.declare(queue='test.basic.50k',
                                            passive=True)
        LOGGER.info('Published 50k messages in %d', round(end_time, 3))
        self.assertEqual(result['message_count'], 50000)

    def tearDown(self):
        self.channel.queue.delete('test.basic.50k')
        self.channel.close()
        self.connection.close()
Пример #9
0
class PublishLargeMessagesAndGetTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.confirm_deliveries()
        self.channel.queue.declare('test.basic.large_messages')
        self.channel.queue.purge('test.basic.large_messages')

    def test_publish_5_large_messages(self):
        body = str(uuid.uuid4()) * 8192
        messages_to_publish = 5

        # Publish 5 Messages.
        for _ in range(messages_to_publish):
            self.channel.basic.publish(body=body,
                                       routing_key='test.basic.large_messages')

        inbound_messages = []
        for _ in range(messages_to_publish):
            message = self.channel.basic.get('test.basic.large_messages',
                                             no_ack=True, to_dict=False)
            self.assertEqual(message.body, body)
            inbound_messages.append(message)
        self.assertEqual(len(inbound_messages), messages_to_publish)

    def tearDown(self):
        self.channel.queue.delete('test.basic.large_messages')
        self.channel.close()
        self.connection.close()
Пример #10
0
class PublishFailAndFix(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.confirm_deliveries()

    def test_publish_and_confirm(self):
        try:
            self.channel.basic.publish(body=str(uuid.uuid4()),
                                       routing_key='test.publish.fail.and.fix',
                                       mandatory=True)
        except AMQPChannelError as why:
            self.assertTrue(self.channel.is_open)
            self.assertEqual(why.error_code, 312)
            if why.error_code == 312:
                self.channel.queue.declare('test.publish.fail.and.fix')

        result = \
            self.channel.basic.publish(body=str(uuid.uuid4()),
                                       routing_key='test.publish.fail.and.fix',
                                       mandatory=True)
        self.assertTrue(result)

        payload = self.channel.queue.declare('test.publish.fail.and.fix',
                                             passive=True)
        self.assertEqual(payload['message_count'], 1)

    def tearDown(self):
        self.channel.queue.delete('test.publish.fail.and.fix')
        self.channel.close()
        self.connection.close()
Пример #11
0
    def test_connection_close_when_already_closed(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection.set_state(connection.OPEN)
        io = IO(connection.parameters, [])
        io.socket = Mock(name='socket', spec=socket.socket)
        connection._io = io

        connection.set_state(connection.CLOSED)

        # Create some fake channels.
        for index in range(10):
            connection._channels[index + 1] = Channel(
                index + 1, connection, 360)

        def state_set(state):
            self.assertEqual(state, connection.CLOSED)

        connection.set_state = state_set

        self.assertTrue(connection.is_closed)

        connection.close()

        # Make sure all the fake channels were closed as well.
        for index in range(10):
            self.assertNotIn(index + 1, connection._channels)

        self.assertFalse(connection._channels)
        self.assertTrue(connection.is_closed)
Пример #12
0
class PublishAndGetMessagesTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.declare('test.basic.get')
        self.channel.queue.purge('test.basic.get')

    def test_publish_and_get_five_messages(self):
        # Publish 5 Messages.
        for _ in range(5):
            self.channel.basic.publish(body=str(uuid.uuid4()),
                                       routing_key='test.basic.get')

        # Sleep for 0.5s to make sure RabbitMQ has time to catch up.
        time.sleep(0.5)

        # Get 5 messages.
        for _ in range(5):
            payload = self.channel.basic.get('test.basic.get', to_dict=False)
            self.assertIsInstance(payload, Message)

    def tearDown(self):
        self.channel.queue.delete('test.basic.get')
        self.channel.close()
        self.connection.close()
Пример #13
0
    def test_connection_handle_amqp_frame_none_returns_none(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        result = connection._handle_amqp_frame('')

        self.assertEqual(result[0], '')
        self.assertIsNone(result[1])
        self.assertIsNone(result[2])
Пример #14
0
class GeneratorConsumeMessagesTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.declare('test.basic.generator')
        self.channel.queue.purge('test.basic.generator')
        self.channel.confirm_deliveries()
        for _ in range(5):
            self.channel.basic.publish(body=str(uuid.uuid4()),
                                       routing_key='test.basic.generator')
        self.channel.basic.consume(queue='test.basic.generator',
                                   no_ack=True)
        # Sleep for 0.5s to make sure RabbitMQ has time to catch up.
        time.sleep(0.5)

    def test_generator_consume(self):
        # Store and inbound messages.
        inbound_messages = []
        for message in \
                self.channel.build_inbound_messages(break_on_empty=True):
            self.assertIsInstance(message, Message)
            inbound_messages.append(message)

        # Make sure all five messages were downloaded.
        self.assertEqual(len(inbound_messages), 5)

    def tearDown(self):
        self.channel.queue.delete('test.basic.generator')
        self.channel.close()
        self.connection.close()
Пример #15
0
    def test_connection_handle_unicode_error(self):
        """This test covers an unlikely issue triggered by network corruption.

            pamqp.decode._maybe_utf8 raises:
                UnicodeDecodeError: 'utf8' codec can't
                decode byte 0xc5 in position 1: invalid continuation byte

            The goal here is not to fix issues caused by network corruption,
            but rather to make sure that the exceptions raised when
            connections do fail are always predictable.

            Fail fast and reliably!

        :return:
        """
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)

        def throw_error(_):
            raise UnicodeDecodeError(str(), bytes(), 1, 1, str())

        restore_func = pamqp_frame.unmarshal
        try:
            pamqp_frame.unmarshal = throw_error

            result = connection._handle_amqp_frame('travis-ci')

            self.assertEqual(result[0], 'travis-ci')
            self.assertIsNone(result[1])
            self.assertIsNone(result[2])
        finally:
            pamqp_frame.unmarshal = restore_func

        self.assertEqual(self.get_last_log(),
                         "'' codec can't decode bytes in position 1-0: ")
Пример #16
0
    def test_connection_handle_value_error(self):
        """This test covers an unlikely issue triggered by network corruption.

            pamqp.decode._embedded_value raises:
                ValueError: Unknown type: b'\x13'

            The goal here is not to fix issues caused by network corruption,
            but rather to make sure that the exceptions raised when
            connections do fail are always predictable.

            Fail fast and reliably!

        :return:
        """
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)

        def throw_error(_):
            raise ValueError("Unknown type: b'\x13'")

        restore_func = pamqp_frame.unmarshal
        try:
            pamqp_frame.unmarshal = throw_error

            result = connection._handle_amqp_frame('travis-ci')

            self.assertEqual(result[0], 'travis-ci')
            self.assertIsNone(result[1])
            self.assertIsNone(result[2])
        finally:
            pamqp_frame.unmarshal = restore_func

        self.assertEqual(self.get_last_log(),
                         "Unknown type: b'\x13'")
Пример #17
0
class PublishWithPropertiesAndGetTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.declare('test.basic.properties')
        self.channel.queue.purge('test.basic.properties')
        self.channel.confirm_deliveries()

    def test_publish_with_properties_and_get(self):
        app_id = 'travis-ci'.encode('utf-8')
        properties = {
            'headers': {
                'key': 1234567890,
                'alpha': 'omega'
            }
        }

        message = Message.create(self.channel,
                                 body=str(uuid.uuid4()),
                                 properties=properties)
        # Assign Property app_id
        message.app_id = app_id

        # Check that it was set correctly.
        self.assertEqual(message.properties['app_id'], app_id)

        # Get Property Correlation Id
        correlation_id = message.correlation_id

        # Publish Message
        message.publish(routing_key='test.basic.properties')

        # Sleep for 0.5s to make sure RabbitMQ has time to catch up.
        time.sleep(0.5)

        # New way
        payload = self.channel.basic.get('test.basic.properties',
                                         to_dict=False)
        self.assertEqual(payload.properties['headers']['key'], 1234567890)
        self.assertEqual(payload.properties['headers']['alpha'], 'omega')
        self.assertEqual(payload.app_id, app_id.decode('utf-8'))
        self.assertEqual(payload.correlation_id, correlation_id)
        self.assertIsInstance(payload.properties['app_id'], str)
        self.assertIsInstance(payload.properties['correlation_id'], str)

        # Old way
        result = payload.to_dict()
        self.assertEqual(result['properties']['headers'][b'key'], 1234567890)
        self.assertEqual(result['properties']['headers'][b'alpha'], b'omega')
        self.assertIsInstance(result['properties']['app_id'], bytes)
        self.assertIsInstance(result['properties']['correlation_id'], bytes)
        self.assertEqual(result['properties']['app_id'], app_id)
        self.assertEqual(result['properties']['correlation_id'],
                         correlation_id.encode('utf-8'))

    def tearDown(self):
        self.channel.queue.delete('test.basic.properties')
        self.channel.close()
        self.connection.close()
Пример #18
0
    def test_connection_closed_on_exception(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection.set_state(connection.OPEN)
        connection.exceptions.append(AMQPConnectionError('travis-ci'))

        self.assertTrue(connection.is_open)
        self.assertRaises(AMQPConnectionError, connection.check_for_errors)
        self.assertTrue(connection.is_closed)
def consumer():
    connection = Connection('127.0.0.1', 'guest', 'guest',
                            ssl=True, port=5671)
    channel = connection.channel()
    channel.queue.declare('simple_queue')
    channel.basic.publish(body='Hello World!', routing_key='simple_queue')
    channel.close()
    connection.close()
Пример #20
0
    def test_connection_fileno_property(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection.set_state(connection.OPENING)
        io = IO(connection.parameters, [])
        io.socket = MagicMock(name='socket', spec=socket.socket)
        connection._io = io
        io.socket.fileno.return_value = 5

        self.assertEqual(connection.fileno, 5)
Пример #21
0
    def test_connection_send_handshake(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)

        def on_write_to_socket(message):
            self.assertEqual(message, b'AMQP\x00\x00\t\x01')

        connection._io.write_to_socket = on_write_to_socket

        self.assertIsNone(connection._send_handshake())
Пример #22
0
 def test_connection_get_channel_ids(self):
     connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
     self.assertEqual(connection._last_channel_id, None)
     connection._channels[1] = None
     for index in range(2, 100):
         channel_id = connection._get_next_available_channel_id()
         connection._channels[channel_id] = None
         self.assertEqual(channel_id, index)
         self.assertEqual(connection._last_channel_id, index)
Пример #23
0
 def setUp(self):
     self.connection = Connection(HOST, USERNAME, PASSWORD)
     self.channel = self.connection.channel()
     self.channel.queue.declare('test.basic.resend')
     self.channel.queue.purge('test.basic.resend')
     self.channel.confirm_deliveries()
     message = Message.create(self.channel, body=str(uuid.uuid4()))
     message.app_id = 'travis-ci'
     message.publish('test.basic.resend')
Пример #24
0
    def test_connection_closed_on_exception(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', timeout=1,
                                lazy=True)
        connection.set_state(connection.OPEN)
        connection.exceptions.append(AMQPConnectionError('error'))

        self.assertTrue(connection.is_open)
        self.assertRaises(AMQPConnectionError, connection.check_for_errors)
        self.assertTrue(connection.is_closed)
Пример #25
0
    def test_connection_wait_for_connection_does_raise_on_error(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection.set_state(connection.OPENING)

        connection.exceptions.append(AMQPConnectionError('travis-ci'))

        self.assertRaises(AMQPConnectionError,
                          connection._wait_for_connection_state,
                          connection.OPEN)
Пример #26
0
    def test_connection_send_handshake(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)

        def on_write_to_socket(message):
            self.assertEqual(message, b'AMQP\x00\x00\t\x01')

        connection._io.write_to_socket = on_write_to_socket

        self.assertIsNone(connection._send_handshake())
Пример #27
0
    def test_connection_fileno_property(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection.set_state(connection.OPENING)
        io = IO(connection.parameters, [])
        io.socket = Mock(name='socket', spec=socket.socket)
        connection._io = io
        io.socket.fileno.return_value = 5

        self.assertEqual(connection.fileno, 5)
Пример #28
0
class PublishMessageAndResend(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.declare('test.basic.resend')
        self.channel.queue.purge('test.basic.resend')
        self.channel.confirm_deliveries()
        message = Message.create(self.channel, body=str(uuid.uuid4()))
        message.app_id = 'travis-ci'
        message.publish('test.basic.resend')

    def test_publish_with_properties_and_get(self):
        message = self.channel.basic.get('test.basic.resend',
                                         to_dict=False,
                                         no_ack=True)

        # Check original app_id
        self.assertEqual(message.app_id, 'travis-ci')

        # Assign Property app_id
        app_id = 'travis-ci-2'.encode('utf-8')
        message.app_id = app_id

        # Check that it was set correctly.
        self.assertEqual(message.properties['app_id'], app_id)

        # Get Property Correlation Id
        correlation_id = message.correlation_id

        # Publish Message
        message.publish(routing_key='test.basic.resend')

        # Sleep for 0.5s to make sure RabbitMQ has time to catch up.
        time.sleep(0.5)

        # New way
        payload = self.channel.basic.get('test.basic.resend',
                                         to_dict=False,
                                         no_ack=True)
        self.assertEqual(payload.app_id, app_id.decode('utf-8'))
        self.assertEqual(payload.correlation_id, correlation_id)
        self.assertIsInstance(payload.properties['app_id'], str)
        self.assertIsInstance(payload.properties['correlation_id'], str)

        # Old way
        result = payload.to_dict()
        self.assertIsInstance(result['properties']['app_id'], bytes)
        self.assertIsInstance(result['properties']['correlation_id'], bytes)
        self.assertEqual(result['properties']['app_id'], app_id)
        self.assertEqual(result['properties']['correlation_id'],
                         correlation_id.encode('utf-8'))

    def tearDown(self):
        self.channel.queue.delete('test.basic.resend')
        self.channel.close()
        self.connection.close()
Пример #29
0
    def test_connection_wait_for_connection_does_raise_on_error(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection.set_state(connection.OPENING)

        connection.exceptions.append(AMQPConnectionError('travis-ci'))

        self.assertRaises(
            AMQPConnectionError, connection._wait_for_connection_state,
            connection.OPEN, 0.1
        )
Пример #30
0
    def test_connection_wait_for_connection_raises_on_timeout(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', timeout=0.1,
                                lazy=True)
        connection.set_state(connection.OPENING)
        io = IO(connection.parameters, [])
        io.socket = MagicMock(name='socket', spec=socket.socket)
        connection._io = io

        self.assertRaises(AMQPConnectionError,
                          connection._wait_for_connection_to_open)
Пример #31
0
    def test_connection_basic_handle_amqp_frame(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        payload = (b'\x01\x00\x00\x00\x00\x00\x0c\x00\n\x00\x1e\x00\x00\x00'
                   b'\x02\x00\x00\x00<\xce')

        data_in, channel_id, frame_in = connection._handle_amqp_frame(payload)

        self.assertEqual(data_in, b'')
        self.assertEqual(channel_id, 0)
        self.assertIsInstance(frame_in, specification.Connection.Tune)
Пример #32
0
    def test_connection_open_many_channels(self):
        connection = Connection('127.0.0.1',
                                'guest',
                                'guest',
                                timeout=0.1,
                                lazy=True)
        connection.set_state(connection.OPEN)

        for index in range(MAX_CHANNELS - 1):
            self.assertEqual(int(connection.channel(lazy=True)), index + 1)
Пример #33
0
    def test_connection_wait_for_connection_raises_on_timeout(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', timeout=1,
                                lazy=True)
        connection.set_state(connection.OPENING)
        io = IO(connection.parameters, [])
        io.socket = MagicMock(name='socket', spec=socket.socket)
        connection._io = io

        self.assertRaises(AMQPConnectionError,
                          connection._wait_for_connection_to_open)
Пример #34
0
    def test_connection_cleanup_multiple_channels(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)

        for index in range(1, 10):
            connection._channels[index] = Channel(index, connection, 0.1)

        for index in range(1, 10):
            connection._cleanup_channel(index)

        self.assertFalse(connection._channels)
Пример #35
0
class PublishMessageAndResend(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.declare('test.basic.resend')
        self.channel.queue.purge('test.basic.resend')
        self.channel.confirm_deliveries()
        message = Message.create(self.channel,
                                 body=str(uuid.uuid4()))
        message.app_id = 'travis-ci'
        message.publish('test.basic.resend')

    def test_publish_with_properties_and_get(self):
        message = self.channel.basic.get('test.basic.resend',
                                         to_dict=False, no_ack=True)

        # Check original app_id
        self.assertEqual(message.app_id, 'travis-ci')

        # Assign Property app_id
        app_id = 'travis-ci-2'.encode('utf-8')
        message.app_id = app_id

        # Check that it was set correctly.
        self.assertEqual(message.properties['app_id'], app_id)

        # Get Property Correlation Id
        correlation_id = message.correlation_id

        # Publish Message
        message.publish(routing_key='test.basic.resend')

        # Sleep for 0.5s to make sure RabbitMQ has time to catch up.
        time.sleep(0.5)

        # New way
        payload = self.channel.basic.get('test.basic.resend',
                                         to_dict=False, no_ack=True)
        self.assertEqual(payload.app_id, app_id.decode('utf-8'))
        self.assertEqual(payload.correlation_id, correlation_id)
        self.assertIsInstance(payload.properties['app_id'], str)
        self.assertIsInstance(payload.properties['correlation_id'], str)

        # Old way
        result = payload.to_dict()
        self.assertIsInstance(result['properties']['app_id'], bytes)
        self.assertIsInstance(result['properties']['correlation_id'], bytes)
        self.assertEqual(result['properties']['app_id'], app_id)
        self.assertEqual(result['properties']['correlation_id'],
                         correlation_id.encode('utf-8'))

    def tearDown(self):
        self.channel.queue.delete('test.basic.resend')
        self.channel.close()
        self.connection.close()
Пример #36
0
class QueueFunctionalTests(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()

    def test_functional_queue_declare(self):
        self.channel.queue.declare('test_functional_queue_declare',
                                   passive=False,
                                   durable=True,
                                   auto_delete=True)
        self.channel.queue.declare('test_functional_queue_declare',
                                   passive=True)

    def test_functional_queue_delete(self):
        self.channel.queue.declare('test_functional_queue_delete')
        self.channel.queue.delete('test_functional_queue_delete',
                                  if_unused=True)
        self.assertRaises(amqpstorm.AMQPChannelError,
                          self.channel.queue.declare,
                          'test_functional_queue_delete',
                          passive=True)

    def test_functional_queue_purge(self):
        payload = 'hello world'
        queue = 'test_functional_functional_queue_purge'
        messages_to_send = 10
        self.channel.queue.declare(queue, auto_delete=True)
        for _ in range(messages_to_send):
            self.channel.basic.publish(payload, queue)
        result = self.channel.queue.purge(queue)
        self.assertEqual(result['message_count'], messages_to_send)

    def test_functional_queue_bind(self):
        queue = 'test_functional_queue_bind'
        self.channel.queue.declare(queue,
                                   passive=False,
                                   durable=True,
                                   auto_delete=True)
        self.assertEqual(
            self.channel.queue.bind(queue=queue, exchange='amq.direct'), {})

    def test_functional_queue_unbind(self):
        queue = 'test_functional_queue_unbind'
        self.channel.queue.declare(queue,
                                   passive=False,
                                   durable=True,
                                   auto_delete=True)
        self.channel.queue.bind(queue=queue, exchange='amq.direct')
        self.assertEqual(
            self.channel.queue.unbind(queue=queue, exchange='amq.direct'), {})

    def tearDown(self):
        self.channel.close()
        self.connection.close()
Пример #37
0
def consumer():
    connection = Connection(HOST, USERNAME, PASSWORD, ssl=True, port=5671,
                            ssl_options={
                                'ssl_version': ssl.PROTOCOL_TLSv1,
                                'cert_reqs': ssl.CERT_NONE
                            })
    channel = connection.channel()
    channel.queue.declare('simple_queue')
    channel.basic.publish(body='Hello World!', routing_key='simple_queue')
    channel.close()
    connection.close()
Пример #38
0
class PublishWithPropertiesAndGetTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.declare('test.basic.properties')
        self.channel.queue.purge('test.basic.properties')
        self.channel.confirm_deliveries()

    def test_publish_with_properties_and_get(self):
        app_id = 'travis-ci'.encode('utf-8')
        properties = {'headers': {'key': 1234567890, 'alpha': 'omega'}}

        message = Message.create(self.channel,
                                 body=str(uuid.uuid4()),
                                 properties=properties)
        # Assign Property app_id
        message.app_id = app_id

        # Check that it was set correctly.
        self.assertEqual(message.properties['app_id'], app_id)

        # Get Property Correlation Id
        correlation_id = message.correlation_id

        # Publish Message
        message.publish(routing_key='test.basic.properties')

        # Sleep for 0.5s to make sure RabbitMQ has time to catch up.
        time.sleep(0.5)

        # New way
        payload = self.channel.basic.get('test.basic.properties',
                                         to_dict=False)
        self.assertEqual(payload.properties['headers']['key'], 1234567890)
        self.assertEqual(payload.properties['headers']['alpha'], 'omega')
        self.assertEqual(payload.app_id, app_id.decode('utf-8'))
        self.assertEqual(payload.correlation_id, correlation_id)
        self.assertIsInstance(payload.properties['app_id'], str)
        self.assertIsInstance(payload.properties['correlation_id'], str)

        # Old way
        result = payload.to_dict()
        self.assertEqual(result['properties']['headers'][b'key'], 1234567890)
        self.assertEqual(result['properties']['headers'][b'alpha'], b'omega')
        self.assertIsInstance(result['properties']['app_id'], bytes)
        self.assertIsInstance(result['properties']['correlation_id'], bytes)
        self.assertEqual(result['properties']['app_id'], app_id)
        self.assertEqual(result['properties']['correlation_id'],
                         correlation_id.encode('utf-8'))

    def tearDown(self):
        self.channel.queue.delete('test.basic.properties')
        self.channel.close()
        self.connection.close()
Пример #39
0
    def test_connection_heartbeat_stopped_on_close(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection.set_state(connection.OPEN)
        connection.heartbeat.start(connection.exceptions)
        connection.exceptions.append(AMQPConnectionError('travis-ci'))

        self.assertTrue(connection.heartbeat._running.is_set())

        self.assertRaises(AMQPConnectionError, connection.check_for_errors)

        self.assertFalse(connection.heartbeat._running.is_set())
Пример #40
0
 def test_connection_get_channel_ids(self):
     connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
     self.assertEqual(connection._last_channel_id, None)
     connection._channels[1] = None
     for index in range(2, 100):
         channel_id = connection._get_next_available_channel_id()
         connection._channels[channel_id] = None
         self.assertEqual(
             channel_id, index
         )
         self.assertEqual(connection._last_channel_id, index)
Пример #41
0
 def setUp(self):
     self.connection = Connection(HOST, USERNAME, PASSWORD)
     self.channel = self.connection.channel()
     self.channel.queue.declare('test.basic.generator')
     self.channel.queue.purge('test.basic.generator')
     self.channel.confirm_deliveries()
     for _ in range(5):
         self.channel.basic.publish(body=str(uuid.uuid4()),
                                    routing_key='test.basic.generator')
     self.channel.basic.consume(queue='test.basic.generator', no_ack=True)
     # Sleep for 0.5s to make sure RabbitMQ has time to catch up.
     time.sleep(0.5)
Пример #42
0
    def test_connection_basic_handle_amqp_frame(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        payload = (
            b'\x01\x00\x00\x00\x00\x00\x0c\x00\n\x00\x1e\x00\x00\x00'
            b'\x02\x00\x00\x00<\xce'
        )

        data_in, channel_id, frame_in = connection._handle_amqp_frame(payload)

        self.assertEqual(data_in, b'')
        self.assertEqual(channel_id, 0)
        self.assertIsInstance(frame_in, specification.Connection.Tune)
Пример #43
0
    def open(self):
        """"
        Create/open connection and declare logging exchange.
        """
        if not self.connection or self.connection.is_closed:
            self.connection = Connection(self.host,
                                         self.username,
                                         self.password,
                                         port=self.port,
                                         kwargs={'heartbeat': 600})

        if not self.channel or self.channel.is_closed:
            self.channel = self.connection.channel()
Пример #44
0
    def test_functional_alternative_virtual_host(self):
        self.api.virtual_host.create(self.virtual_host_name)

        self.api.user.set_permission('guest', self.virtual_host_name)

        self.connection = Connection(HOST, USERNAME, PASSWORD,
                                     virtual_host=self.virtual_host_name,
                                     timeout=1)
        self.channel = self.connection.channel()
        self.channel.queue.declare(self.queue_name)
        self.channel.queue.delete(self.queue_name)
        self.api.user.delete_permission('guest', self.virtual_host_name)
        self.api.virtual_host.delete(self.virtual_host_name)
Пример #45
0
 def setUp(self):
     self.connection = Connection(HOST, USERNAME, PASSWORD)
     self.channel = self.connection.channel()
     self.channel.queue.declare(self.queue_name)
     self.channel.queue.purge(self.queue_name)
     self.channel.confirm_deliveries()
     self.channel.basic.publish(body=self.message,
                                routing_key=self.queue_name)
     message = self.channel.basic.get(self.queue_name, no_ack=False,
                                      to_dict=False)
     message.reject()
     # Sleep for 0.1s to make sure RabbitMQ has time to catch up.
     time.sleep(0.1)
Пример #46
0
    def test_connection_maximum_channels_reached(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection.set_state(connection.OPEN)

        for index in compatibility.RANGE(MAX_CHANNELS):
            channel_id = connection._get_next_available_channel_id()
            connection._channels[channel_id] = None
            self.assertEqual(connection._last_channel_id, index + 1)

        self.assertRaisesRegexp(
            AMQPConnectionError,
            'reached the maximum number of channels %d' % MAX_CHANNELS,
            connection.channel, lazy=True)
Пример #47
0
 def create_connection(self):
     attempts = 0
     while True:
         attempts += 1
         try:
             self.connection = Connection('127.0.0.1', 'guest', 'guest')
             break
         except amqpstorm.AMQPError as why:
             LOGGER.exception(why)
             if self.max_retries and attempts > self.max_retries:
                 break
             time.sleep(min(attempts * 2, 30))
         except KeyboardInterrupt:
             break
Пример #48
0
    def test_connection_close_handles_raise_on_write(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection.set_state(connection.OPEN)
        io = IO(connection.parameters, [])
        io.socket = Mock(name='socket', spec=socket.socket)
        connection._io = io

        # Create some fake channels.
        for index in range(10):
            connection._channels[index + 1] = Channel(
                index + 1, connection, 360)

        def raise_on_write(_):
            raise AMQPConnectionError('travis-ci')

        connection._channel0._write_frame = raise_on_write

        self.assertFalse(connection.is_closed)

        connection.close()

        # Make sure all the fake channels were closed as well.
        for index in range(10):
            self.assertNotIn(index + 1, connection._channels)

        self.assertFalse(connection._channels)
        self.assertTrue(connection.is_closed)
Пример #49
0
    def test_connection_close(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        connection.set_state(connection.OPEN)
        io = IO(connection.parameters, [])
        io.socket = Mock(name='socket', spec=socket.socket)
        connection._io = io

        # Create some fake channels.
        for index in range(10):
            connection._channels[index + 1] = Channel(
                index + 1, connection, 360)

        def on_write(frame_out):
            self.assertIsInstance(frame_out, specification.Connection.Close)
            connection._channel0._close_connection_ok()

        connection._channel0._write_frame = on_write

        self.assertFalse(connection.is_closed)

        connection.close()

        # Make sure all the fake channels were closed as well.
        for index in range(10):
            self.assertNotIn(index + 1, connection._channels)

        self.assertTrue(connection.is_closed)
Пример #50
0
class Consumer(object):
    def __init__(self, max_retries=None):
        self.max_retries = max_retries
        self.connection = None

    def create_connection(self):
        """Create a connection.

        :return:
        """
        attempts = 0
        while True:
            attempts += 1
            try:
                self.connection = Connection('127.0.0.1', 'guest', 'guest')
                break
            except amqpstorm.AMQPError as why:
                LOGGER.exception(why)
                if self.max_retries and attempts > self.max_retries:
                    break
                time.sleep(min(attempts * 2, 30))
            except KeyboardInterrupt:
                break

    def start(self):
        """Start the Consumers.

        :return:
        """
        if not self.connection:
            self.create_connection()
        while True:
            try:
                channel = self.connection.channel()
                channel.queue.declare('simple_queue')
                channel.basic.consume(self, 'simple_queue', no_ack=False)
                channel.start_consuming()
                if not channel.consumer_tags:
                    channel.close()
            except amqpstorm.AMQPError as why:
                LOGGER.exception(why)
                self.create_connection()
            except KeyboardInterrupt:
                self.connection.close()
                break

    def __call__(self, message):
        print("Message:", message.body)
        message.ack()
Пример #51
0
    def __init__(self,
                 source_queue,
                 sink_queue,
                 host="localhost",
                 user="******",
                 password="******"):
        self.source_queue = source_queue
        self.sink_queue = sink_queue

        self.connection = Connection(host, user, password)
        self.channel = self.connection.channel()
        self.channel.queue.declare(source_queue)
        self.channel.queue.declare(sink_queue)

        self.process_func = None
Пример #52
0
class ExchangeCreateTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()

    def test_exchange_create(self):
        # Create the Exchange.
        self.channel.exchange.declare('test.exchange.create')

        # Confirm that the Exchange was declared.
        self.channel.exchange.declare('test.exchange.create', passive=True)

    def tearDown(self):
        self.channel.exchange.delete('test.exchange.create')
        self.channel.close()
        self.connection.close()
Пример #53
0
def start_consumer():
    ssl_options = {
        'context': ssl.create_default_context(cafile='cacert.pem'),
        'server_hostname': 'rmq.eandersson.net'
    }

    with Connection('rmq.eandersson.net', 'guest', 'guest', port=5671,
                    ssl=True, ssl_options=ssl_options) as connection:
        with connection.channel() as channel:
            # Declare the Queue, 'simple_queue'.
            channel.queue.declare('simple_queue')

            # Set QoS to 100.
            # This will limit the consumer to only prefetch a 100 messages.

            # This is a recommended setting, as it prevents the
            # consumer from keeping all of the messages in a queue to itself.
            channel.basic.qos(100)

            # Start consuming the queue 'simple_queue' using the callback
            # 'on_message' and last require the message to be acknowledged.
            channel.basic.consume(on_message, 'simple_queue', no_ack=False)

            try:
                # Start consuming messages.
                channel.start_consuming()
            except KeyboardInterrupt:
                channel.close()
Пример #54
0
    def test_functional_open_close_connection_loop(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD, lazy=True)
        for _ in range(25):
            self.connection.open()
            channel = self.connection.channel()

            # Make sure that it's a new channel.
            self.assertEqual(int(channel), 1)

            channel.queue.declare(self.queue_name)

            channel.close()

            # Verify that the Connection/Channel has been opened properly.
            self.assertIsNotNone(self.connection._io.socket)
            self.assertIsNotNone(self.connection._io.poller)
            self.assertTrue(self.connection.is_open)

            self.connection.close()

            # Verify that the Connection has been closed properly.
            self.assertTrue(self.connection.is_closed)
            self.assertIsNone(self.connection._io.socket)
            self.assertIsNone(self.connection._io.poller)
            self.assertFalse(self.connection._io._running.is_set())
            self.assertFalse(self.connection.exceptions)
Пример #55
0
def connect():
    module.connection = Connection(current_app.config['AMQP_HOST'],
                                   current_app.config['AMQP_USER'],
                                   current_app.config['AMQP_PASS'],
                                   kwargs={'heartbeat': 600})
    module.channel = connection.channel()
    channel.confirm_deliveries()
Пример #56
0
    def test_connection_invalid_timeout_on_channel(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', timeout=1,
                                lazy=True)

        self.assertRaisesRegexp(AMQPInvalidArgument,
                                'rpc_timeout should be an integer',
                                connection.channel, None)
def publish_messages():
    with Connection('127.0.0.1', 'guest', 'guest') as connection:
        with connection.channel() as channel:
            # Declare the Queue, 'simple_queue'.
            channel.queue.declare('simple_queue')

            # Message Properties.
            properties = {
                'content_type': 'text/plain',
                'headers': {
                    'key': 'value'
                }
            }

            # Enable server local transactions on channel.
            channel.tx.select()

            # Create the message.
            message = Message.create(channel, 'Hello World!', properties)

            # Publish the message to a queue called, 'simple_queue'.
            message.publish('simple_queue')

            # Commit the message(s).
            channel.tx.commit()

            # Alternatively rollback the message(s).
            # channel.tx.rollback()

            # You can also use the context manager.
            with channel.tx:
                message.publish('simple_queue')
Пример #58
0
 def test_connection_with_statement_when_failing(self):
     try:
         with Connection('127.0.0.1', 'guest', 'guest', lazy=True) as con:
             con.exceptions.append(AMQPConnectionError('error'))
             con.check_for_errors()
     except AMQPConnectionError as why:
         self.assertIsInstance(why, AMQPConnectionError)