def test_promise_all_if(): p1 = Promise() p2 = Promise() pd1 = Promise.all([p1, p2]) pd2 = Promise.all([p1]) pd3 = Promise.all([]) pd3._wait() assert p1.is_pending assert p2.is_pending assert pd1.is_pending assert pd2.is_pending assert pd3.is_fulfilled p1.do_resolve(5) p1._wait() pd2._wait() assert p1.is_fulfilled assert p2.is_pending assert pd1.is_pending assert pd2.is_fulfilled p2.do_resolve(10) p2._wait() pd1._wait() pd2._wait() assert p1.is_fulfilled assert p2.is_fulfilled assert pd1.is_fulfilled assert pd2.is_fulfilled assert 5 == p1.get() assert 10 == p2.get() assert 5 == pd1.get()[0] assert 5 == pd2.get()[0] assert 10 == pd1.get()[1] assert [] == pd3.get()
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 pd3._wait() assert pd3.is_fulfilled p1.do_resolve(5) p1._wait() pd2._wait() assert p1.is_fulfilled assert p2.is_pending assert pd1.is_pending assert pd2.is_fulfilled p2.do_resolve(10) p2._wait() pd1._wait() assert p1.is_fulfilled assert p2.is_fulfilled assert pd1.is_fulfilled assert pd2.is_fulfilled assert 5 == p1.get() assert 10 == p2.get() assert 5 == pd1.get()["a"] assert 5 == pd2.get()["a"] assert 10 == pd1.get()["b"] assert {} == pd3.get()
def test_3_2_6_3_if_rejected(): """ Testing return of pending promises to make sure they are properly chained. This covers the case where the root promise is rejected before the chaining is defined. """ p1 = Promise() p1.do_reject(Exception("Error")) pending = Promise() pending.do_resolve(10) pr = p1.then(None, lambda r: pending) pending._wait() assert pending.is_fulfilled assert 10 == pending.get() pr._wait() assert pr.is_fulfilled assert 10 == pr.get() p2 = Promise() p2.do_reject(Exception("Error")) bad = Promise() bad.do_reject(Exception("Assertion")) pr = p2.then(None, lambda r: bad) bad._wait() assert bad.is_rejected assert_exception(bad.reason, Exception, "Assertion") pr._wait() assert pr.is_rejected assert_exception(pr.reason, Exception, "Assertion")
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.do_resolve(5) p1._wait() assert p1.is_fulfilled assert p2.is_pending assert pd.is_pending p2.do_resolve(10) p2._wait() assert p1.is_fulfilled assert p2.is_fulfilled
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.do_resolve(5) p1._wait() assert p1.is_fulfilled assert p2.is_pending assert pl.is_pending p2.do_resolve(10) p2._wait() pl._wait() 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]
def test_3_2_6_3_when_fulfilled(): """ Testing return of pending promises to make sure they are properly chained. This covers the case where the root promise is fulfilled after the chaining is defined. """ p1 = Promise() pending = Promise() def p1_resolved(v): return pending pf = p1.then(p1_resolved) assert pending.is_pending assert pf.is_pending p1.do_resolve(10) pending.do_resolve(5) pending._wait() assert pending.is_fulfilled assert 5 == pending.get() pf._wait() assert pf.is_fulfilled assert 5 == pf.get() p2 = Promise() bad = Promise() pr = p2.then(lambda r: bad) assert bad.is_pending assert pr.is_pending p2.do_resolve(10) bad._reject_callback(Exception("Error")) bad._wait() assert bad.is_rejected assert_exception(bad.reason, Exception, "Error") pr._wait() assert pr.is_rejected assert_exception(pr.reason, Exception, "Error")
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.do_resolve(5) p1._wait() assert p1.is_fulfilled assert p2.is_pending assert pl.is_pending p2.do_resolve(10) p2._wait() pl._wait() 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]
def test_wait_if(): p1 = Promise() p1.do_resolve(5) p1._wait() assert p1.is_fulfilled