def test_wait_for_exception(self): def callback(): raise TelegramError('Error') promise = Promise(callback, [], {}) promise.run() with pytest.raises(TelegramError, match='Error'): promise.result()
def test_done_cb_before_run(self): def callback(): return "done!" def done_callback(_): self.test_flag = True promise = Promise(callback, [], {}) promise.add_done_callback(done_callback) assert promise.result(0) != "done!" assert self.test_flag is False promise.run() assert promise.result() == "done!" assert self.test_flag is True
def test_done_cb_before_run_excp(self, caplog): def callback(): return "done!" def done_callback(_): raise Exception("Error!") promise = Promise(callback, [], {}) promise.add_done_callback(done_callback) assert promise.result(0) != "done!" caplog.clear() with caplog.at_level(logging.WARNING): promise.run() assert len(caplog.records) == 2 assert caplog.records[0].message == ( "`done_callback` of a Promise raised the following exception." " The exception won't be handled by error handlers.") assert caplog.records[1].message.startswith("Full traceback:") assert promise.result() == "done!"
def test_done_cb_after_run_excp(self): def callback(): return "done!" def done_callback(_): raise Exception("Error!") promise = Promise(callback, [], {}) promise.run() assert promise.result() == "done!" with pytest.raises(Exception) as err: promise.add_done_callback(done_callback) assert str(err) == "Error!"