diff --git a/tangostationcontrol/tangostationcontrol/test/common/test_lofar_logging.py b/tangostationcontrol/tangostationcontrol/test/common/test_lofar_logging.py index 4d59590c8fbe0b5a9d166c65b3abde022492e606..a6c53862b36604daabefb4de145d18997b752c1c 100644 --- a/tangostationcontrol/tangostationcontrol/test/common/test_lofar_logging.py +++ b/tangostationcontrol/tangostationcontrol/test/common/test_lofar_logging.py @@ -74,25 +74,36 @@ class TestLofarLogging(base.TestCase): logger = lofar_logging.configure_logger() + # alias to distinguish from other "self"s + test_object = self + + # Since we spawn a new process for the Device ("process=True" in the DeviceTestContext), + # and the mock doesn't work across processes, we do all of our mocking and checks in + # the device itself. If any Exception is raised, it will be marshalled to the device + # server by Tango and reraised here in the test as a DevFailed exception. + # create a Tango Device that logs something class MyDevice(Device): def init_device(self): - self.log_deeper_in_stack() + with mock.patch.object(device_server.DeviceImpl, '__info_stream') as m_info_stream: + self.log_deeper_in_stack() - def log_deeper_in_stack(self): - logger.info("test") + # check if we actually routed the log to self.info_stream + test_object.assertEqual(1, m_info_stream.call_count, msg="configure_logger did not send logs to active Tango device") - with mock.patch.object(device_server.DeviceImpl, '__info_stream') as m_info_stream: - with DeviceTestContext(MyDevice) as mydevice: - self.assertEqual(1, m_info_stream.call_count, msg="configure_logger did not send logs to active Tango device") + # Lookup our "test" logline among f.e. the debug messages output by Tango + test_record = [record for record in test_object.memory_handler.records if record.msg == "test log_deeper_in_stack"] - # Lookup our "test" logline among f.e. the debug messages output by Tango - test_record = [record for record in self.memory_handler.records if record.msg == "test"] + # Tango uses slightly different class representations of MyDevice, so + # we can't compare them direclty. Just verify we're talking about the same thing. + test_object.assertEqual(str(self), str(test_record[0].tango_device), msg="configure_logging did not detect active Tango device") - # Tango uses slightly different class representations of MyDevice, so - # we can't compare them direclty. Just verify we're talking about the same thing. - self.assertEqual(str(mydevice), str(test_record[0].tango_device), msg="configure_logging did not detect active Tango device") + def log_deeper_in_stack(self): + logger.info("test log_deeper_in_stack") + # if init_device fails, an exception will be thrown + with DeviceTestContext(MyDevice, process=True) as mydevice: + pass def test_log_exceptions(self): """ Test whether log_exceptions actually logs and reraises exceptions. """