Exemple #1
0
    def test_eq(self):
        self.assertEqual(credentials.PlainCredentials('u', 'p'),
                         credentials.PlainCredentials('u', 'p'))

        self.assertEqual(credentials.PlainCredentials('u', 'p', True),
                         credentials.PlainCredentials('u', 'p', True))

        self.assertEqual(credentials.PlainCredentials('u', 'p', False),
                         credentials.PlainCredentials('u', 'p', False))

        self.assertEqual(credentials.PlainCredentials('u', 'p', False),
                         ChildPlainCredentials('u', 'p', False))

        self.assertEqual(ChildPlainCredentials('u', 'p', False),
                         credentials.PlainCredentials('u', 'p', False))

        class Foreign(object):
            def __eq__(self, other):
                return 'foobar'

        self.assertEqual(
            credentials.PlainCredentials('u', 'p', False) == Foreign(),
            'foobar')
        self.assertEqual(
            Foreign() == credentials.PlainCredentials('u', 'p', False),
            'foobar')
Exemple #2
0
 def credentials(username, password):
     """返回一个plain credentials对象
     :param username:用户名
     :param password:密码
     :return:pika_credentials.PlainCredentials
     """
     return credentials.PlainCredentials(username, password)
Exemple #3
0
 def _init_messaging(self):
     LOG.info('Initializing connection to rabbitmq node..')
     # construct credentials
     credentials = pika_credentials.PlainCredentials(
         username=self.rabbitUID, password=self.rabbitPass)
     parameters = pika.ConnectionParameters(
         host=self.rabbitHost,
         port=self.rabbitPort,
         virtual_host=self.rabbitVHost,
         credentials=credentials,
         channel_max=self.channel_max,
         frame_max=self.frame_max,
         heartbeat_interval=self.heartbeat_rate,
         connection_attempts=self.connection_attemps,
         retry_delay=self.retry_delay,
         socket_timeout=self.socket_timeout,
         locale=self.pika_locale)
     self.connection = pika.BlockingConnection(parameters=parameters)
     self.channel = self.connection.channel()
     self.channel.confirm_delivery()
     # self.channel.basic_qos(prefetch_size=0,
     #                        prefetch_count=0,
     #                        all_channels=False
     #                        )
     self.channel.exchange_declare(exchange=self.exchange_name,
                                   exchange_type=self.exchange_type,
                                   passive=self.is_passive,
                                   durable=self.is_durable,
                                   auto_delete=self.is_auto_delete,
                                   internal=self.is_internal,
                                   arguments=self.arguments)
 def create_or_get_instance(rabbitmq_conf):
     global __conn
     if __conn is None:
         connection_conf = []
         creds = credentials.PlainCredentials(rabbitmq_conf['user'],
                                              rabbitmq_conf['pass'])
         hosts = rabbitmq_conf['hosts']
         for host_idx in range(len(hosts)):
             params = connection.ConnectionParameters(host=hosts[host_idx],
                                                      credentials=creds)
             if host_idx == len(hosts) - 1:
                 params.connection_attempts = rabbitmq_conf[
                     'connection_attempts']
                 params.retry_delay = rabbitmq_conf['retry_delay']
             connection_conf.append(params)
         try:
             __conn = adapters.select_connection.SelectConnection(
                 connection_conf, custom_ioloop=IOLoop.current())
             __conn.add_on_close_callback(
                 RabbitMq._on_connection_close(rabbitmq_conf))
         except AMQPConnectionError as e:
             print(
                 f'Failed to connect to any of the RabbitMq hosts {hosts} because {e}'
             )
             raise e
     return __conn
Exemple #5
0
 def new_mock_parameters(self, host, port, vhost, user, password):
     mock_credentials = credentials.PlainCredentials(user, password)
     mock_credentials.username = user
     mock_credentials.password = password
     mock_parameters = connection.ConnectionParameters(
         host, port, vhost, mock_credentials)
     return mock_parameters
Exemple #6
0
 def test_good_connection_parameters(self):
     """make sure connection kwargs get set correctly"""
     kwargs = {
         'host': 'https://www.test.com',
         'port': 5678,
         'virtual_host': u'vvhost',
         'channel_max': 3,
         'frame_max': 40000,
         'credentials': credentials.PlainCredentials('very', 'secure'),
         'heartbeat_interval': 7,
         'backpressure_detection': False,
         'retry_delay': 3,
         'ssl': True,
         'connection_attempts': 2,
         'locale': 'en',
         'ssl_options': {'ssl': 'options'}
     }
     conn = connection.ConnectionParameters(**kwargs)
     #check values
     for t_param in ('host', 'port', 'virtual_host', 'channel_max',
                     'frame_max', 'backpressure_detection', 'ssl',
                     'credentials', 'retry_delay', 'connection_attempts',
                     'locale'):
         self.assertEqual(kwargs[t_param], getattr(conn, t_param), t_param)
     self.assertEqual(kwargs['heartbeat_interval'], conn.heartbeat)
    def test_good_connection_parameters(self):
        """make sure connection kwargs get set correctly"""
        kwargs = {
            'backpressure_detection': False,
            'blocked_connection_timeout': 10.5,
            'channel_max': 3,
            'client_properties': {'good': 'day'},
            'connection_attempts': 2,
            'credentials': credentials.PlainCredentials('very', 'secure'),
            'frame_max': 40000,
            'heartbeat': 7,
            'host': 'https://www.test.com',
            'locale': 'en',
            'port': 5678,
            'retry_delay': 3,
            'socket_timeout': 100.5,
            'ssl': True,
            'ssl_options': {'ssl': 'options'},
            'virtual_host': u'vvhost',
        }
        params = connection.ConnectionParameters(**kwargs)

        # Verify

        expected_values = copy.copy(kwargs)

        # Make sure we're testing all public properties
        self.assertSequenceEqual(sorted(expected_values),
                                 sorted(_ALL_PUBLIC_PARAMETERS_PROPERTIES))
        # Check property values
        for t_param in expected_values:
            value = getattr(params, t_param)
            self.assertEqual(expected_values[t_param], value,
                             msg='Expected %s=%r, but got %r' %
                             (t_param, expected_values[t_param], value))
    def get_default_properties(self):
        """
        :returns: a dict of expected public property names and default values
            for `pika.connection.Parameters`

        """
        kls = connection.Parameters
        defaults = {
            'backpressure_detection': kls.DEFAULT_BACKPRESSURE_DETECTION,
            'blocked_connection_timeout':
                kls.DEFAULT_BLOCKED_CONNECTION_TIMEOUT,
            'channel_max': kls.DEFAULT_CHANNEL_MAX,
            'client_properties': kls.DEFAULT_CLIENT_PROPERTIES,
            'connection_attempts': kls.DEFAULT_CONNECTION_ATTEMPTS,
            'credentials': credentials.PlainCredentials(kls.DEFAULT_USERNAME,
                                                        kls.DEFAULT_PASSWORD),
            'frame_max': kls.DEFAULT_FRAME_MAX,
            'heartbeat': kls.DEFAULT_HEARTBEAT_TIMEOUT,
            'host': kls.DEFAULT_HOST,
            'locale': kls.DEFAULT_LOCALE,
            'port': kls.DEFAULT_PORT,
            'retry_delay': kls.DEFAULT_RETRY_DELAY,
            'socket_timeout': kls.DEFAULT_SOCKET_TIMEOUT,
            'ssl': kls.DEFAULT_SSL,
            'ssl_options': kls.DEFAULT_SSL_OPTIONS,
            'virtual_host': kls.DEFAULT_VIRTUAL_HOST
        }

        # Make sure we didn't miss anything
        self.assertSequenceEqual(sorted(defaults),
                                 sorted(_ALL_PUBLIC_PARAMETERS_PROPERTIES))

        return defaults
 def _credentials(self, username, password):
     '''返回一个plain credentials对象
     :param username:用户名
     :param password:密码
     :return:pika_credentials.PlainCredentials
     '''
     return credentials.PlainCredentials(username, password)
Exemple #10
0
    def _credentials(self, username, password):
        """Return a plain credentials object for the specified username and
        password.

        :param str username: The username to use
        :param str password: The password to use
        :rtype: pika_credentials.PlainCredentials

        """
        return pika_credentials.PlainCredentials(username, password)
    def __init__(self, level=logging.NOTSET, formatter=JSONFormatter(),
                 host='localhost', port=5672, connection_params=None,
                 username=None, password=None,
                 exchange='log', declare_exchange=False,
                 routing_key_format="{name}.{level}", close_after_emit=False,
                 fields=None, fields_under_root=True):
        """
        Initialize the handler.

        :param level:              Logs level.
        :param formatter:          Use custom formatter for the logs.
        :param host:               RabbitMQ host. Default localhost
        :param port:               RabbitMQ Port. Default 5672
        :param connection_params:  Allow extra params to connect with RabbitMQ.
        :param username:           Username in case of authentication.
        :param password:           Password for the username.
        :param exchange:           Send logs using this exchange.
        :param declare_exchange:   Whether or not to declare the exchange.
        :param routing_key_format: Customize how messages will be routed to the queues.
        :param close_after_emit:   Close connection after emit the record?
        :param fields:             Send these fields as part of all logs.
        :param fields_under_root:  Merge the fields in the root object.
        """

        super(RabbitMQHandler, self).__init__(level=level)

        # Important instances/properties.
        self.exchange = exchange
        self.connection = None
        self.channel = None
        self.exchange_declared = not declare_exchange
        self.routing_key_format = routing_key_format
        self.close_after_emit = close_after_emit

        # Connection parameters.
        # Allow extra params when connect to RabbitMQ.
        # @see: http://pika.readthedocs.io/en/0.10.0/modules/parameters.html#pika.connection.ConnectionParameters
        conn_params = connection_params if isinstance(connection_params, dict) else {}
        self.connection_params = conn_params.copy()
        self.connection_params.update(dict(host=host, port=port, heartbeat_interval=0))

        if username and password:
            self.connection_params['credentials'] = credentials.PlainCredentials(username, password)

        # Logging.
        self.formatter = formatter
        self.fields = fields if isinstance(fields, dict) else {}
        self.fields_under_root = fields_under_root

        if len(self.fields) > 0:
            self.addFilter(FieldFilter(self.fields, self.fields_under_root))

        # Connect.
        self.createLock()
Exemple #12
0
    def __init__(self, config, type, callback=None):
        '''
        初始化需要连接的rabbitmq server
        :param config: 要连接的rabbitmq server 配置信息,config.py模块导入
        :param type: 需要接收的日志等级
        :param callback: 对接收到的日志信息进行的处理
        '''
        self.connection_para = dict(host=config.MQHOST)
        if config.MQPORT:
            self.connection_para['port'] = config.MQPORT
        if config.VIRTUAL_HOST:
            self.connection_para['virtual_host'] = config.VIRTUAL_HOST
        if config.USERNAME and config.PASSWORD:
            self.connection_para['credentials'] = credentials.PlainCredentials(
                config.USERNAME, config.PASSWORD)
        self.exchange = config.EXCHANGE
        self.exchange_type = config.EXCHANGE_TYPE
        self.connection, self.channel = None, None

        #连接到指定的rabbitmq server
        try:
            self.connection = pika.BlockingConnection(
                pika.ConnectionParameters(**self.connection_para))
        except Exception as e:
            print "connect to rabbitmq is error"
        else:
            self.channel = self.connection.channel()

        #创建rabbitmq server 的exchange,并使该机制持久化存储在rabbitmq中
        self.channel.exchange_declare(exchange=self.exchange,
                                      exchange_type=self.exchange_type,
                                      durable=True,
                                      auto_delete=False)
        sys.stdout.write('%s - stdout - [mq] Declare exchange success.\n' %
                         datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

        #创建需要监听的消息队列
        result = self.channel.queue_declare(queue=config.QUEUE_NAMES[type])
        queue_name = result.method.queue

        #exchange和需要监听的queue进行绑定
        self.channel.queue_bind(exchange=MQLogConfig.EXCHANGE,
                                queue=config.QUEUE_NAMES[type],
                                routing_key="#." + config.BRIND_KEYS[type])

        #对rabbitmq server 队列中的消息进行处理,默认把消息打印到console,可自己指定xxxcallback
        if callback == None:
            self.channel.basic_consume(printcallback,
                                       queue=queue_name,
                                       no_ack=True)
        else:
            self.channel.basic_consume(callback, queue=queue_name, no_ack=True)
Exemple #13
0
    def test_eq(self):
        self.assertEqual(credentials.PlainCredentials('u', 'p'),
                         credentials.PlainCredentials('u', 'p'))

        self.assertEqual(credentials.PlainCredentials('u', 'p', True),
                         credentials.PlainCredentials('u', 'p', True))

        self.assertEqual(credentials.PlainCredentials('u', 'p', False),
                         credentials.PlainCredentials('u', 'p', False))
Exemple #14
0
    def test_eq(self):
        params_1 = connection.Parameters()
        params_2 = connection.Parameters()
        params_3 = ChildParameters()

        self.assertEqual(params_1, params_2)
        self.assertEqual(params_2, params_1)

        params_1.host = 'localhost'
        params_1.port = 5672
        params_1.virtual_host = '/'
        params_1.credentials = credentials.PlainCredentials('u', 'p')
        params_2.host = 'localhost'
        params_2.port = 5672
        params_2.virtual_host = '//'
        params_2.credentials = credentials.PlainCredentials('uu', 'pp')
        self.assertEqual(params_1, params_2)
        self.assertEqual(params_2, params_1)

        params_1.host = 'localhost'
        params_1.port = 5672
        params_1.virtual_host = '/'
        params_1.credentials = credentials.PlainCredentials('u', 'p')
        params_3.host = 'localhost'
        params_3.port = 5672
        params_3.virtual_host = '//'
        params_3.credentials = credentials.PlainCredentials('uu', 'pp')
        self.assertEqual(params_1, params_3)
        self.assertEqual(params_3, params_1)

        class Foreign(object):
            def __eq__(self, other):
                return 'foobar'

        self.assertEqual(params_1 == Foreign(), 'foobar')
        self.assertEqual(Foreign() == params_1, 'foobar')
Exemple #15
0
 def _get_common_settings(cls):
     if 'url' in settings.CQRS:
         host, port, user, password = cls._parse_url(settings.CQRS.get('url'))
     else:
         host = settings.CQRS.get('host', ConnectionParameters.DEFAULT_HOST)
         port = settings.CQRS.get('port', ConnectionParameters.DEFAULT_PORT)
         user = settings.CQRS.get('user', ConnectionParameters.DEFAULT_USERNAME)
         password = settings.CQRS.get('password', ConnectionParameters.DEFAULT_PASSWORD)
     exchange = settings.CQRS.get('exchange', 'cqrs')
     return (
         host,
         port,
         credentials.PlainCredentials(user, password, erase_on_connect=True),
         exchange,
     )
Exemple #16
0
    def test_credentials(self):
        params = connection.Parameters()

        plain_cred = credentials.PlainCredentials('very', 'reliable')
        params.credentials = plain_cred
        self.assertEqual(params.credentials, plain_cred)

        ext_cred = credentials.ExternalCredentials()
        params.credentials = ext_cred
        self.assertEqual(params.credentials, ext_cred)

        with self.assertRaises(TypeError):
            params.credentials = connection.Parameters()

        with self.assertRaises(TypeError):
            params.credentials = repr(plain_cred)
Exemple #17
0
def receive_messages(
    host: str, port: int, user: str, password: str, queue: str
) -> None:
    """
    Start consuming messages from RabbitMQ broker.

    :param host: Host of XQueue broker.
    :param port: Port of XQueue broker.
    :param user: Username for basic auth.
    :param password: Password for basic auth.
    :param queue: Queue name.
    """
    #logger: Logger = get_logger("rabbitmq")

    connection: BlockingConnection = BlockingConnection(
        ConnectionParameters(
            host=host,
            port=port,
            credentials=credentials.PlainCredentials(user, password),
        )
    )
    ch: channel = connection.channel()

    # Set durable=True to save messages between RabbitMQ restarts
    ch.queue_declare(queue=queue, durable=True)

    # Make RabbitMQ avoid giving more than 1 message at a time to a worker
    ch.basic_qos(prefetch_count=1)

    # Start receiving messages
    ch.basic_consume(queue=queue, on_message_callback=callback_function)

    try:
        #logger.info("Started consuming messages from RabbitMQ.")
        print("Started consuming messages from RabbitMQ.")
        ch.start_consuming()
    except KeyboardInterrupt as exception:
        #logger.info("Stopped consuming messages from RabbitMQ.")
        print("Stopped consuming messages from RabbitMQ.")

        ch.stop_consuming()
        connection.close()

        raise exception
Exemple #18
0
 def __init__(self,
              auth_user: str,
              auth_pass: str,
              host: str,
              vhost: str,
              queue: str,
              port: int = 5672):
     self.queue_name = queue
     self.params = pika.ConnectionParameters(
         host=host,
         port=port,
         virtual_host=vhost,
         blocked_connection_timeout=2,
         socket_timeout=2,
         credentials=credentials.PlainCredentials(username=auth_user,
                                                  password=auth_pass,
                                                  erase_on_connect=True))
     self.connection = None
     self.channel = None
Exemple #19
0
    def __init__(self, host, port, virtual_host, user, password):
        """Construct our RabbitMQ object for use on the Tornado IOLoop

        :param host: RabbitMQ server host
        :type host: str
        :param port: RabbitMQ server port
        :type port: int
        :param virtual_host: RabbitMQ virtual host to use
        :type virtual_host: str
        :param user: RabbitMQ user to connect as
        :type user: str
        :param password: RabbitMQ user's password
        :type paassword: str

        """

        # Create a logger instance
        self._logger = logging.getLogger(__name__)

        # We don't have a valid connection until the initial connect is done
        self._connection = None

        # We don't have a channel until we're connected
        self._channel = None

        # Set our app_id for publishing messages
        self.app_id = "%s/%s" % (RabbitMQ.DEFAULT_APP_ID, __version__)

        # Set our delivery mode for publishing messages
        self.delivery_mode = RabbitMQ.DEFAULT_DELIVERY_MODE

        # Set our encoding for publishing messages
        self.encoding = RabbitMQ.DEFAULT_ENCODING

        # Create our credentials
        creds = credentials.PlainCredentials(username=user, password=password)

        # Create the connection parameters
        self.params = connection.ConnectionParameters(
            host=host, port=port, virtual_host=virtual_host, credentials=creds)

        # Create a new connection
        tornado_connection.TornadoConnection(self.params, self._on_connected)
Exemple #20
0
def add_tasks(self, task):
    """
    Add load testing tasks to the queue
    """
    message = QueueTaskSerializer().to_representation(task)
    message["host"] = settings.EXTERNAL_ADDRESS
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(
            host=str(settings.AMQP_HOST),
            credentials=pika_credentials.PlainCredentials(
                settings.AMQP_USER, settings.AMQP_PASS)))
    channel = connection.channel()

    serialized_message = json.dumps(message)

    for _ in xrange(task.run_count):
        channel.basic_publish(exchange='',
                              routing_key=settings.AMQP_QUEUE,
                              body=serialized_message)

    connection.close()
Exemple #21
0
 def __init__(self):
     logging.Handler.__init__(self)
     self.zafira_connected = self.__connect_to_zafira()
     self.exchange = 'logs'
     self.connection = None
     self.channel = None
     self.virtual_host = '/'
     self.type = 'x-recent-history'
     self.history = 1000
     self.routing_key = ''
     # make sure exchange only declared once.
     self.is_exchange_declared = False
     # lock for serializing log sending, because pika is not thread safe.
     self.emit_lock = threading.Lock()
     # init connection.
     self.connection_params = dict(host=self.host,
                                   port=self.port,
                                   virtual_host=self.virtual_host,
                                   credentials=credentials.PlainCredentials(
                                       self.username, self.password))
     self.activate_options()
Exemple #22
0
    def __init__(self, config):
        '''
        初始化需要连接的rabbitmq server

        :param config: 要连接的rabbitmq server 配置信息,config.py模块导入
        '''
        logging.Handler.__init__(self)
        self.connection_para = dict(host=config["mqhsot"])
        if config["mqhsot"]:
            self.connection_para['port'] = config["mqport"]
        if config["virtual_host"]:
            self.connection_para['virtual_host'] = config["virtual_host"]
        if config["username"] and config["password"]:
            self.connection_para['credentials'] = credentials.PlainCredentials(
                config["username"], config["password"])
        self.exchange = config["exchange"]
        self.exchange_type = config["exchange_type"]
        self.connection, self.channel = None, None
        # make sure exchange only declared once.
        self.is_exchange_declared = False
        # init connection.
        self.connect()
Exemple #23
0
    def startService(self):
        params = {}
        if self.settings.get("username", None, section='amqp') \
                and self.settings.get("pass", None, section='amqp'):
            params['credentials'] = pika_credentials.PlainCredentials(
                self.settings.get("username", None, section='amqp'),
                self.settings.get("pass", None, section='amqp'))

        if self.settings.getdict("params", dict(), section='amqp'):
            params.update(
                self.settings.getdict("params", dict(), section='amqp'))

        if self.settings.get("amqp_vhost", '/'):
            params.update({
                'virtual_host':
                self.settings.get("vhost", '/', section='amqp')
            })
        parameters = ConnectionParameters(**params)

        self._client = protocol.ClientCreator(
            reactor, twisted_connection.TwistedProtocolConnection, parameters)

        self.do_connect()
    def _do_create_host_connection(self, host, for_listening):
        connection_params = pika.ConnectionParameters(
            host=host.hostname,
            port=host.port,
            credentials=pika_credentials.PlainCredentials(
                host.username, host.password
            ),
            heartbeat_interval=(
                self._heartbeat_interval if for_listening else None
            ),
            **self._common_pika_params
        )
        if for_listening:
            connection = pika_connection.ThreadSafePikaConnection(
                parameters=connection_params
            )
        else:
            connection = pika.BlockingConnection(
                parameters=connection_params
            )
            connection.params = connection_params

        self._set_tcp_user_timeout(connection._impl.socket)
        return connection
Exemple #25
0
    def __init__(self, level=logging.NOTSET, formatter=None,
        host='localhost', port=5672, connection_params=None,
        username=None, password=None,
        exchange='log', declare_exchange=False,
        routing_key_format="{name}.{level}",
        routing_key_formatter=None,
        close_after_emit=False,
        fields=None, fields_under_root=True, message_headers=None,
        record_fields=None, exclude_record_fields=None):
        # Initialize the handler.
        #
        # :param level:                 Logs level.
        # :param formatter:             Use custom formatter for the logs.
        # :param host:                  RabbitMQ host. Default localhost
        # :param port:                  RabbitMQ Port. Default 5672
        # :param connection_params:     Allow extra params to connect with RabbitMQ.
        # :param message_headers:       A dictionary of headers to be published with the message. Optional.
        # :param username:              Username in case of authentication.
        # :param password:              Password for the username.
        # :param exchange:              Send logs using this exchange.
        # :param declare_exchange:      Whether or not to declare the exchange.
        # :param routing_key_format:    Customize how messages will be routed to the queues.
        # :param routing_key_formatter: Override how messages will be routed to the queues.
        #                               Formatter is passed record object.
        # :param close_after_emit:      Close connection after emit the record?
        # :param fields:                Send these fields as part of all logs.
        # :param fields_under_root:     Merge the fields in the root object.
        # :record_fields                A set of attributes that should be preserved from the record object.
        # :exclude_record_fields        A set of attributes that should be ignored from the record object.

        super(RabbitMQHandler, self).__init__(level=level)

        # Important instances/properties.
        self.exchange = exchange
        self.connection = None
        self.channel = None
        self.exchange_declared = not declare_exchange
        self.routing_key_format = routing_key_format
        self.close_after_emit = close_after_emit

        # Connection parameters.
        # Allow extra params when connect to RabbitMQ.
        # @see: http://pika.readthedocs.io/en/0.10.0/modules/parameters.html#pika.connection.ConnectionParameters
        conn_params = connection_params if isinstance(connection_params, dict) else {}
        self.connection_params = conn_params.copy()
        self.connection_params.update(dict(host=host, port=port, heartbeat_interval=0))

        if username and password:
            self.connection_params['credentials'] = credentials.PlainCredentials(username, password)

        # Extra params for message publication
        self.message_headers = message_headers

        # Save routing-key formatter.
        self.routing_key_formatter = routing_key_formatter

        # Logging.
        self.formatter = formatter or JSONFormatter(
            include=record_fields,
            exclude=exclude_record_fields
        )
        self.fields = fields if isinstance(fields, dict) else {}
        self.fields_under_root = fields_under_root

        if len(self.fields) > 0:
            self.addFilter(FieldFilter(self.fields, self.fields_under_root))

        # Connect.
        self.createLock()
    def __init__(self,
                 conf,
                 url,
                 default_exchange=None,
                 allowed_remote_exmods=None):
        self.conf = conf

        self._force_select_poller_use = (
            pika_drv_cmns.is_eventlet_monkey_patched('select'))

        # processing rpc options
        self.default_rpc_exchange = (
            conf.oslo_messaging_pika.default_rpc_exchange)
        self.rpc_reply_exchange = (conf.oslo_messaging_pika.rpc_reply_exchange)

        self.allowed_remote_exmods = [pika_drv_cmns.EXCEPTIONS_MODULE]
        if allowed_remote_exmods:
            self.allowed_remote_exmods.extend(allowed_remote_exmods)

        self.rpc_listener_prefetch_count = (
            conf.oslo_messaging_pika.rpc_listener_prefetch_count)

        self.default_rpc_retry_attempts = (
            conf.oslo_messaging_pika.default_rpc_retry_attempts)

        self.rpc_retry_delay = (conf.oslo_messaging_pika.rpc_retry_delay)
        if self.rpc_retry_delay < 0:
            raise ValueError("rpc_retry_delay should be non-negative integer")

        self.rpc_reply_listener_prefetch_count = (
            conf.oslo_messaging_pika.rpc_listener_prefetch_count)

        self.rpc_reply_retry_attempts = (
            conf.oslo_messaging_pika.rpc_reply_retry_attempts)
        self.rpc_reply_retry_delay = (
            conf.oslo_messaging_pika.rpc_reply_retry_delay)
        if self.rpc_reply_retry_delay < 0:
            raise ValueError("rpc_reply_retry_delay should be non-negative "
                             "integer")

        self.rpc_queue_expiration = (
            self.conf.oslo_messaging_pika.rpc_queue_expiration)

        # processing notification options
        self.default_notification_exchange = (
            conf.oslo_messaging_pika.default_notification_exchange)

        self.notification_persistence = (
            conf.oslo_messaging_pika.notification_persistence)

        self.notification_listener_prefetch_count = (
            conf.oslo_messaging_pika.notification_listener_prefetch_count)

        self.default_notification_retry_attempts = (
            conf.oslo_messaging_pika.default_notification_retry_attempts)
        if self.default_notification_retry_attempts is None:
            raise ValueError("default_notification_retry_attempts should be "
                             "an integer")
        self.notification_retry_delay = (
            conf.oslo_messaging_pika.notification_retry_delay)
        if (self.notification_retry_delay is None
                or self.notification_retry_delay < 0):
            raise ValueError("notification_retry_delay should be non-negative "
                             "integer")

        self._tcp_user_timeout = self.conf.oslo_messaging_pika.tcp_user_timeout
        self.host_connection_reconnect_delay = (
            self.conf.oslo_messaging_pika.host_connection_reconnect_delay)
        self._heartbeat_interval = (
            self.conf.oslo_messaging_pika.heartbeat_interval)

        # initializing connection parameters for configured RabbitMQ hosts
        common_pika_params = {
            'virtual_host': url.virtual_host,
            'channel_max': self.conf.oslo_messaging_pika.channel_max,
            'frame_max': self.conf.oslo_messaging_pika.frame_max,
            'ssl': self.conf.oslo_messaging_pika.ssl,
            'ssl_options': self.conf.oslo_messaging_pika.ssl_options,
            'socket_timeout': self.conf.oslo_messaging_pika.socket_timeout,
        }

        self._connection_lock = threading.Lock()

        self._connection_host_param_list = []
        self._connection_host_status_list = []

        if not url.hosts:
            raise ValueError("You should provide at least one RabbitMQ host")

        for transport_host in url.hosts:
            pika_params = common_pika_params.copy()
            pika_params.update(
                host=transport_host.hostname,
                port=transport_host.port,
                credentials=pika_credentials.PlainCredentials(
                    transport_host.username, transport_host.password),
            )
            self._connection_host_param_list.append(pika_params)
            self._connection_host_status_list.append({
                self.HOST_CONNECTION_LAST_TRY_TIME:
                0,
                self.HOST_CONNECTION_LAST_SUCCESS_TRY_TIME:
                0
            })

        self._next_connection_host_num = random.randint(
            0,
            len(self._connection_host_param_list) - 1)

        # initializing 2 connection pools: 1st for connections without
        # confirmations, 2nd - with confirmations
        self.connection_without_confirmation_pool = pika_pool.QueuedPool(
            create=self.create_connection,
            max_size=self.conf.oslo_messaging_pika.pool_max_size,
            max_overflow=self.conf.oslo_messaging_pika.pool_max_overflow,
            timeout=self.conf.oslo_messaging_pika.pool_timeout,
            recycle=self.conf.oslo_messaging_pika.pool_recycle,
            stale=self.conf.oslo_messaging_pika.pool_stale,
        )

        self.connection_with_confirmation_pool = pika_pool.QueuedPool(
            create=self.create_connection,
            max_size=self.conf.oslo_messaging_pika.pool_max_size,
            max_overflow=self.conf.oslo_messaging_pika.pool_max_overflow,
            timeout=self.conf.oslo_messaging_pika.pool_timeout,
            recycle=self.conf.oslo_messaging_pika.pool_recycle,
            stale=self.conf.oslo_messaging_pika.pool_stale,
        )

        self.connection_with_confirmation_pool.Connection = (
            _PooledConnectionWithConfirmations)
Exemple #27
0
 def test_erase_response_for_no_mechanism_match(self):
     cred = credentials.PlainCredentials(*self.CREDENTIALS)
     start = spec.Connection.Start()
     start.mechanisms = 'FOO BAR BAZ'
     self.assertEqual(cred.response_for(start), (None, None))
Exemple #28
0
 def test_erase_credentials_false(self):
     cred = credentials.PlainCredentials(*self.CREDENTIALS)
     cred.erase_credentials()
     self.assertEqual((cred.username, cred.password), self.CREDENTIALS)
Exemple #29
0
 def test_erase_credentials_true(self):
     cred = credentials.PlainCredentials(self.CREDENTIALS[0],
                                         self.CREDENTIALS[1], True)
     cred.erase_credentials()
     self.assertEqual((cred.username, cred.password), (None, None))
Exemple #30
0
    def test_ne(self):
        self.assertNotEqual(credentials.PlainCredentials('uu', 'p', False),
                            credentials.PlainCredentials('u', 'p', False))

        self.assertNotEqual(credentials.PlainCredentials('u', 'p', False),
                            credentials.PlainCredentials('uu', 'p', False))

        self.assertNotEqual(credentials.PlainCredentials('u', 'pp', False),
                            credentials.PlainCredentials('u', 'p', False))

        self.assertNotEqual(credentials.PlainCredentials('u', 'p', False),
                            credentials.PlainCredentials('u', 'pp', False))

        self.assertNotEqual(credentials.PlainCredentials('u', 'p', True),
                            credentials.PlainCredentials('u', 'p', False))

        self.assertNotEqual(credentials.PlainCredentials('u', 'p', False),
                            credentials.PlainCredentials('u', 'p', True))

        self.assertNotEqual(credentials.PlainCredentials('uu', 'p', False),
                            ChildPlainCredentials('u', 'p', False))

        self.assertNotEqual(ChildPlainCredentials('u', 'pp', False),
                            credentials.PlainCredentials('u', 'p', False))

        self.assertNotEqual(
            credentials.PlainCredentials('u', 'p', False),
            dict(username='******', password='******', erase_on_connect=False))

        self.assertNotEqual(
            dict(username='******', password='******', erase_on_connect=False),
            credentials.PlainCredentials('u', 'p', False))

        class Foreign(object):
            def __ne__(self, other):
                return 'foobar'

        self.assertEqual(
            credentials.PlainCredentials('u', 'p', False) != Foreign(),
            'foobar')
        self.assertEqual(
            Foreign() != credentials.PlainCredentials('u', 'p', False),
            'foobar')