Esempio n. 1
0
    def test_promise_safe_guard(self, capsys):
        """Use the safeguard() on a failing promise.

        It ensures the error is logged in the stdout, with sufficient details.

        """
        class Err(Exception):
            pass

        logging.basicConfig(stream=sys.stdout)

        def in_promise(a, b):
            msg = 'ERROR'
            raise Err(msg)

        p = Promise(in_promise)

        def callback(value):
            assert value is 3

        p.then(callback)
        p.safeguard()

        out, err = capsys.readouterr()
        assert '[SAFEGUARD]' in out
Esempio n. 2
0
    def test_then_with_error_callback_only_on_fulfilled_promise(self):
        """Chain a successful Promise with only the error callback."""
        def on_error(err):
            raise Exception('This should never be called!')

        p = Promise(lambda ok, error: ok(185))
        p2 = p.then(None, on_error)
        assert p2.result(0.01) is 185
Esempio n. 3
0
    def test_then_sync_call(self):
        """Test to chain a callback using then() on an fulfilled Promise."""

        def _callback(arg):
            assert arg is 23
            return arg * 2

        p = Promise(lambda ok, error: ok(23))
        p2 = p.then(_callback)
        assert p2.result(0.01) is 46
        assert p.result(0.01) is 23
Esempio n. 4
0
    def test_then_with_error_callback_on_fulfilled_promise(self):
        """Chain a Promise with both success and error callbacks.
        Only the success callback should be called.
        """
        def on_success(value):
            return value * 3

        def on_error(err):
            raise Exception('This should never be called!')

        p = Promise(lambda ok, error: ok(654))
        p2 = p.then(on_success, on_error)
        assert p2.result(0.01) == 1962
Esempio n. 5
0
    def test_then_with_promise_factory(self):
        """Chain a Future factory, using then().

        a Promise factory is a function who returns a Promise.
        """

        def factory(value):
            x = Promise(lambda ok, error: ok(value * value))
            return x

        p = Promise(lambda ok, error: ok(6))
        p2 = p.then(factory)
        assert p2.result(0.01) is 36
Esempio n. 6
0
    def test_then_with_error_callback_only_on_rejected_promise(self):
        """Chain a failing promise with only the error callback."""
        class MyException(Exception):
            pass

        def task(ok, error):
            raise MyException()

        def on_error(err):
            assert isinstance(err, MyException)
            return 8

        p = Promise(task)
        p2 = p.then(None, on_error)
        assert p2.result(0.01) is 8
Esempio n. 7
0
    def test_then_on_failing_sync_promise(self):
        """Chain a callback using then() to a Promise raising an exception"""
        class Err(Exception):
            pass

        def _task(ok, error):
            raise Err()

        def _callback(__):
            assert not 'This should not be executed.'

        p = Promise(_task)
        p2 = p.then(_callback)
        with pytest.raises(Err):
            p2.result(0.01)
Esempio n. 8
0
    def test_then_async_call(self):
        """Chain a callback using then() on an not-yet fulfilled Promise."""

        _fulfill = []

        def _init(ok, error):
            _fulfill.append(ok)

        def _callback(arg):
            assert arg is 23
            return arg * 2

        p = Promise(_init)
        p2 = p.then(_callback)
        _fulfill[0](23)  # fulfill the Promise now.
        assert p2.result(0.01) is 46
        assert p.result(0.01) is 23
Esempio n. 9
0
    def test_then_on_failing_async_promise(self):
        """Chain a callback using then() to a Promise who will be rejected."""
        class Err(Exception):
            pass

        _reject = []

        def _task(ok, error):
            _reject.append(error)

        def _callback(__):
            assert not 'This should not be executed.'

        p = Promise(_task)
        p2 = p.then(_callback)
        _reject[0](Err())  # fulfill the Promise now.
        with pytest.raises(Err):
            p2.result()
Esempio n. 10
0
    def test_then_with_error_callback_on_rejected_promise(self):
        """Chain a Promise with both success and error callbacks.

        Only the error callback should be called.
        The resulting Promise will (successfully) resolve with the value
        returned by the error callback.
        """
        class MyException(Exception):
            pass

        def task(ok, error):
            error(MyException())

        def on_success(value):
            raise Exception('This should never be called!')

        def on_error(err):
            assert type(err) is MyException
            return 38

        p = Promise(task)
        p2 = p.then(on_success, on_error)
        assert p2.result(0.01) is 38