def test_construction_with_sync_espdrone_instance(self): # Fixture sed_mock = MagicMock(spec=SyncEspdrone) sed_mock.ed = self.ed_mock # Test sut = SyncLogger(sed_mock, self.log_config_mock) sut.connect()
def setUp(self): self.ed_mock = MagicMock(spec=Espdrone) self.ed_mock.disconnected = Caller() self.log_mock = MagicMock(spec=Log) self.ed_mock.log = self.log_mock self.log_config_mock = MagicMock(spec=LogConfig) self.log_config_mock.data_received_cb = Caller() self.sut = SyncLogger(self.ed_mock, self.log_config_mock)
def test_connect_disconnect_with_context_management(self): # Fixture # Test with (SyncLogger(self.ed_mock, self.log_config_mock)) as sut: # Assert self.assertTrue(sut.is_connected()) self.assertFalse(sut.is_connected())
def assert_add_logging_and_get_non_zero_value(self, sed): log_name = 'stabilizer.roll' expected = 0.0 lg_conf = LogConfig(name='SysTest', period_in_ms=10) lg_conf.add_variable(log_name, 'float') with SyncLogger(sed, lg_conf) as logger: for log_entry in logger: actual = log_entry[1][log_name] break self.assertNotAlmostEqual(expected, actual, places=4)
def verify_writing_memory_data(self, mems, sed): self.wrote_data = False sed.ed.param.set_value('memTst.resetW', '1') time.sleep(0.1) mems[0].write_data(5, 1000, self._data_written) while not self.wrote_data: time.sleep(1) log_conf = LogConfig(name='memtester', period_in_ms=100) log_conf.add_variable('memTst.errCntW', 'uint32_t') with SyncLogger(sed, log_conf) as logger: for log_entry in logger: errorCount = log_entry[1]['memTst.errCntW'] self.assertEqual(0, errorCount) break
def wait_for_position_estimator(sed): print('Waiting for estimator to find position...') log_config = LogConfig(name='Kalman Variance', period_in_ms=500) log_config.add_variable('kalman.varPX', 'float') log_config.add_variable('kalman.varPY', 'float') log_config.add_variable('kalman.varPZ', 'float') var_y_history = [1000] * 10 var_x_history = [1000] * 10 var_z_history = [1000] * 10 threshold = 0.001 with SyncLogger(sed, log_config) as logger: for log_entry in logger: data = log_entry[1] var_x_history.append(data['kalman.varPX']) var_x_history.pop(0) var_y_history.append(data['kalman.varPY']) var_y_history.pop(0) var_z_history.append(data['kalman.varPZ']) var_z_history.pop(0) min_x = min(var_x_history) max_x = max(var_x_history) min_y = min(var_y_history) max_y = max(var_y_history) min_z = min(var_z_history) max_z = max(var_z_history) # print("{} {} {}". # format(max_x - min_x, max_y - min_y, max_z - min_z)) if (max_x - min_x) < threshold and ( max_y - min_y) < threshold and ( max_z - min_z) < threshold: break
if args.uri: uri = args.uri else: available = edlib.crtp.scan_interfaces() print('Espdrones found:') if available: print(available[0]) uri = available[0][0] else: quit() lg_stab = LogConfig(name='Stabilizer', period_in_ms=10) lg_stab.add_variable('stabilizer.roll', 'float') lg_stab.add_variable('stabilizer.pitch', 'float') lg_stab.add_variable('stabilizer.yaw', 'float') ed = Espdrone(rw_cache='./cache') with SyncEspdrone(uri, ed=ed) as sed: with SyncLogger(sed, lg_stab) as logger: endTime = time.time() + 10 for log_entry in logger: timestamp = log_entry[0] data = log_entry[1] logconf_name = log_entry[2] print('[%d][%s]: %s' % (timestamp, logconf_name, data)) if time.time() > endTime: break
class SyncLoggerTest(unittest.TestCase): def setUp(self): self.ed_mock = MagicMock(spec=Espdrone) self.ed_mock.disconnected = Caller() self.log_mock = MagicMock(spec=Log) self.ed_mock.log = self.log_mock self.log_config_mock = MagicMock(spec=LogConfig) self.log_config_mock.data_received_cb = Caller() self.sut = SyncLogger(self.ed_mock, self.log_config_mock) def test_that_log_configuration_is_added_on_connect(self): # Fixture # Test self.sut.connect() # Assert self.log_mock.add_config.assert_called_once_with(self.log_config_mock) def test_that_logging_is_started_on_connect(self): # Fixture # Test self.sut.connect() # Assert self.log_config_mock.start.assert_called_once_with() def test_connection_status_after_connect(self): # Fixture self.sut.connect() # Test actual = self.sut.is_connected() # Assert self.assertTrue(actual) def test_that_callbacks_are_removed_on_disconnect(self): # Fixture # Test self.sut.connect() self.sut.disconnect() # Assert self.assertEqual(0, len(self.ed_mock.disconnected.callbacks)) self.assertEqual(0, len(self.log_config_mock.data_received_cb.callbacks)) def test_that_log_config_is_stopped_on_disconnect(self): # Fixture self.sut.connect() # Test self.sut.disconnect() # Assert self.log_config_mock.stop.assert_called_once_with() self.log_config_mock.delete.assert_called_once_with() def test_that_data_is_received(self): # Fixture self.sut.connect() expected = ('Some ts', 'Some data', 'Some logblock') AsyncCallbackCaller(cb=self.log_config_mock.data_received_cb, args=[expected[0], expected[1], expected[2]]).trigger() # Test actual = None for log_block in self.sut: actual = log_block break # Assert self.assertEqual(expected, actual) def test_connection_status_after_disconnect(self): # Fixture self.sut.connect() self.sut.disconnect() # Test actual = self.sut.is_connected() # Assert self.assertFalse(actual) def test_that_connect_to_connected_instance_raises_exception(self): # Fixture self.sut.connect() # Test # Assert with self.assertRaises(Exception): self.sut.connect() def test_connect_to_disconnected_instance(self): # Fixture self.sut.connect() self.sut.disconnect() # Test self.sut.connect() # Assert # Nothing here. Just not expecting an exception def test_disconnect_from_disconnected_instance(self): # Fixture # Test self.sut.disconnect() # Assert # Nothing here. Just not expecting an exception def test_connect_disconnect_with_context_management(self): # Fixture # Test with (SyncLogger(self.ed_mock, self.log_config_mock)) as sut: # Assert self.assertTrue(sut.is_connected()) self.assertFalse(sut.is_connected()) def test_that_iterator_is_implemented(self): # Fixture # Test actual = self.sut.__iter__() # Assert self.assertEqual(self.sut, actual) def test_construction_with_sync_espdrone_instance(self): # Fixture sed_mock = MagicMock(spec=SyncEspdrone) sed_mock.ed = self.ed_mock # Test sut = SyncLogger(sed_mock, self.log_config_mock) sut.connect() # Assert # Nothing here. Just not expecting an exception def test_getting_data_without_conection_raises_exception(self): # Fixture # Test with self.assertRaises(StopIteration): self.sut.__next__() # Assert def test_lost_connection_in_espdrone_disconnects(self): # Fixture self.sut.connect() # Test AsyncCallbackCaller(cb=self.ed_mock.disconnected, args=['Some uri']).call_and_wait() # Assert self.assertFalse(self.sut.is_connected()) def test_lost_connection_in_espdrone_raises_exception_in_iterator(self): # Fixture self.sut.connect() # Note this is not foolproof, the disconnected callback may be called # before we are waiting on data. It will raise the same exception # though and will pass AsyncCallbackCaller(cb=self.ed_mock.disconnected, delay=0.5, args=['Some uri']).trigger() # Test # Assert with self.assertRaises(StopIteration): self.sut.__next__()