예제 #1
0
    def test_publish_message_amqp_error_connection_set(
            self, build_message_properties_mock, init_connection_mock):
        """If an AMQPError is raised when `_publish_message` is called, and the connection is set,
        then it should be closed and logger.error should be called.
        
        Args:
            build_message_properties_mock ([type]): [description]
            init_connection_mock ([type]): [description]
        """
        message_properties = build_message_properties_mock.return_value
        connection = init_connection_mock.return_value
        channel = connection.channel.return_value
        body = "{'hello': 'world'}"
        channel.basic_publish.side_effect = AMQPError()

        self.publisher._publish_message(body)

        channel.exchange_declare.assert_called_once_with(
            exchange=config.NAVI_EXCHANGE,
            exchange_type=config.NAVI_EXCHANGE_TYPE,
            durable=True,
        )
        channel.basic_publish.assert_called_once_with(
            exchange=config.NAVI_EXCHANGE,
            routing_key=self.publisher._routing_key,
            properties=message_properties,
            body=body,
        )
        connection.close.assert_called_once()
        self.publisher.logger.error.assert_called_once()
예제 #2
0
 def _declare_exchange_and_queue(self):
     if(self._connection and self._channel):
         try:
             self._channel.exchange_declare(exchange=self.exchange,
                                        exchange_type=self.exchange_type,
                                                durable=self.durable)
         except AMQPError as err:
             Log.error('Exchange: [{%s}], type: [ {%s} ] cannot be declared.\
                        Details: {%s}'%(self.exchange,
                                           self.exchange_type,
                                           str(err)))
         try:
             self._channel.queue_declare(queue=self.exchange_queue,
                                         exclusive=self.exclusive,
                                                 durable=self.durable)
             self._channel.queue_bind(exchange=self.exchange,
                                      queue=self.exchange_queue,
                                      routing_key=self.routing_key)
             Log.info(f'Initialized Exchange: {self.exchange}, '
                     f'Queue: {self.exchange_queue}, routing_key: {self.routing_key}')
         except AMQPError as err:
             Log.error(f'Fails to initialize the AMQP queue.\
                   Details: {err}')
             Log.exception(err)
             raise AMQPError(-1, f'{err}')
예제 #3
0
    def test_when_first_connection_fails_then_secondary_succeeds(self):
        # Given
        with patch(
                "app.submitter.submitter.BlockingConnection"
        ) as connection, patch(
                "app.submitter.submitter.URLParameters") as url_parameters:
            secondary_connection = Mock()
            connection.side_effect = [AMQPError(), secondary_connection]

            # When
            published = self.submitter.send_message(
                message={},
                tx_id="12345",
                case_id="456",
            )

            # Then
            self.assertTrue(published, "send_message should publish message")
            # Check we create url for primary then secondary
            url_parameters_calls = [
                call(f"amqp://{self.host1}:{self.port}/%2F"),
                call(f"amqp://{self.host2}:{self.port}/%2F"),
            ]
            url_parameters.assert_has_calls(url_parameters_calls)
            # Check we create connection twice, failing first then with self.url2
            self.assertEqual(connection.call_count, 2)
예제 #4
0
    def test_url_generation_with_credentials(self):
        # Given
        with patch('app.submitter.submitter.BlockingConnection') as connection, \
                patch('app.submitter.submitter.URLParameters') as url_parameters:
            secondary_connection = Mock()
            connection.side_effect = [AMQPError(), secondary_connection]

            username = '******'
            password = str(uuid.uuid4())

            submitter = RabbitMQSubmitter(host=self.host1,
                                          secondary_host=self.host2,
                                          port=self.port,
                                          username=username,
                                          password=password)

            # When
            published = submitter.send_message(message={},
                                               queue=self.queue_name,
                                               tx_id='12345')

            # Then
            self.assertTrue(published, 'send_message should publish message')
            # Check we create url for primary then secondary
            url_parameters_calls = [
                call('amqp://{}:{}@{}:{}/%2F'.format(username, password,
                                                     self.host1, self.port)),
                call('amqp://{}:{}@{}:{}/%2F'.format(username, password,
                                                     self.host2, self.port))
            ]
            url_parameters.assert_has_calls(url_parameters_calls)
            # Check we create connection twice, failing first then with self.url2
            self.assertEqual(connection.call_count, 2)
예제 #5
0
    def test_when_fail_to_connect_to_queue_then_published_false(self):
        # Given
        with patch('app.submitter.submitter.BlockingConnection') as connection:
            connection.side_effect = AMQPError()

            # When
            published = self.submitter.send_message(message={}, queue=self.queue_name, tx_id='12345')

            # Then
            self.assertFalse(published, 'send_message should fail to publish message')
예제 #6
0
    def test_listen_failure_connection_not_set(self, init_connection_mock,
                                               close_mock):
        """
        When the listener's `listen` method is called, `_init_connection` should be called, and from
        the connection returned by it, `ioloop.start` should be called. If the execution fails, if
        the connection is not set, then `connection.close` shouldn't be called.
        """
        init_connection_mock.side_effect = AMQPError()

        self.listener._listen()

        close_mock.assert_not_called()
예제 #7
0
    def test_listen_failure_connection_set(self, init_connection_mock):
        """
        When the listener's `listen` method is called, `_init_connection` should be called, and from
        the connection returned by it, `ioloop.start` should be called. As the execution fails, if
        the connection is set, then `connection.close` should be be called.
        """
        self.listener._connection_parameters = mock.MagicMock()
        connection = init_connection_mock.return_value
        connection.ioloop.start.side_effect = (AMQPError(), True)

        self.listener._listen()

        connection.ioloop.start.assert_any_call()
        connection.close.assert_called_once()
예제 #8
0
    def test_when_fail_to_connect_to_queue_then_published_false(self):
        # Given
        with patch("app.submitter.submitter.BlockingConnection") as connection:
            connection.side_effect = AMQPError()

            # When
            published = self.submitter.send_message(
                message={},
                tx_id="123",
                case_id="456",
            )

            # Then
            self.assertFalse(published, "send_message should fail to publish message")
예제 #9
0
    def test_when_fail_to_disconnect_then_log_warning_message(self):
        # Given
        connection = Mock()
        error = AMQPError()
        connection.close.side_effect = [error]

        with patch('app.submitter.submitter.BlockingConnection', return_value=connection), \
                patch('app.submitter.submitter.logger') as logger:

            # When
            published = self.submitter.send_message(message={}, queue=self.queue_name, tx_id='12345')

            # Then
            self.assertTrue(published)
            logger.error.assert_called_once_with('unable to close connection', category='rabbitmq', exc_info=error)
예제 #10
0
    def test_publish_message_connection_error(self,
                                              build_message_properties_mock,
                                              init_connection_mock):
        """If an AMQPError is raised when `_publish_message` is called, and the connection is not
        set, then it shouldn't be closed and logger.error should be called.
        
        Args:
            build_message_properties_mock ([type]): [description]
            init_connection_mock ([type]): [description]
        """
        message_properties = build_message_properties_mock.return_value
        init_connection_mock.side_effect = AMQPError()
        body = "{'hello': 'world'}"

        self.publisher._publish_message(body)

        self.publisher.logger.error.assert_called_once()
    def test_url_generation_with_credentials(self):
        # Given
        with patch("app.submitter.submitter.BlockingConnection") as connection, patch(
            "app.submitter.submitter.URLParameters"
        ) as url_parameters:
            secondary_connection = Mock()
            connection.side_effect = [AMQPError(), secondary_connection]

            username = "******"
            password = str(uuid.uuid4())

            submitter = RabbitMQSubmitter(
                host=self.host1,
                secondary_host=self.host2,
                port=self.port,
                queue=self.queue,
                username=username,
                password=password,
            )

            # When
            published = submitter.send_message(
                message={},
                tx_id="12345",
                questionnaire_id="0123456789000000",
                case_id="456",
            )

            # Then
            self.assertTrue(published, "send_message should publish message")
            # Check we create url for primary then secondary
            url_parameters_calls = [
                call(
                    "amqp://{}:{}@{}:{}/%2F".format(
                        username, password, self.host1, self.port
                    )
                ),
                call(
                    "amqp://{}:{}@{}:{}/%2F".format(
                        username, password, self.host2, self.port
                    )
                ),
            ]
            url_parameters.assert_has_calls(url_parameters_calls)
            # Check we create connection twice, failing first then with self.url2
            self.assertEqual(connection.call_count, 2)
예제 #12
0
    def test_when_first_connection_fails_then_secondary_succeeds(self):
        # Given
        with patch('app.submitter.submitter.BlockingConnection') as connection, \
                patch('app.submitter.submitter.URLParameters') as url_parameters:
            secondary_connection = Mock()
            connection.side_effect = [AMQPError(), secondary_connection]

            # When
            published = self.submitter.send_message(message={}, queue=self.queue_name, tx_id='12345')

            # Then
            self.assertTrue(published, 'send_message should publish message')
            # Check we create url for primary then secondary
            url_parameters_calls = [call('amqp://{}:{}/%2F'.format(self.host1, self.port)),
                                    call('amqp://{}:{}/%2F'.format(self.host2, self.port))]
            url_parameters.assert_has_calls(url_parameters_calls)
            # Check we create connection twice, failing first then with self.url2
            self.assertEqual(connection.call_count, 2)
예제 #13
0
    def test_when_fail_to_disconnect_then_log_warning_message(self):
        # Given
        connection = Mock()
        error = AMQPError()
        connection.close.side_effect = [error]
        with patch('app.submitter.submitter.BlockingConnection', return_value=connection), \
                patch('app.submitter.submitter.logger') as logger:

            # When
            published = RabbitMQSubmitter().send_message(message={},
                                                         queue='test_queue')

            # Then
            self.assertTrue(published)
            logger.error.assert_called_once_with(
                'unable to close rabbit mq connection',
                rabbit_url='amqp://localhost:5672/%2F',
                exc_info=error)
예제 #14
0
    def test_when_fail_to_disconnect_then_log_warning_message(self):
        # Given
        connection = Mock()
        error = AMQPError()
        connection.close.side_effect = [error]

        with patch("app.submitter.submitter.BlockingConnection",
                   return_value=connection), patch(
                       "app.submitter.submitter.logger") as logger:
            # When
            published = self.submitter.send_message(
                message={},
                tx_id="123",
                case_id="456",
            )

            # Then
            self.assertTrue(published)
            logger.error.assert_called_once_with("unable to close connection",
                                                 category="rabbitmq",
                                                 exc_info=error)
예제 #15
0
    def test_when_first_connection_fails_then_secondary_succeeds(self):
        # Given
        with patch('app.submitter.submitter.BlockingConnection') as connection, \
                patch('app.submitter.submitter.URLParameters') as url_parameters:
            secondary_connection = Mock()
            connection.side_effect = [AMQPError(), secondary_connection]

            # When
            published = RabbitMQSubmitter().send_message(message={},
                                                         queue='test_queue')

            # Then
            self.assertTrue(published, 'send_message should publish message')
            # Check we create url for primary then secondary
            url_parameters_calls = [
                call(settings.EQ_RABBITMQ_URL),
                call(settings.EQ_RABBITMQ_URL_SECONDARY)
            ]
            url_parameters.assert_has_calls(url_parameters_calls)
            # Check we create connection twice, failing first then with settings.EQ_RABBITMQ_URL_SECONDARY
            self.assertEqual(connection.call_count, 2)
예제 #16
0
파일: test.py 프로젝트: hbyhl/utils4py
    # d = {'key1': 1, 'key2': 2}
    # b = map(lambda x,y: (x,y * 10), d.keys(),d.values())
    # for k,v in b:
    #     print(k)
    #     print(v)

    # graph = Graph('http://10.31.1.102:7474', username='******', password='******')
    # print('')
    # parsed= urlsplit("http://*****:*****@host:port/db/db/")
    # u_v = coalesce(parsed.username, "use0r")
    # print(parsed)
    # print(u_v)
    print(os.path.abspath('.abc'))

    if not os.path.exists(os.path.abspath('abc')):
        os.makedirs(os.path.abspath('abc'))

    try:
        raise AMQPError('cuowu ')
    except AMQPError as e:
        print('catch excption')
        raise e
    except Exception:
        print('other exception')
    finally:
        print('finally')
        ChannelWrongStateError