async def test_notify_call_soon_threadsafe_with_exception(notifier: Notifier): notifier.logger = Mock() notifier._loop = Mock(call_soon_threadsafe=Mock(side_effect=RuntimeError)) notifier.add_observer('topic', lambda:...) notifier.notify('topic') notifier.logger.warning.assert_called_once()
async def test_notifier_add_observer(notifier: Notifier): def callback(): ... # test that add observer stores topics and callbacks as a set to prevent duplicates notifier.add_observer('topic', callback) notifier.add_observer('topic', callback) assert len(notifier.observers['topic']) == 1
async def test_notifier_remove_observer(notifier: Notifier): def callback1(): ... def callback2(): ... notifier.add_observer('topic', callback1) notifier.add_observer('topic', callback2) notifier.remove_observer('topic', callback1) assert notifier.observers['topic'] == {callback2: True}
async def test_notify(notifier: Notifier): # test that notify works as expected normal_callback = TestCallback() notifier.add_observer('topic', normal_callback.callback) notifier.notify('topic', 'arg', kwarg='value') # wait for the callback await normal_callback.event.wait() assert normal_callback.callback_has_been_called assert normal_callback.callback_has_been_called_with_args == ('arg', ) assert normal_callback.callback_has_been_called_with_kwargs == { 'kwarg': 'value' }
async def test_notify_with_exception(notifier: Notifier): # test that notify works as expected even if one of callbacks will raise an exception normal_callback = TestCallback() side_effect_callback = TestCallback(ValueError) notifier.add_observer('topic', side_effect_callback.callback) notifier.add_observer('topic', normal_callback.callback) notifier.add_observer('topic', side_effect_callback.callback) notifier.notify('topic') # wait await asyncio.sleep(1) assert normal_callback.callback_has_been_called assert side_effect_callback.callback_has_been_called
async def test_notifier(self): notifier = Notifier() notifier.add_observer(NTFY.TORRENT_FINISHED, self.callback_func) notifier.notify(NTFY.TORRENT_FINISHED) await self.test_future self.assertTrue(self.called_callback)