コード例 #1
0
    def __run_checks(self, app, context, groups):
        """Returns a list of tuples containing a Group object, a Check object, and a
        Reporter object.

        :param app (App Object) - An App object that represents the Splunk App
            object
        """
        futures = []
        with concurrent.futures.ThreadPoolExecutor(max_workers=2) as threadpool:
            ready_for_deferred = ThenableFuture()

            logger.debug("Beginning validation execution.")
            for group in groups:
                logger.debug(("Executing start_group event for"
                              " Group: {}"
                              " Group_Checks: {}"
                              " Listeners: {}"
                              ).format(group,
                                       list(group.checks()),
                                       self.listeners))

                self.__emit_event('start_group', self.listeners, group, group.checks())
                # This runs the initial checks
                future_checks = map(lambda check: (check, self.__dispatch_check(ready_for_deferred, threadpool, context, app, check)),
                                    group.checks())
                # This accumulates the deferred checks
                futures.append((group, future_checks))

                logger.debug(("Executing finish_group event for"
                              " Group: {}"
                              " Group_Checks: {}"
                              " Listeners: {}"
                              ).format(group,
                                       list(group.checks()),
                                       self.listeners))
                self.__emit_event('finish_group', self.listeners, group, group.checks())

            # This allows the deferred checks to be run
            ready_for_deferred.set_result(True)

        # After exiting 'with', all checks are run.
        # future.result() calls a promise that returns the reporter
        return_values = [(group_object, check_object, future.result())
                         for group_object, checks
                         in futures
                         for check_object, future
                         in checks]
        return return_values
コード例 #2
0
    def test_err_callback_and_failure_repackage(self):

        class _MyException(Exception):
            pass

        class _MyRepackagedException(Exception):
            pass

        class _NotMatched(Exception):
            pass

        base_f = ThenableFuture()
        new_f = base_f.then(
            None,
            lambda ex: _MyRepackagedException(ex.message + ' repackaged') if isinstance(ex, _MyException) else _NotMatched('WTF?')
        )

        assert base_f is not new_f

        assert not base_f.done()
        assert not new_f.done()

        base_f.set_exception(_MyException('sad'))

        assert base_f.done()
        assert new_f.done()

        assert new_f.exception()
        with self.assertRaises(_MyRepackagedException) as catcher:
            new_f.result()
        assert catcher.exception.message == 'sad repackaged'
コード例 #3
0
    def test_chained_success_callback_and_success(self):

        base_f = ThenableFuture()

        def _transform(value):
            """
            :param int value:
            :rtype: int
            """
            f = ThenableFuture()
            if value < 5:
                f.set_result(_transform(value+1))
            else:
                f.set_result(value)
            return f

        new_f = base_f.then(_transform)

        assert base_f is not new_f

        assert not base_f.done()
        assert not new_f.done()

        base_f.set_result(1)

        assert base_f.done()
        assert new_f.done()

        assert not new_f.exception()
        assert new_f.result() == 5
コード例 #4
0
    def test_success_callback_and_failure_raised(self):

        class _MyException(Exception):
            pass

        def raise_something_else(value):
            raise _MyException(
                value + ' repackaged'
            )

        base_f = ThenableFuture()
        new_f = base_f.then(
            raise_something_else
        )

        assert base_f is not new_f

        assert not base_f.done()
        assert not new_f.done()

        base_f.set_result('sad')

        assert base_f.done()
        assert new_f.done()

        assert new_f.exception()
        with self.assertRaises(_MyException) as catcher:
            new_f.result()
        assert catcher.exception.message == 'sad repackaged'
コード例 #5
0
    def test_err_callback_convert_to_success(self):

        class _MyException(Exception):
            pass

        class _NotMatched(Exception):
            pass

        base_f = ThenableFuture()
        new_f = base_f.then(
            None,
            lambda ex: ex.message + ' repackaged' if isinstance(ex, _MyException) else _NotMatched('WTF?')
        )

        assert base_f is not new_f

        assert not base_f.done()
        assert not new_f.done()

        base_f.set_exception(_MyException('sad'))

        assert base_f.done()
        assert new_f.done()

        assert not new_f.exception()
        assert new_f.result() == 'sad repackaged'
コード例 #6
0
 def _transform(value):
     """
     :param int value:
     :rtype: int
     """
     f = ThenableFuture()
     if value < 5:
         f.set_result(_transform(value+1))
     else:
         f.set_result(value)
     return f
コード例 #7
0
    def test_detect_circular_chains(self):

        base_f = ThenableFuture()

        f1 = ThenableFuture()
        f2 = ThenableFuture()

        chain = [f1, f2, f1]

        def _transform(a):
            """
            :param int value:
            :rtype: int
            """
            try:
                f = chain.pop(0)
                f.set_result(_transform(a))
                return f
            except IndexError:
                return 5

        new_f = base_f.then(_transform)

        assert base_f is not new_f

        assert not base_f.done()
        assert not new_f.done()

        base_f.set_result(1)

        assert base_f.done()
        assert new_f.done()

        assert new_f.exception()
        with self.assertRaises(CircularFuturesChainException) as catcher:
            new_f.result()

        assert 'Circular Futures chain detected' in catcher.exception.message
コード例 #8
0
    def test_no_callbacks_and_success(self):

        base_f = ThenableFuture()
        new_f = base_f.then()

        assert base_f is not new_f

        assert not base_f.done()
        assert not new_f.done()

        base_f.set_result('done')

        assert base_f.done()
        assert new_f.done()

        assert not new_f.exception()
        assert new_f.result() == 'done'
コード例 #9
0
    def test_success_callback_and_success(self):

        base_f = ThenableFuture()
        new_f = base_f.then(
            lambda result: result + ' manipulated'
        )

        assert base_f is not new_f

        assert not base_f.done()
        assert not new_f.done()

        base_f.set_result('done')

        assert base_f.done()
        assert new_f.done()

        assert not new_f.exception()
        assert new_f.result() == 'done manipulated'
コード例 #10
0
    def test_no_callbacks_and_failure(self):

        base_f = ThenableFuture()
        new_f = base_f.then()

        assert base_f is not new_f

        assert not base_f.done()
        assert not new_f.done()

        class _MyException(Exception):
            pass

        base_f.set_exception(_MyException('sad'))

        assert base_f.done()
        assert new_f.done()

        assert new_f.exception()
        with self.assertRaises(_MyException) as catcher:
            new_f.result()
        assert catcher.exception.message == 'sad'