Ejemplo n.º 1
0
    def __init__(self, message_queue, config=None):
        self.functions = {}
        self.is_running = False
        self.message_queue = message_queue

        if config is None:
            self.config = get_config()["handlers"]["multi_thread"]
        else:
            self.config = config
Ejemplo n.º 2
0
    def test_merges_config_with_default_config(self):
        config.set_config({"handlers": {"multi_thread": {"thread_count": 10}}})

        multi_thread_config = config.get_config()["handlers"]["multi_thread"]
        self.assertEqual(multi_thread_config["thread_count"], 10)
        default_timeout = DEFAULTS["handlers"]["multi_thread"][
            "queue_listen_timeout"]
        self.assertEqual(multi_thread_config["queue_listen_timeout"],
                         default_timeout)
Ejemplo n.º 3
0
    def test_starts_multiple_threads(self):
        multi_thread_handler = MultiThreadHandler(Queue())
        multi_thread_handler.start()

        thread_count = get_config()["handlers"]["multi_thread"]["thread_count"]
        try:
            wait_until_success(lambda: self.assertTrue(threading.active_count(
            ) >= thread_count + 1))
        finally:
            multi_thread_handler.stop()
Ejemplo n.º 4
0
    def __init__(self, message_queue, config=None):
        self.is_running = False
        if config is None:
            self.config = get_config()["redis"]
        else:
            self.config = config

        self.message_queue = message_queue

        self.handlers = []
        self.redis_client = redis.StrictRedis(host=self.config["host"],
                                              port=self.config["port"])
Ejemplo n.º 5
0
    def test_executes_events(self):
        queue = Queue()

        first_queue_mock = Mock()
        queue_name = "queue"

        multi_thread_handler = MultiThreadHandler(queue)
        multi_thread_handler.add_function(queue_name, first_queue_mock)

        event_count = 10
        for i in range(0, 10):
            queue.put_nowait(Event(queue_name, "some data"))

        try:
            multi_thread_handler.start()
            wait_until_success(lambda: self.assertEqual(
                first_queue_mock.call_count, event_count))

            thread_count = get_config(
            )["handlers"]["multi_thread"]["thread_count"]
            self.assertTrue(threading.active_count() >= thread_count + 1)
        finally:
            multi_thread_handler.stop()
Ejemplo n.º 6
0
    def test_catch_all_errors_from_user_functions(self):
        def fail_if_one(callback, number):
            if number == 1:
                raise Exception("Boom!")
            callback()

        queue = Queue()
        queue_name = "queue"
        success_callback = Mock()

        handler_config = get_config()["handlers"]["multi_thread"]
        handler_config["thread_count"] = 1
        multi_thread_handler = MultiThreadHandler(queue, config=handler_config)
        multi_thread_handler.add_function(
            queue_name, partial(fail_if_one, success_callback))

        queue.put_nowait(Event(queue_name, 1))
        queue.put_nowait(Event(queue_name, 2))
        try:
            multi_thread_handler.start()
            wait_until_success(
                lambda: self.assertTrue(success_callback.called))
        finally:
            multi_thread_handler.stop()
Ejemplo n.º 7
0
 def test_sets_config(self):
     config.set_config({"extra_config": "foobar"})
     self.assertEqual(config.get_config()["extra_config"], "foobar")
Ejemplo n.º 8
0
 def test_raises_error_if_config_not_set(self):
     with self.assertRaises(config.ConfigurationException):
         config.get_config()