def create_robot_mock(): robot_mock = mockobj() robot_mock.resources = { 'led_ring': Resource(), 'drivetrain': Resource(), 'sound': Resource() } robot_mock.robot = mockobj() robot_mock.robot.time = lambda: 0 robot_mock.robot.motors = [] robot_mock.robot.sensors = [] robot_mock.config = mockobj() robot_mock.config.motors = mockobj() robot_mock.config.motors.__iter__ = lambda: [] robot_mock.config.motors.names = {} robot_mock.config.sensors = mockobj() robot_mock.config.sensors.__iter__ = lambda: [] robot_mock.config.sensors.names = {} robot_mock.robot.drivetrain = mockobj() robot_mock.robot.drivetrain.turn = lambda *args, **kwargs: None robot_mock.robot.drivetrain.drive = lambda *args, **kwargs: None robot_mock.robot.sound = MockSound() robot_mock.robot.led = mockobj() robot_mock.robot.led.count = 0 robot_mock.robot.imu = mockobj() return robot_mock
def test_resource_can_be_retaken_by_same_priority(self): r = Resource() handle = r.request(priority_high) handle2 = r.request(priority_high) self.assertEqual(handle, handle2)
def create_robot_mock(): robot_mock = mockobj() robot_mock.resources = { 'led_ring': Resource(), 'drivetrain': Resource(), 'sound': Resource() } robot_mock.robot = mockobj() robot_mock.robot.start_time = 0 robot_mock.robot.motors = [] robot_mock.robot.sensors = [] robot_mock.config = mockobj() robot_mock.config.motors = mockobj() robot_mock.config.motors.__iter__ = lambda: [] robot_mock.config.motors.names = {} robot_mock.config.sensors = mockobj() robot_mock.config.sensors.__iter__ = lambda: [] robot_mock.config.sensors.names = {} robot_mock.robot.drivetrain = mockobj() robot_mock.robot.sound = mockobj() robot_mock.robot.led_ring = mockobj() robot_mock.robot.led_ring.count = 0 robot_mock.robot.imu = mockobj() return robot_mock
def test_lower_priority_can_take_resource_after_higher_priority_releases(self): r = Resource() handle = r.request(priority_high) handle.release() handle2 = r.request(priority_low) self.assertTrue(handle) self.assertFalse(handle.is_interrupted) self.assertTrue(handle2)
def test_higher_priority_handle_can_not_be_taken_taken_away(self): r = Resource() mock = Mock() handle = r.request(priority_high, mock) handle2 = r.request(priority_low) self.assertIsNotNone(handle) self.assertEqual(False, handle.is_interrupted) self.assertIsNone(handle2) self.assertEqual(0, mock.call_count)
def test_lower_priority_handle_can_be_taken_taken_away(self): r = Resource() mock = Mock() handle = r.request(priority_low, mock) handle2 = r.request(priority_high) self.assertTrue(handle) self.assertTrue(handle.is_interrupted) self.assertTrue(handle2) self.assertEqual(1, mock.call_count)
def test_run_should_only_run_on_active_handle(self): r = Resource() mock = Mock() handle = r.request(priority_low) handle2 = r.request(priority_high) handle2.run_uninterruptable(mock) handle.run_uninterruptable(lambda: self.fail('This should not run')) self.assertEqual(1, mock.call_count)
def test_resource_can_be_retaken_by_same_priority(self): r = Resource() handle = r.request(priority_high) handle2 = r.request(priority_high) self.assertTrue(handle) self.assertTrue(handle2) self.assertTrue(handle.is_interrupted) self.assertFalse(handle2.is_interrupted)
def __init__(self, interface: RevvyControl, revvy, sound_paths, sw_version, default_config=None): print("RobotManager: __init__()") self.needs_interrupting = True self._configuring = False self._robot = Robot(interface, sound_paths, sw_version) self._interface = interface self._ble = revvy self._default_configuration = default_config or RobotConfig() self._status_update_thread = periodic(self._update, 0.02, "RobotStatusUpdaterThread") self._background_fn_lock = Lock() self._background_fns = [] rc = RemoteController() rcs = RemoteControllerScheduler(rc) rcs.on_controller_detected(self._on_controller_detected) rcs.on_controller_lost(self._on_controller_lost) self._remote_controller = rc self._remote_controller_scheduler = rcs self._remote_controller_thread = create_remote_controller_thread(rcs) self._resources = { 'led_ring': Resource(), 'drivetrain': Resource(), 'sound': Resource(), **{ 'motor_{}'.format(port.id): Resource() for port in self._robot.motors }, **{ 'sensor_{}'.format(port.id): Resource() for port in self._robot.sensors } } revvy['live_message_service'].register_message_handler( self._remote_controller_scheduler.data_ready) revvy.on_connection_changed(self._on_connection_changed) self._scripts = ScriptManager(self) self._config = self._default_configuration self._status_code = RevvyStatusCode.OK self.exited = False
def __init__(self, robot: Robot, sw_version, revvy_ble): self._log = get_logger('RobotManager') self._log('init') self.needs_interrupting = True self._configuring = False self._robot = robot self._ble = revvy_ble self._sw_version = sw_version self._status_update_thread = periodic(self._update, 0.005, "RobotStatusUpdaterThread") self._background_fns = [] rc = RemoteController() rcs = RemoteControllerScheduler(rc) rcs.on_controller_detected(self._on_controller_detected) rcs.on_controller_lost(self._on_controller_lost) self._remote_controller = rc self._remote_controller_thread = create_remote_controller_thread(rcs) self._resources = { 'led_ring': Resource('RingLed'), 'drivetrain': Resource('DriveTrain'), 'sound': Resource('Sound'), **{ f'motor_{port.id}': Resource(f'Motor {port.id}') for port in self._robot.motors }, **{ f'sensor_{port.id}': Resource(f'Sensor {port.id}') for port in self._robot.sensors } } revvy_ble['live_message_service'].register_message_handler( rcs.data_ready) revvy_ble.on_connection_changed(self._on_connection_changed) self._scripts = ScriptManager(self) self._config = empty_robot_config self._status_code = RevvyStatusCode.OK self.exited = Event() self.start_remote_controller = self._remote_controller_thread.start
def test_resource_handle_needed_to_release(self): r = Resource() handle = r.request(priority_low) handle2 = r.request(priority_high) r.release(handle) handle3 = r.request(priority_low) self.assertIsNone(handle3) self.assertEqual(False, handle2.is_interrupted)
def test_ring_leds_are_indexed_from_one(self): led_mock = Mock() led_mock.display_user_frame = Mock() led_mock.count = 6 led_resource = ResourceWrapper(Resource(), 0) script = Mock() script.is_stop_requested = False rw = RingLedWrapper(script, led_mock, led_resource) self.assertRaises(IndexError, lambda: rw.set(0, '#112233')) rw.set(1, '#112233') rw.set(2, '#112233') rw.set(3, '#112233') rw.set(4, '#112233') rw.set(5, '#112233') rw.set(6, '#112233') self.assertRaises(IndexError, lambda: rw.set(7, '#112233'))
def test_ring_leds_are_indexed_from_one(self): led_mock = Mock() led_mock.display_user_frame = Mock() led_mock.count = 6 led_resource = ResourceWrapper(Resource(), 0) script = Mock() script.is_stop_requested = False rw = RingLedWrapper(script, led_mock, led_resource) self.assertRaises(IndexError, lambda: rw.set(leds=[0], color='#112233')) rw.set(leds=[1], color='#112233') rw.set(leds=[2], color='#112233') rw.set(leds=[3], color='#112233') rw.set(leds=[4], color='#112233') rw.set(leds=[5], color='#112233') rw.set(leds=[6], color='#112233') self.assertRaises(IndexError, lambda: rw.set(leds=[7], color='#112233'))
def test_ring_led_set_remembers_previous_state(self): led_mock = Mock() led_mock.display_user_frame = Mock() led_mock.count = 6 led_resource = ResourceWrapper(Resource(), 0) script = Mock() script.is_stop_requested = False rw = RingLedWrapper(script, led_mock, led_resource) rw.set(1, '#112233') self.assertEqual([hex2rgb("#112233"), 0, 0, 0, 0, 0], led_mock.display_user_frame.call_args[0][0]) self.assertEqual(1, led_mock.display_user_frame.call_count) rw.set([3, 4], '#223344') self.assertEqual( [hex2rgb("#112233"), 0, hex2rgb("#223344"), hex2rgb("#223344"), 0, 0], led_mock.display_user_frame.call_args[0][0] ) self.assertEqual(2, led_mock.display_user_frame.call_count)
def test_empty_resource_can_be_taken(self): r = Resource() handle = r.request() self.assertIsNotNone(handle)