Exemplo n.º 1
0
def test_callback_var_args():
    holder = []
    d = Deferred()
    d.add_callback(lambda *args, **kwargs: holder.append((args, kwargs)), 2, 3, a=4, b=5)
    d.callback(1)

    assert holder[0] == ((1, 2, 3), {'a': 4, 'b': 5})
Exemplo n.º 2
0
def test_deferred_exception_catch():
    def dummy_errback(deferred_exception):
        deferred_exception.catch(OSError)
        return "caught"

    deferred = Deferred()
    deferred.add_errback(dummy_errback)
    deferred.errback(OSError())
    assert deferred.result == 'caught'
Exemplo n.º 3
0
def test_errback():
    holder = []
    d = Deferred()
    e = Exception('errback test')
    d.add_errback(lambda e: holder.append(e))
    d.errback(e)

    assert isinstance(holder[0], DeferredException)
    assert holder[0].value == e
Exemplo n.º 4
0
def test_callback_resolution():
    d = Deferred()
    d.add_callback(lambda r: fail(Exception(r + "b")))
    d.add_errback(lambda e: e.value.args[0] + "c")
    d.add_callbacks(lambda r: r + "d", lambda e: e.value.args[0] + 'f')

    d.callback("a")

    assert d.result == "abcd"
Exemplo n.º 5
0
def test_deferred_list():
    d = Deferred()

    dl = DeferredList([1, d])

    assert not dl.called
    d.callback(2)

    assert dl.called
    assert dl.result == [1, 2]
Exemplo n.º 6
0
def test_deferred_dict():
    d = Deferred()

    dd = DeferredDict({'a': 1, 'b': d})

    assert not dd.called
    d.callback(2)

    assert dd.called
    assert dd.result == {'a': 1, 'b': 2}
Exemplo n.º 7
0
def test_errback_chain():
    holder = []
    d = Deferred()
    e = Exception('a')
    d.add_callbacks(holder.append, lambda e: Exception(e.value.args[0] + 'b'))
    d.add_callbacks(holder.append, lambda e: Exception(e.value.args[0] + 'c'))

    d.errback(e)

    assert d.result.value.args[0] == 'abc'
    assert len(holder) == 0
Exemplo n.º 8
0
def test_callback_raises_exception():
    def callback(val):
        raise AttributeError(val)

    d = Deferred()
    d.add_callback(callback)
    d.callback('test')

    assert isinstance(d.result, DeferredException)
    assert isinstance(d.result.value, AttributeError)
    assert d.result.value.args[0] == "test"
Exemplo n.º 9
0
def test_callback_resolution():
    d = Deferred()
    d.add_callback(lambda r: fail(Exception(r + "b")))
    d.add_errback(lambda e: e.value.args[0] + "c")
    d.add_callbacks(lambda r: r + "d", lambda e: e.value.args[0] + 'f')

    d.callback("a")

    assert d.result == "abcd"
Exemplo n.º 10
0
def test_deferred_list():
    d = Deferred()

    dl = DeferredList([
        1,
        d
    ])

    assert not dl.called
    d.callback(2)

    assert dl.called
    assert dl.result == [1, 2]
Exemplo n.º 11
0
def test_deferred_dict():
    d = Deferred()

    dd = DeferredDict({
        'a': 1,
        'b': d
    })

    assert not dd.called
    d.callback(2)

    assert dd.called
    assert dd.result == {'a': 1, 'b': 2}
Exemplo n.º 12
0
def test_callback_resolution_weaving():
    d = Deferred()
    d.add_callbacks(lambda r: fail(Exception(r + "b")), lambda e: e.value.args[0] + 'w')
    d.add_callbacks(lambda e: Exception(e + "x"), lambda e: e.value.args[0] + "c")
    d.add_callbacks(lambda r: Exception(r + "d"), lambda e: e.value.args[0] + 'y')
    d.add_callbacks(lambda r: r + "z", lambda e: e.value.args[0] + 'e')

    d.callback("a")

    assert d.result == "abcde"
Exemplo n.º 13
0
def test_deferred_callback_returns_another_deferred():
    d = Deferred()
    d2 = Deferred()

    d.add_callback(lambda r: succeed(r + 5).add_callback(lambda v: v + 5))
    d.add_callback(lambda r: d2)
    d.callback(5)

    assert d.result is d2
    assert d.paused
    assert d.called

    d2.callback(7)
    assert d.result == 7
    assert d2.result == 7
Exemplo n.º 14
0
def test_callback_resolution_weaving_2():
    d = Deferred()
    d.add_callbacks(lambda r: fail(Exception(r + "b")), lambda e: e.value.args[0] + 'w')
    d.add_callbacks(lambda e: Exception(e + "x"), lambda e: e.value.args[0] + "c")
    d.add_callbacks(lambda r: Exception(r + "d"), lambda e: e.value.args[0] + 'y')
    d.add_callbacks(lambda r: fail(ValueError(r + "z")), lambda e: e.value.args[0] + 'e')

    d.errback(Exception('v'))

    assert isinstance(d.result, DeferredException)
    assert isinstance(d.result.value, ValueError)
    assert d.result.value.args[0] == "vwxyz"
Exemplo n.º 15
0
    def run_resolve_fn(self, resolver, original_resolver):
        result = resolver()
        if isinstance(result, Future) or iscoroutine(result):
            future = ensure_future(result)
            d = Deferred()
            future.add_done_callback(process_future_result(d))
            return d

        return result
Exemplo n.º 16
0
def test_deferred_exception_catch():
    def dummy_errback(deferred_exception):
        deferred_exception.catch(OSError)
        return "caught"

    deferred = Deferred()
    deferred.add_errback(dummy_errback)
    deferred.errback(OSError())
    assert deferred.result == 'caught'
Exemplo n.º 17
0
def test_errback():
    holder = []
    d = Deferred()
    e = Exception('errback test')
    d.add_errback(lambda e: holder.append(e))
    d.errback(e)

    assert isinstance(holder[0], DeferredException)
    assert holder[0].value == e
Exemplo n.º 18
0
def test_callback_var_args():
    holder = []
    d = Deferred()
    d.add_callback(lambda *args, **kwargs: holder.append((args, kwargs)),
                   2,
                   3,
                   a=4,
                   b=5)
    d.callback(1)

    assert holder[0] == ((1, 2, 3), {'a': 4, 'b': 5})
Exemplo n.º 19
0
def test_errback_chain():
    holder = []
    d = Deferred()
    e = Exception('a')
    d.add_callbacks(holder.append, lambda e: Exception(e.value.args[0] + 'b'))
    d.add_callbacks(holder.append, lambda e: Exception(e.value.args[0] + 'c'))

    d.errback(e)

    assert d.result.value.args[0] == 'abc'
    assert len(holder) == 0
Exemplo n.º 20
0
def test_callback_raises_exception():
    def callback(val):
        raise AttributeError(val)

    d = Deferred()
    d.add_callback(callback)
    d.callback('test')

    assert isinstance(d.result, DeferredException)
    assert isinstance(d.result.value, AttributeError)
    assert d.result.value.args[0] == "test"
Exemplo n.º 21
0
def test_deferred_list_fails():
    d1 = Deferred()
    d2 = Deferred()
    d3 = Deferred()

    dl = DeferredList([
        1,
        succeed(2),
        d1,
        d2,
        d3
    ])

    assert not dl.called

    e1 = Exception('d1 failed')
    d1.errback(e1)
    d2.errback(Exception('d2 failed'))
    d3.callback('hello')

    assert dl.called
    assert isinstance(dl.result, DeferredException)
    assert dl.result.value == e1
Exemplo n.º 22
0
def test_callback_resolution_weaving():
    d = Deferred()
    d.add_callbacks(lambda r: fail(Exception(r + "b")),
                    lambda e: e.value.args[0] + 'w')
    d.add_callbacks(lambda e: Exception(e + "x"),
                    lambda e: e.value.args[0] + "c")
    d.add_callbacks(lambda r: Exception(r + "d"),
                    lambda e: e.value.args[0] + 'y')
    d.add_callbacks(lambda r: r + "z", lambda e: e.value.args[0] + 'e')

    d.callback("a")

    assert d.result == "abcde"
Exemplo n.º 23
0
def test_cant_callback_twice():
    d1 = Deferred()
    d1.callback('hello')

    with raises(AlreadyCalledDeferred):
        d1.callback('world')
Exemplo n.º 24
0
def test_cant_errback_twice():
    d1 = Deferred()
    d1.errback(Exception('hello'))

    with raises(AlreadyCalledDeferred):
        d1.errback(Exception('world'))
Exemplo n.º 25
0
def test_deferred_callback_returns_another_deferred():
    d = Deferred()
    d2 = Deferred()

    d.add_callback(lambda r: succeed(r + 5).add_callback(lambda v: v + 5))
    d.add_callback(lambda r: d2)
    d.callback(5)

    assert d.result is d2
    assert d.paused
    assert d.called

    d2.callback(7)
    assert d.result == 7
    assert d2.result == 7
Exemplo n.º 26
0
def test_callbacks_and_errbacks_return_original_deferred():
    d = Deferred()
    assert d.add_callback(lambda a: None) is d
    assert d.add_errback(lambda a: None) is d
    assert d.add_callbacks(lambda a: None, lambda a: None) is d
Exemplo n.º 27
0
def test_cant_errback_twice():
    d1 = Deferred()
    d1.errback(Exception('hello'))

    with raises(AlreadyCalledDeferred):
        d1.errback(Exception('world'))
Exemplo n.º 28
0
def test_cant_callback_twice():
    d1 = Deferred()
    d1.callback('hello')

    with raises(AlreadyCalledDeferred):
        d1.callback('world')
Exemplo n.º 29
0
def test_deferred_list_fails():
    d1 = Deferred()
    d2 = Deferred()
    d3 = Deferred()

    dl = DeferredList([1, succeed(2), d1, d2, d3])

    assert not dl.called

    e1 = Exception('d1 failed')
    d1.errback(e1)
    d2.errback(Exception('d2 failed'))
    d3.callback('hello')

    assert dl.called
    assert isinstance(dl.result, DeferredException)
    assert dl.result.value == e1
Exemplo n.º 30
0
def test_callback_resolution_weaving_2():
    d = Deferred()
    d.add_callbacks(lambda r: fail(Exception(r + "b")),
                    lambda e: e.value.args[0] + 'w')
    d.add_callbacks(lambda e: Exception(e + "x"),
                    lambda e: e.value.args[0] + "c")
    d.add_callbacks(lambda r: Exception(r + "d"),
                    lambda e: e.value.args[0] + 'y')
    d.add_callbacks(lambda r: fail(ValueError(r + "z")),
                    lambda e: e.value.args[0] + 'e')

    d.errback(Exception('v'))

    assert isinstance(d.result, DeferredException)
    assert isinstance(d.result.value, ValueError)
    assert d.result.value.args[0] == "vwxyz"
Exemplo n.º 31
0
def test_callbacks_and_errbacks_return_original_deferred():
    d = Deferred()
    assert d.add_callback(lambda a: None) is d
    assert d.add_errback(lambda a: None) is d
    assert d.add_callbacks(lambda a: None, lambda a: None) is d