Ejemplo n.º 1
0
def test_done_all():
    counter = [0]

    def inc(_):
        counter[0] += 1

    def dec(_):
        counter[0] -= 1

    p = Promise()
    p.done_all()
    p.done_all((inc, dec))
    p.done_all([
        (inc, dec),
        (inc, dec),
        {'success': inc, 'failure': dec},
    ])
    p.fulfill(4)

    assert counter[0] == 4

    p = Promise()
    p.done_all()
    p.done_all((inc, dec))
    p.done_all([
        (inc, dec),
        {'success': inc, 'failure': dec},
    ])
    p.reject(Exception())

    assert counter[0] == 1
Ejemplo n.º 2
0
def test_dict_promise_when(promise_for_dict):
    p1 = Promise()
    p2 = Promise()
    d = {"a": p1, "b": p2}
    pd1 = promise_for_dict(d)
    pd2 = promise_for_dict({"a": p1})
    pd3 = promise_for_dict({})
    assert p1.is_pending
    assert p2.is_pending
    assert pd1.is_pending
    assert pd2.is_pending
    assert pd3.is_fulfilled
    p1.fulfill(5)
    assert p1.is_fulfilled
    assert p2.is_pending
    assert pd1.is_pending
    assert pd2.is_fulfilled
    p2.fulfill(10)
    assert p1.is_fulfilled
    assert p2.is_fulfilled
    assert pd1.is_fulfilled
    assert pd2.is_fulfilled
    assert 5 == p1.value
    assert 10 == p2.value
    assert 5 == pd1.value["a"]
    assert 5 == pd2.value["a"]
    assert 10 == pd1.value["b"]
    assert {} == pd3.value
Ejemplo n.º 3
0
def test_then_all():
    p = Promise()

    handlers = [
        ((lambda x: x * x), (lambda r: 1)),
        {'success': (lambda x: x + x), 'failure': (lambda r: 2)},
    ]

    results = p.then_all() + p.then_all(((lambda x: x * x), (lambda r: 1))) + p.then_all(handlers)

    p.fulfill(4)

    assert [r.value for r in results] == [16, 16, 8]

    p = Promise()

    handlers = [
        ((lambda x: x * x), (lambda r: 1)),
        {'success': (lambda x: x + x), 'failure': (lambda r: 2)},
    ]

    results = p.then_all() + p.then_all(((lambda x: x * x), (lambda r: 1))) + p.then_all(handlers)

    p.reject(Exception())

    assert [r.value for r in results] == [1, 1, 2]
Ejemplo n.º 4
0
def test_promise_all_if():
    p1 = Promise()
    p2 = Promise()
    pd1 = Promise.all([p1, p2])
    pd2 = Promise.all([p1])
    pd3 = Promise.all([])
    assert p1.is_pending
    assert p2.is_pending
    assert pd1.is_pending
    assert pd2.is_pending
    assert pd3.is_fulfilled
    p1.fulfill(5)
    assert p1.is_fulfilled
    assert p2.is_pending
    assert pd1.is_pending
    assert pd2.is_fulfilled
    p2.fulfill(10)
    assert p1.is_fulfilled
    assert p2.is_fulfilled
    assert pd1.is_fulfilled
    assert pd2.is_fulfilled
    assert 5 == p1.value
    assert 10 == p2.value
    assert 5 == pd1.value[0]
    assert 5 == pd2.value[0]
    assert 10 == pd1.value[1]
    assert [] == pd3.value
Ejemplo n.º 5
0
def test_type_resolver_promise(type, value, expected):
    promise_value = Promise()
    resolver = type_resolver(type, lambda: promise_value)
    resolved_promise = resolver()
    assert not resolved_promise.is_fulfilled
    promise_value.fulfill(value)
    assert resolved_promise.is_fulfilled
    resolved = resolved_promise.get()
    assert resolved == expected
Ejemplo n.º 6
0
def test_promisify_promise_subclass():
    class MyPromise(Promise):
        pass

    p = Promise()
    p.fulfill(10)
    m_p = MyPromise.promisify(p)
    assert isinstance(m_p, MyPromise)
    assert m_p.get() == p.get()
Ejemplo n.º 7
0
def test_3_2_1():
    """
    Test that the arguments to 'then' are optional.
    """

    p1 = Promise()
    p2 = p1.then()
    p3 = Promise()
    p4 = p3.then()
    p1.fulfill(5)
    p3.reject(Exception("How dare you!"))
Ejemplo n.º 8
0
def test_3_2_6_4_fulfilled():
    """
    Handles the case where the arguments to then
    are values, not functions or promises.
    """
    p1 = Promise()
    p1.fulfill(10)
    p2 = p1.then(5)
    assert 10 ==  p1.value
    assert p2.is_fulfilled
    assert 10 ==  p2.value
Ejemplo n.º 9
0
def test_3_2_6_4_pending():
    """
    Handles the case where the arguments to then
    are not functions or promises.
    """
    p1 = Promise()
    p2 = p1.then(5)
    p1.fulfill(10)
    assert 10 == p1.get()
    p2.wait()
    assert p2.is_fulfilled
    assert 10 == p2.get()
Ejemplo n.º 10
0
def test_3_2_3_3():
    """
    Make sure rejected callback never called if promise is fulfilled
    """

    cf = Counter()
    cr = Counter()
    p1 = Promise()
    p2 = p1.then(lambda v: cf.tick(), lambda r: cr.tick())
    p1.fulfill(5)
    assert 0 ==  cr.value()
    assert 1 ==  cf.value()
Ejemplo n.º 11
0
def test_exceptions():
    def throws(v):
        assert False

    p1 = Promise()
    p1.add_callback(throws)
    p1.fulfill(5)

    p2 = Promise()
    p2.add_errback(throws)
    p2.reject(Exception())

    with pytest.raises(Exception) as excinfo:
        p2.get()
Ejemplo n.º 12
0
def test_exceptions():
    def throws(v):
        assert False

    p1 = Promise()
    p1.add_callback(throws)
    p1.fulfill(5)

    p2 = Promise()
    p2.add_errback(throws)
    p2.reject(Exception())

    with raises(Exception) as excinfo:
        p2.get()
Ejemplo n.º 13
0
def test_3_2_6_1():
    """
    Promises returned by then must be fulfilled when the promise
    they are chained from is fulfilled IF the fulfillment value
    is not a promise.
    """

    p1 = Promise()
    pf = p1.then(lambda v: v * v)
    p1.fulfill(5)
    assert pf.value ==  25

    p2 = Promise()
    pr = p2.then(None, lambda r: 5)
    p2.reject(Exception("Error"))
    assert 5 ==  pr.value
Ejemplo n.º 14
0
def test_3_2_2_1():
    """
    The first argument to 'then' must be called when a promise is
    fulfilled.
    """

    c = Counter()

    def check(v, c):
        assert v ==  5
        c.tick()

    p1 = Promise()
    p2 = p1.then(lambda v: check(v, c))
    p1.fulfill(5)
    assert 1 ==  c.value()
Ejemplo n.º 15
0
def test_3_2_2_2():
    """
    Make sure callbacks are never called more than once.
    """

    c = Counter()
    p1 = Promise()
    p2 = p1.then(lambda v: c.tick())
    p1.fulfill(5)
    try:
        # I throw an exception
        p1.fulfill(5)
        assert False  # Should not get here!
    except AssertionError:
        # This is expected
        pass
    assert 1 ==  c.value()
Ejemplo n.º 16
0
def test_promise_all_when_mixed_promises():
    p1 = Promise()
    p2 = Promise()
    pl = Promise.all([p1, 32, p2, False, True])
    assert p1.is_pending
    assert p2.is_pending
    assert pl.is_pending
    p1.fulfill(5)
    assert p1.is_fulfilled
    assert p2.is_pending
    assert pl.is_pending
    p2.fulfill(10)
    assert p1.is_fulfilled
    assert p2.is_fulfilled
    assert pl.is_fulfilled
    assert 5 == p1.value
    assert 10 == p2.value
    assert pl.value == [5, 32, 10, False, True]
Ejemplo n.º 17
0
def test_promise_all_when_mixed_promises():
    p1 = Promise()
    p2 = Promise()
    pl = Promise.all([p1, 32, p2, False, True])
    assert p1.is_pending
    assert p2.is_pending
    assert pl.is_pending
    p1.fulfill(5)
    assert p1.is_fulfilled
    assert p2.is_pending
    assert pl.is_pending
    p2.fulfill(10)
    assert p1.is_fulfilled
    assert p2.is_fulfilled
    assert pl.is_fulfilled
    assert 5 == p1.get()
    assert 10 == p2.get()
    assert pl.get() == [5, 32, 10, False, True]
Ejemplo n.º 18
0
def test_3_2_5_1_if():
    """
    Then can be called multiple times on the same promise
    and callbacks must be called in the order of the
    then calls.
    """

    def add(l, v):
        l.append(v)

    p1 = Promise()
    p1.fulfill(2)
    order = []
    p2 = p1.then(lambda v: add(order, "p2"))
    p3 = p1.then(lambda v: add(order, "p3"))
    assert 2 ==  len(order)
    assert "p2" ==  order[0]
    assert "p3" ==  order[1]
Ejemplo n.º 19
0
def test_promise_all_when():
    p1 = Promise()
    p2 = Promise()
    pl = Promise.all([p1, p2])
    assert p1.is_pending
    assert p2.is_pending
    assert pl.is_pending
    p1.fulfill(5)
    assert p1.is_fulfilled
    assert p2.is_pending
    assert pl.is_pending
    p2.fulfill(10)
    assert p1.is_fulfilled
    assert p2.is_fulfilled
    assert pl.is_fulfilled
    assert 5 == p1.get()
    assert 10 == p2.get()
    assert 5 == pl.get()[0]
    assert 10 == pl.get()[1]
Ejemplo n.º 20
0
def test_promise_all_when():
    p1 = Promise()
    p2 = Promise()
    pl = Promise.all([p1, p2])
    assert p1.is_pending
    assert p2.is_pending
    assert pl.is_pending
    p1.fulfill(5)
    assert p1.is_fulfilled
    assert p2.is_pending
    assert pl.is_pending
    p2.fulfill(10)
    assert p1.is_fulfilled
    assert p2.is_fulfilled
    assert pl.is_fulfilled
    assert 5 == p1.value
    assert 10 == p2.value
    assert 5 == pl.value[0]
    assert 10 == pl.value[1]
Ejemplo n.º 21
0
def test_3_2_6_2_if():
    """
    Promises returned by then must be rejected when any of their
    callbacks throw an exception.
    """

    def fail(v):
        raise AssertionError("Exception Message")

    p1 = Promise()
    p1.fulfill(5)
    pf = p1.then(fail)
    assert pf.is_rejected
    assert_exception(pf.reason, AssertionError, "Exception Message")

    p2 = Promise()
    p2.reject(Exception("Error"))
    pr = p2.then(None, fail)
    assert pr.is_rejected
    assert_exception(pr.reason, AssertionError, "Exception Message")
Ejemplo n.º 22
0
def test_dict_promise_if(promise_for_dict):
    p1 = Promise()
    p2 = Promise()
    d = {"a": p1, "b": p2}
    pd = promise_for_dict(d)
    assert p1.is_pending
    assert p2.is_pending
    assert pd.is_pending
    p1.fulfill(5)
    assert p1.is_fulfilled
    assert p2.is_pending
    assert pd.is_pending
    p2.fulfill(10)
    assert p1.is_fulfilled
    assert p2.is_fulfilled
    assert pd.is_fulfilled
    assert 5 == p1.get()
    assert 10 == p2.get()
    assert 5 == pd.get()["a"]
    assert 10 == pd.get()["b"]
Ejemplo n.º 23
0
def test_dict_promise_if(promise_for_dict):
    p1 = Promise()
    p2 = Promise()
    d = {"a": p1, "b": p2}
    pd = promise_for_dict(d)
    assert p1.is_pending
    assert p2.is_pending
    assert pd.is_pending
    p1.fulfill(5)
    assert p1.is_fulfilled
    assert p2.is_pending
    assert pd.is_pending
    p2.fulfill(10)
    assert p1.is_fulfilled
    assert p2.is_fulfilled
    assert pd.is_fulfilled
    assert 5 == p1.value
    assert 10 == p2.value
    assert 5 == pd.value["a"]
    assert 10 == pd.value["b"]
Ejemplo n.º 24
0
def test_done():
    counter = [0]

    def inc(_):
        counter[0] += 1

    def dec(_):
        counter[0] -= 1

    p = Promise()
    p.done(inc, dec)
    p.fulfill(4)

    assert counter[0] == 1

    p = Promise()
    p.done(inc, dec)
    p.done(inc, dec)
    p.reject(Exception())

    assert counter[0] == -1
Ejemplo n.º 25
0
def test_done():
    counter = [0]

    def inc(_):
        counter[0] += 1

    def dec(_):
        counter[0] -= 1

    p = Promise()
    p.done(inc, dec)
    p.fulfill(4)

    assert counter[0] == 1

    p = Promise()
    p.done(inc, dec)
    p.done(inc, dec)
    p.reject(Exception())

    assert counter[0] == -1
Ejemplo n.º 26
0
def test_3_2_6_3_if_fulfilled():
    """
    Testing return of pending promises to make
    sure they are properly chained.
    This covers the case where the root promise
    is fulfilled before the chaining is defined.
    """

    p1 = Promise()
    p1.fulfill(10)
    pending = Promise()
    pending.fulfill(5)
    pf = p1.then(lambda r: pending)
    assert pending.is_fulfilled
    assert 5 ==  pending.value
    assert pf.is_fulfilled
    assert 5 ==  pf.value

    p2 = Promise()
    p2.fulfill(10)
    bad = Promise()
    bad.reject(Exception("Error"))
    pr = p2.then(lambda r: bad)
    assert_exception(bad.reason, Exception, "Error")
    assert pr.is_rejected
    assert_exception(pr.reason, Exception, "Error")
Ejemplo n.º 27
0
def test_done_all():
    counter = [0]

    def inc(_):
        counter[0] += 1

    def dec(_):
        counter[0] -= 1

    p = Promise()
    p.done_all()
    p.done_all([(inc, dec)])
    p.done_all([
        (inc, dec),
        (inc, dec),
        {
            'success': inc,
            'failure': dec
        },
    ])
    p.fulfill(4)

    assert counter[0] == 4

    p = Promise()
    p.done_all()
    p.done_all([inc])
    p.done_all([(inc, dec)])
    p.done_all([
        (inc, dec),
        {
            'success': inc,
            'failure': dec
        },
    ])
    p.reject(Exception())

    assert counter[0] == 1
Ejemplo n.º 28
0
def test_fulfill_self():
    p = Promise()
    with raises(TypeError) as excinfo:
        p.fulfill(p).get()
Ejemplo n.º 29
0
def test_fake_promise():
    p = Promise()
    p.fulfill(FakeThenPromise())
    assert p.is_rejected
    assert_exception(p.reason, Exception, "FakeThenPromise raises in 'then'")
Ejemplo n.º 30
0
def test_get_if():
    p1 = Promise()
    p1.fulfill(5)
    v = p1.get()
    assert p1.is_fulfilled
    assert 5 == v
Ejemplo n.º 31
0
def test_fulfill_self():
    p = Promise()
    with pytest.raises(TypeError) as excinfo:
        p.fulfill(p)
Ejemplo n.º 32
0
def test_wait_if():
    p1 = Promise()
    p1.fulfill(5)
    p1.wait()
    assert p1.is_fulfilled
Ejemplo n.º 33
0
def test_get_if():
    p1 = Promise()
    p1.fulfill(5)
    v = p1.get()
    assert p1.is_fulfilled
    assert 5 == v
Ejemplo n.º 34
0
def test_wait_if():
    p1 = Promise()
    p1.fulfill(5)
    p1.wait()
    assert p1.is_fulfilled
Ejemplo n.º 35
0
def test_fake_promise():
    p = Promise()
    p.fulfill(FakeThenPromise())
    assert p.is_rejected
    assert_exception(p.reason, Exception, "FakeThenPromise raises in 'then'")