def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" xknx = XKNX() device = Device(xknx, "TestDevice") after_update_callback1 = Mock() after_update_callback2 = Mock() async def async_after_update_callback1(device): """Async callback No. 1.""" after_update_callback1(device) async def async_after_update_callback2(device): """Async callback No. 2.""" after_update_callback2(device) device.register_device_updated_cb(async_after_update_callback1) device.register_device_updated_cb(async_after_update_callback2) # Triggering first time. Both have to be called self.loop.run_until_complete(device.after_update()) after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Triggering 2nd time. Both have to be called self.loop.run_until_complete(device.after_update()) after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering first callback device.unregister_device_updated_cb(async_after_update_callback1) self.loop.run_until_complete(device.after_update()) after_update_callback1.assert_not_called() after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering second callback device.unregister_device_updated_cb(async_after_update_callback2) self.loop.run_until_complete(device.after_update()) after_update_callback1.assert_not_called() after_update_callback2.assert_not_called()
def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') after_update_callback1 = Mock() after_update_callback2 = Mock() async def async_after_update_callback1(device): """Async callback No. 1.""" after_update_callback1(device) async def async_after_update_callback2(device): """Async callback No. 2.""" after_update_callback2(device) device.register_device_updated_cb(async_after_update_callback1) device.register_device_updated_cb(async_after_update_callback2) # Triggering first time. Both have to be called self.loop.run_until_complete(asyncio.Task(device.after_update())) after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Triggering 2nd time. Both have to be called self.loop.run_until_complete(asyncio.Task(device.after_update())) after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering first callback device.unregister_device_updated_cb(async_after_update_callback1) self.loop.run_until_complete(asyncio.Task(device.after_update())) after_update_callback1.assert_not_called() after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering second callback device.unregister_device_updated_cb(async_after_update_callback2) self.loop.run_until_complete(asyncio.Task(device.after_update())) after_update_callback1.assert_not_called() after_update_callback2.assert_not_called()
async def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" xknx = XKNX() device = Device(xknx, "TestDevice") after_update_callback1 = AsyncMock() after_update_callback2 = AsyncMock() device.register_device_updated_cb(after_update_callback1) device.register_device_updated_cb(after_update_callback2) # Triggering first time. Both have to be called await device.after_update() after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Triggering 2nd time. Both have to be called await device.after_update() after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering first callback device.unregister_device_updated_cb(after_update_callback1) await device.after_update() after_update_callback1.assert_not_called() after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering second callback device.unregister_device_updated_cb(after_update_callback2) await device.after_update() after_update_callback1.assert_not_called() after_update_callback2.assert_not_called()
async def test_bad_callback(self, logging_exception_mock): """Test handling callback raising an exception""" xknx = XKNX() device = Device(xknx, "TestDevice") good_callback_1 = AsyncMock() bad_callback = AsyncMock(side_effect=Exception("Boom")) good_callback_2 = AsyncMock() device.register_device_updated_cb(good_callback_1) device.register_device_updated_cb(bad_callback) device.register_device_updated_cb(good_callback_2) await device.after_update() good_callback_1.assert_called_with(device) bad_callback.assert_called_with(device) good_callback_2.assert_called_with(device) logging_exception_mock.assert_called_once_with( "Unexpected error while processing device_updated_cb for %s", device, )