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
    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'
 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
示例#4
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
    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'
    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'
    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