def test_get_result_timeout(self): """Try to get the result with a timeout while the Promise is pending. """ def executor(on_fulfilled, on_rejected): pass p = Promise(executor) with pytest.raises(TimeoutError): p.result(0)
def test_async_failing_call(self): """Try to get the result of a failing Promise while it's pending.""" class Err(Exception): pass def executor(on_fulfilled, on_rejected): Timer(0.001, on_rejected, args=[Err()]).start() p = Promise(executor) with pytest.raises(Err): p.result()
def test_executor_raising_error(self): """Make a Promise with an executor raising an error and get result.""" class Err(Exception): pass def executor(on_fulfilled, on_rejected): raise Err() p = Promise(executor) with pytest.raises(Err): p.result()
def test_synchronous_call_failing(self): """Make a Promise rejected by a synchronous executor and get result.""" class Err(Exception): pass def executor(on_fulfilled, on_rejected): on_rejected(Err()) p = Promise(executor) with pytest.raises(Err): p.result()
def test_asynchronous_call(self): """Make a Promise fulfilled after the call to result().""" def executor(on_fulfilled, on_rejected): Timer(0.001, on_fulfilled, args=['OK']).start() p = Promise(executor) assert p.result() == 'OK'
def test_synchronous_call(self): """Make a Promise fulfilled by a synchronous function and get result""" def executor(on_fulfilled, on_rejected): on_fulfilled(3) p = Promise(executor) assert p.result() is 3
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
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