def test_should_not_fire_event_on_tick_if_its_not_yet_time(self): self.listen(EventName.start) mock_time = Mock(side_effect=[145.6, 146.4]) timeshift = TimeShift(self.exchange,mock_time) self.exchange.publish(Event(EventName.after,seconds = 3,next_event=Event(EventName.start))) self.exchange.publish(Event(EventName.tick)) self.assertEqual(self.event_count(EventName.start),0,"start should not have been called")
def setUp(self): self.logger = Mock() self.logger.error = Mock(side_effect=print_msg) self.servo = Mock() self.exchange = Exchange(self.logger) self.timer = StubTimer() TimeShift(self.exchange, self.timer.time)
def test_should_fire_event_on_tick_if_its_past_its_time(self): self.listen(EventName.start) mock_time = Mock(side_effect=[145.6, 155.4]) timeshift = TimeShift(self.exchange,mock_time) self.exchange.publish(Event(EventName.after,seconds = 5,next_event=Event(EventName.start))) self.exchange.publish(Event(EventName.tick)) self.assertEqual(self.event_count(EventName.start),1,"start should have been called") self.assertEqual(self.last_event.name,EventName.start)
def test_should_only_fire_after_events_once(self): self.listen(EventName.start) mock_time = Mock(side_effect=[145.6, 155.4, 166.2]) timeshift = TimeShift(self.exchange,mock_time) self.exchange.publish(Event(EventName.after,seconds = 5,next_event=Event(EventName.start))) self.exchange.publish(Event(EventName.tick)) self.exchange.publish(Event(EventName.tick)) event_count = self.event_count(EventName.start) self.assertEqual(event_count,1,"start should have been called once, but was called {0} times".format(event_count))
def test_should_fire_every_events_repeatedly(self): self.listen(EventName.start) # Note that time() is called on register, then on tick, awkwardness only during test mock_time = Mock(side_effect=[145.6, 155.4, 155.7, 166.2, 166.3, 188.3, 188.4]) timeshift = TimeShift(self.exchange,mock_time) self.exchange.publish(Event(EventName.every,seconds = 5,next_event=Event(EventName.start))) self.exchange.publish(Event(EventName.tick)) self.exchange.publish(Event(EventName.tick)) self.exchange.publish(Event(EventName.tick)) event_count = self.event_count(EventName.start) self.assertEqual(event_count,3,"turn should have been called three times, but was called {0} times".format(event_count))
def __init__(self, gps=False, servo_port=SERVO_PORT): # devices self._gps = gps self.windsensor = WindSensor(I2C(WINDSENSOR_I2C_ADDRESS)) self.compass = Compass(I2C(COMPASS_I2C_ADDRESS), I2C(ACCELEROMETER_I2C_ADDRESS)) self.red_led = GpioWriter(17, os) self.green_led = GpioWriter(18, os) # Navigation self.globe = Globe() self.timer = Timer() self.application_logger = self._rotating_logger(APPLICATION_NAME) self.position_logger = self._rotating_logger("position") self.exchange = Exchange(self.application_logger) self.timeshift = TimeShift(self.exchange, self.timer.time) self.event_source = EventSource(self.exchange, self.timer, self.application_logger, CONFIG['event source']) self.sensors = Sensors(self.gps, self.windsensor, self.compass, self.timer.time, self.exchange, self.position_logger, CONFIG['sensors']) self.gps_console_writer = GpsConsoleWriter(self.gps) self.rudder_servo = Servo(serial.Serial(servo_port), RUDDER_SERVO_CHANNEL, RUDDER_MIN_PULSE, RUDDER_MIN_ANGLE, RUDDER_MAX_PULSE, RUDDER_MAX_ANGLE) self.steerer = Steerer(self.rudder_servo, self.application_logger, CONFIG['steerer']) self.helm = Helm(self.exchange, self.sensors, self.steerer, self.application_logger, CONFIG['helm']) self.course_steerer = CourseSteerer(self.sensors, self.helm, self.timer, CONFIG['course steerer']) self.navigator = Navigator(self.sensors, self.globe, self.exchange, self.application_logger, CONFIG['navigator']) self.self_test = SelfTest(self.red_led, self.green_led, self.timer, self.rudder_servo, RUDDER_MIN_ANGLE, RUDDER_MAX_ANGLE) # Tracking self.tracking_logger = self._rotating_logger("track") self.tracking_sensors = Sensors(self.gps, self.windsensor, self.compass, self.timer.time, self.exchange, self.tracking_logger, CONFIG['sensors']) self.tracker = Tracker(self.tracking_logger, self.tracking_sensors, self.timer)
def __init__(self): self.globe = Globe() self.console_logger = self._console_logger() self.exchange = Exchange(self.console_logger) self.gps = SimulatedGPS(CHORLTON.position,0,0.1) self.vehicle = SimulatedVehicle(self.gps, self.globe,self.console_logger,single_step=False) self.timeshift = TimeShift(self.exchange,self.vehicle.timer.time) self.event_source = EventSource(self.exchange,self.vehicle.timer,self.console_logger,CONFIG['event source']) self.sensors = Sensors(self.vehicle.gps, self.vehicle.windsensor,self.vehicle.compass,self.vehicle.timer.time,self.exchange,self.console_logger,CONFIG['sensors']) self.steerer = Steerer(self.vehicle.rudder,self.console_logger,CONFIG['steerer']) self.helm = Helm(self.exchange, self.sensors, self.steerer, self.console_logger, CONFIG['helm']) self.course_steerer = CourseSteerer(self.sensors,self.helm,self.vehicle.timer, CONFIG['course steerer']) self.navigator_simulator = Navigator(self.sensors,self.globe,self.exchange,self.console_logger,CONFIG['navigator']) self.tracking_timer = Timer() self.tracker_simulator = Tracker(self.console_logger,self.sensors,self.tracking_timer)
def test_should_be_able_to_keep_and_fire_multiple_events(self): self.listen(EventName.start) self.listen(EventName.navigate) # Note that time() is called on register, then on tick, awkwardness only during test mock_time = Mock(side_effect=[145.6, 145.6, 155.4, 166.2]) timeshift = TimeShift(self.exchange,mock_time) self.exchange.publish(Event(EventName.after,seconds = 5,next_event=Event(EventName.start))) self.exchange.publish(Event(EventName.after,seconds = 10,next_event=Event(EventName.navigate))) self.exchange.publish(Event(EventName.tick)) self.assertEqual(self.event_count(EventName.start),1) self.assertEqual(self.event_count(EventName.navigate),0) self.exchange.publish(Event(EventName.tick)) self.assertEqual(self.event_count(EventName.navigate),1)
def setUp(self): self.mock_logger = Mock() self.mock_logger.error = Mock(side_effect=print_msg) self.exchange = Exchange(self.mock_logger) self.timer = StubTimer() self.timeshift = TimeShift(self.exchange, self.timer.time) self.event_source = EventSource(self.exchange, self.timer, self.mock_logger, {'tick interval': 0.2}) gps = FakeMovingGPS([ Position(10, 10), Position(11, 11), Position(12, 12), Position(13, 13) ]) self.navigator = Navigator(gps, Globe(), self.exchange, self.mock_logger, { 'min time to steer': 5, 'max time to steer': 20 })