def test_successful_instantiation(self):
        """Should not raise an exception when AMQPAsyncConsumer is instantiated with correct params"""

        obj = AMQPAsyncConsumer(host="192.168.1.254",
                                port=1,
                                virtual_host="dx",
                                username="******",
                                amqp_password="******",
                                rest_client=SimpleClient(),
                                helper=ICDXHelper(options={}))
 def test_full_unsuccessful_instantiation(self):
     """Should raise an exception when AMQPAsyncConsumer is instantiated with malformed params"""
     with pytest.raises(Exception) as e_info:
         obj = AMQPAsyncConsumer(host="192.168.1.254",
                                 port=1,
                                 virtual_host="dx",
                                 username="******",
                                 amqp_password="******",
                                 rest_client=SimpleClient(),
                                 helper=ICDXHelper())
    def test_helper_validation(self):
        """Should raise an exception when AMQPAsyncConsumer is instantiated without params"""

        obj = AMQPAsyncConsumer(host="192.168.1.254",
                                port=1,
                                virtual_host="dx",
                                username="******",
                                amqp_password="******",
                                rest_client=SimpleClient(),
                                helper=ICDXHelper(options={}))

        assert isinstance(getattr(obj, "_helper"), ICDXHelper)

        obj_with_no_client = AMQPAsyncConsumer(host="192.168.1.254",
                                               port=1,
                                               virtual_host="dx",
                                               username="******",
                                               amqp_password="******",
                                               rest_client=SimpleClient(),
                                               helper=123)
        assert isinstance(getattr(obj_with_no_client, "_helper"), type(None))
    def test_default_port_usage(self):
        """Should raise an exception when AMQPAsyncConsumer is instantiated without params"""

        obj = AMQPAsyncConsumer(host="192.168.1.254",
                                port=1,
                                virtual_host="dx",
                                username="******",
                                amqp_password="******",
                                rest_client=SimpleClient(),
                                helper=ICDXHelper(options={}))

        assert isinstance(getattr(obj, "_port"), int)
        assert getattr(obj, "_port") == 1

        obj_with_no_client = AMQPAsyncConsumer(host="192.168.1.254",
                                               port="notaPort",
                                               virtual_host="dx",
                                               username="******",
                                               amqp_password="******",
                                               rest_client=SimpleClient(),
                                               helper=123)
        assert isinstance(getattr(obj_with_no_client, "_port"), int)
        assert getattr(obj_with_no_client, "_port") == 5672
    def setup_observer(self):
        """
        Attempts to create an instance of the Consumer class and begin consuming forwarded messages.
        If the connection is closed unexpectedly, Consumer attempts to reopen it.

        AMQP sends hearbeats to keep a connection alive, if 2 consecutive heartbeats are missed
        the Consumer will completly stop.
        """

        helper = ICDXHelper(self.options)
        logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)

        # Init the Consumer, passing in an instance of RestClient and ICDXHelper as injected deps
        observer = AMQPAsyncConsumer(
            host=helper.get_config_option("icdx_amqp_host"),
            port=helper.get_config_option("icdx_amqp_port", True),
            virtual_host=helper.get_config_option("icdx_amqp_vhost"),
            username=helper.get_config_option("icdx_amqp_username"),
            amqp_password=helper.get_config_option("icdx_amqp_password"),
            rest_client=self.rest_client(),
            helper=ICDXHelper(self.options))
        """
        Start our consumer, will attempt a connection to ICDX
        When a connection is attempted a chain of functions are called
        Each of these functions has a callback which in turn will trigger the next part of the setup       
        Interfacing with Pika asynchronously is done by passing in callback methods you would like to have invoked
        when a certain event completes.
        
        The on_message function is triggered to potentially create an incident every time a new forwarded event
        comes off the message queue
        
        The below pattern is similar to how threads work wherein all logic is encapsulated -- we only call run()
        All functions are event-based which requires that the output of one function be the input to another.
        """
        try:
            # Create a thread which targets the observers run function
            # N.B note the lack of parentheses on observer.run
            res_daemon_thread = threading.Thread(target=observer.run)
            # Make the thread a daemon (background)
            res_daemon_thread.daemon = True
            # Start daemon thread in bg so rest of resilient-circuits is not blocked
            res_daemon_thread.start()

        except Exception as e:
            log.error(
                "Encountered an issue when starting the thread: {}".format(
                    str(e)))
 def test_partial_value(self):
     """Should raise an exception when AMQPAsyncConsumer is instantiated without enough params"""
     with pytest.raises(Exception) as e_info:
         obj = AMQPAsyncConsumer(host="192.168.1.254", port=1)
 def test_no_value(self):
     """Should raise an exception when AMQPAsyncConsumer is instantiated without params"""
     with pytest.raises(Exception) as e_info:
         obj = AMQPAsyncConsumer()