def test_open_after_init(self): """Test LogConnection() start logging after initialisation.""" prefix = os.path.join(TMP_PATH, 'unittest') logger = LogConnection(prefix, UnitTestMessageA, open_init=True) self.assertTrue(logger.is_alive()) self.assertTrue(logger.close())
def log_file(self, prefix, connection, broadcaster, data): """Method for testing logging ability.""" # Delete log if it already exists (it shouldn't). log = prefix + '.log' self.delete_if_exists(log) # Create broadcast and logging object. lc = LogConnection(prefix, connection, open_init=True) self.assertFalse(os.path.exists(log)) # Broadcast data. broadcaster.publish(data) time.sleep(DELAY) # Ensure data exists. lc.close() self.assertTrue(os.path.exists(log)) # Re-read split/log file (skip header). with open(log, 'r') as f: lines = f.readlines() lines = [line for line in lines if not line.startswith('#')] recorded_time, received_topic, payload = lines[0].split() self.assertEqual(received_topic, "''") self.assertEqual(msgpack.loads(payload.decode('hex')), data) self.delete_if_exists(log) broadcaster.close()
def test_init(self): """Test LogConnection() initialisation.""" # Path to log file. prefix = os.path.join(TMP_PATH, 'unittest') # Create logging object with connection/message. LogConnection(prefix, UnitTestMessageA.connection) LogConnection(prefix, UnitTestMessageA) # Ensure object can be initialised using all keyword arguments. max_entries = 10 max_time = 60 logger = LogConnection(prefix, UnitTestMessageA, time_origin=datetime.datetime.now(), max_entries=max_entries, max_time=max_time, open_init=False) self.assertEqual(logger.max_entries, max_entries) self.assertEqual(logger.max_time, max_time) self.assertFalse(logger.is_alive()) self.assertTrue(logger.open()) self.assertFalse(logger.open()) self.assertTrue(logger.close()) self.assertFalse(logger.is_alive()) self.assertFalse(logger.close())
def test_bad_init(self): """Test LogConnection() with bad inputs.""" # Ensure WriteFile() can pass through errors. with self.assertRaises(IOError): prefix = os.path.join( TMP_PATH, 'does', 'not', 'exist', ) LogConnection(prefix, UnitTestMessageA) # Ensure QueuedListener() can pass through errors. with self.assertRaises(IOError): prefix = os.path.join(TMP_PATH, 'unittest') LogConnection(prefix, Connection('bad_url'))