def test_slot_behaviour(self, recwarn, mro_slots): inst = Promise(self.test_call, [], {}) for attr in inst.__slots__: assert getattr(inst, attr, 'err') != 'err', f"got extra slot '{attr}'" assert not inst.__dict__, f"got missing slot(s): {inst.__dict__}" assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot" inst.custom, inst.args = 'should give warning', [] assert len(recwarn) == 1 and 'custom' in str(recwarn[0].message), recwarn.list
def test_run_with_exception(self): def callback(): raise TelegramError('Error') promise = Promise(callback, [], {}) promise.run() assert promise.done assert not self.test_flag assert isinstance(promise.exception, TelegramError)
def test_call(self): def callback(): self.test_flag = True promise = Promise(callback, [], {}) promise() assert promise.done assert self.test_flag
def wrapped(self: 'Bot', *args: object, **kwargs: object) -> object: # pylint: disable=W0212 queued = kwargs.pop( 'queued', self._is_messages_queued_default # type: ignore[attr-defined] ) isgroup = kwargs.pop('isgroup', False) if queued: prom = Promise(method, (self,) + args, kwargs) return self._msg_queue(prom, isgroup) # type: ignore[attr-defined] return method(self, *args, **kwargs)
def create_promise(self, post: Post) -> Promise: targets = [] targets.append(self.create_post(post, settings.CHAT_ID)) for chat_id, config in self.config.items(): data = self.create_post(post, int(chat_id), config) if data: self.logger.debug(f'┃ Post also goes to: {chat_id} with tags {config["subs"]}') targets.append(data) return Promise(self.send_posts_to_targets, (targets, post.id), {})
def _run_async( self, func: Callable[..., object], *args: object, update: object = None, error_handling: bool = True, **kwargs: object, ) -> Promise: # TODO: Remove error_handling parameter once we drop the @run_async decorator promise = Promise(func, args, kwargs, update=update, error_handling=error_handling) self.__async_queue.put(promise) return promise
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_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_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!"
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_not_run_on_excp(self): def callback(): raise TelegramError('Error') def done_callback(_): self.test_flag = True promise = Promise(callback, [], {}) promise.add_done_callback(done_callback) promise.run() assert isinstance(promise.exception, TelegramError) assert promise.done assert self.test_flag is False