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.do_resolve(10) pending = Promise() pending.do_resolve(5) pf = p1.then(lambda r: pending) pending._wait() assert pending.is_fulfilled assert 5 == pending.get() pf._wait() assert pf.is_fulfilled assert 5 == pf.get() p2 = Promise() p2.do_resolve(10) bad = Promise() bad.do_reject(Exception("Error")) pr = p2.then(lambda r: bad) bad._wait() assert_exception(bad.reason, Exception, "Error") pr._wait() assert pr.is_rejected assert_exception(pr.reason, Exception, "Error")
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]) + p.then_all( [(lambda x: x * x, lambda r: 1)]) + p.then_all(handlers) p.do_resolve(4) assert [r.get() for r in results] == [4, 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.do_reject(Exception()) assert [r.get() for r in results] == [1, 1, 2]
def test_done(): counter = [0] r = Promise() def inc(_): counter[0] += 1 def dec(_): counter[0] -= 1 def end(_): r.do_resolve(None) p = Promise() p.done(inc, dec) p.done(inc, dec) p.done(end) p.do_resolve(4) Promise.wait(r) assert counter[0] == 2 r = Promise() counter = [0] p = Promise() p.done(inc, dec) p.done(inc, dec) p.done(None, end) p.do_reject(Exception()) Promise.wait(r) assert counter[0] == -2
def test_done(): counter = [0] e = Event() def inc(_): counter[0] += 1 e.set() def dec(_): counter[0] -= 1 e.set() p = Promise() p.done(inc, dec) p.done(inc, dec) p.do_resolve(4) e.wait() assert counter[0] == 2 counter = [0] p = Promise() e = Event() p.done(inc, dec) p.done(inc, dec) p.do_reject(Exception()) e.wait() assert counter[0] == -2
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]) + p.then_all([(lambda x: x * x, lambda r: 1)]) + p.then_all(handlers) ) p.do_resolve(4) assert [r.get() for r in results] == [4, 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.do_reject(Exception()) assert [r.get() for r in results] == [1, 1, 2]
def test_3_2_1(): """ Test that the arguments to 'then' are optional. """ p1 = Promise() p2 = p1.then() p3 = Promise() p4 = p3.then() p1.do_resolve(5) p3.do_reject(Exception("How dare you!"))
def test_3_2_6_5_rejected(): """ Handles the case where the arguments to then are values, not functions or promises. """ p1 = Promise() p1.do_reject(Exception("Error")) p2 = p1.then(None, 5) assert_exception(p1.reason, Exception, "Error") p2._wait() assert p2.is_rejected assert_exception(p2.reason, Exception, "Error")
def test_exceptions(): def throws(v): assert False p1 = Promise() p1.then(throws) p1.do_resolve(5) p2 = Promise() p2.catch(throws) p2.do_reject(Exception()) with raises(Exception) as excinfo: p2.get()
def test_done_all(): counter = [0] def inc(_): counter[0] += 1 def dec(_): counter[0] -= 1 p = Promise() r = Promise() p.done_all() p.done_all([(inc, dec)]) p.done_all([ (inc, dec), (inc, dec), { 'success': inc, 'failure': dec }, lambda _: r.do_resolve(None) ]) p.do_resolve(4) Promise.wait(r) assert counter[0] == 4 p = Promise() r = Promise() p.done_all() p.done_all([inc]) p.done_all([(inc, dec)]) p.done_all([ (inc, dec), { 'success': inc, 'failure': dec }, (None, lambda _: r.do_resolve(None)) ]) p.do_reject(Exception("Uh oh!")) Promise.wait(r) assert counter[0] == 1
def test_done_all(): counter = [0] e = Event() 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 }, lambda _: e.set() ]) p.do_resolve(4) e.wait() assert counter[0] == 4 e = Event() p = Promise() p.done_all() p.done_all([inc]) p.done_all([(inc, dec)]) p.done_all([ (inc, dec), { 'success': inc, 'failure': dec }, (None, lambda _: e.set()) ]) p.do_reject(Exception("Uh oh!")) e.wait() assert counter[0] == 1
def test_3_2_6_3_when_rejected(): """ Testing return of pending promises to make sure they are properly chained. This covers the case where the root promise is rejected after the chaining is defined. """ p1 = Promise() pending = Promise() pr = p1.then(None, lambda r: pending) assert pending.is_pending assert pr.is_pending p1.do_reject(Exception("Error")) pending.do_resolve(10) pending._wait() assert pending.is_fulfilled assert 10 == pending.get() assert 10 == pr.get() p2 = Promise() bad = Promise() pr = p2.then(None, lambda r: bad) assert bad.is_pending assert pr.is_pending p2.do_reject(Exception("Error")) bad.do_reject(Exception("Assertion")) 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_done_all(): counter = [0] def inc(_): counter[0] += 1 def dec(_): counter[0] -= 1 p = Promise() r = Promise() p.done_all() p.done_all([(inc, dec)]) p.done_all( [ (inc, dec), (inc, dec), {"success": inc, "failure": dec}, lambda _: r.do_resolve(None), ] ) p.do_resolve(4) Promise.wait(r) assert counter[0] == 4 p = Promise() r = Promise() p.done_all() p.done_all([inc]) p.done_all([(inc, dec)]) p.done_all( [ (inc, dec), {"success": inc, "failure": dec}, (None, lambda _: r.do_resolve(None)), ] ) p.do_reject(Exception("Uh oh!")) Promise.wait(r) assert counter[0] == 1