class TeamTestCase(unittest.TestCase): def setUp(self): self.clock = SynchronizingClock(max_ticks=1) self.cast = Cast() def test_add_member(self): self.cast.add_member(TaskQueueActor('alice', self.clock)) self.assertEqual(1, len(self.cast.members)) def test_multiple_actors(self): for name in range(0, 10): _actor = TaskQueueActor(name, self.clock) self.cast.add_member(_actor) idle_task = Idling() _actor.allocate_task(idle_task.idle, idle_task) self.cast.initiate_shutdown() self.cast.start() self.clock.start() self.cast.wait_for_shutdown() for actor in self.cast.members: self.assertEqual('idle()[0->1]', str(actor.last_task)) def test_multiple_starts(self): self.cast.add_member(TaskQueueActor('alice', self.clock)) self.cast.start() self.cast.start() self.cast.initiate_shutdown() self.clock.tick() self.cast.wait_for_shutdown()
def test_30_minutes_and_0_hour_tick_after_1800_seconds(self): seconds_clock = SynchronizingClock(max_ticks=3600) minutes_clock = SynchronizingClock() hours_clock = SynchronizingClock() minutes_stopwatch_actor = StopwatchActor( logical_name='seconds_to_minutes', clock=seconds_clock, parent_clock=minutes_clock, granularity=60) hours_stopwatch_actor = StopwatchActor(logical_name='minutes_to_hours', clock=minutes_clock, parent_clock=hours_clock, granularity=60) minutes_stopwatch_actor.start() hours_stopwatch_actor.start() for second in range(0, 1800): seconds_clock.tick() seconds_clock.shutdown() # Ensure final ticks are released to parent clock before testing state. minutes_stopwatch_actor.initiate_shutdown() hours_stopwatch_actor.initiate_shutdown() minutes_stopwatch_actor.wait_for_shutdown() hours_stopwatch_actor.wait_for_shutdown() self.assertEqual(1800, seconds_clock.current_tick) self.assertEqual(30, minutes_clock.current_tick) self.assertEqual(0, hours_clock.current_tick)
def test_one_minute_tick_after_60_seconds(self): seconds_clock = SynchronizingClock(max_ticks=60) minutes_clock = SynchronizingClock() seconds_clock.add_tick_listener( InterClockSynchronization(minutes_clock, granularity=60)) while minutes_clock.current_tick < 1: seconds_clock.tick() self.assertEqual(60, seconds_clock.current_tick) self.assertEqual(1, minutes_clock.current_tick)
class ClockTestCase(unittest.TestCase): def setUp(self): self.clock = SynchronizingClock(max_ticks=2) def test_synchronization(self): self.tick_listener = Mock() self.tick_listener.waiting_for_tick = Mock(spec=_Event) self.clock.add_tick_listener(self.tick_listener) self.clock.tick() self.tick_listener.waiting_for_tick.wait.assert_called_once_with() self.tick_listener.notify_new_tick.assert_called_once_with()
def test_for_handling_imprecision(self): seconds_clock = SynchronizingClock(max_ticks=3600) minutes_clock = SynchronizingClock() hours_clock = SynchronizingClock() seconds_clock.add_tick_listener( InterClockSynchronization(minutes_clock, granularity=60)) minutes_clock.add_tick_listener( InterClockSynchronization( hours_clock, granularity=60, precision=lambda granularity: granularity - 5)) while hours_clock.current_tick < 1: seconds_clock.tick() self.assertEqual(3300, seconds_clock.current_tick) self.assertEqual(55, minutes_clock.current_tick) self.assertEqual(1, hours_clock.current_tick)
def test_one_minute_tick_after_60_seconds(self): seconds_clock = SynchronizingClock(max_ticks=60) minutes_clock = SynchronizingClock() stopwatch_actor = StopwatchActor(logical_name='seconds_to_minutes', clock=seconds_clock, parent_clock=minutes_clock, granularity=60) stopwatch_actor.start() for second in range(0, 60): seconds_clock.tick() seconds_clock.tick() # Ensure final tick is released to parent clock before testing state. stopwatch_actor.shutdown() self.assertEqual(60, seconds_clock.current_tick) self.assertEqual(1, minutes_clock.current_tick)
def test_non_blocking_for_actors(self): seconds_clock = SynchronizingClock(max_ticks=3600) minutes_clock = SynchronizingClock() hours_clock = SynchronizingClock() seconds_clock.add_tick_listener(InterClockSynchronization(minutes_clock, granularity=60)) minutes_clock.add_tick_listener(InterClockSynchronization(hours_clock, granularity=60)) actor = TaskQueueActor(0, minutes_clock) example_workflow = ExampleWorkflow() actor.allocate_task(example_workflow.task_a, example_workflow) actor.initiate_shutdown() actor.start() while hours_clock.current_tick < 1: seconds_clock.tick() self.assertEqual(actor.last_task.finish_tick, 2) self.assertEqual(3600, seconds_clock.current_tick) self.assertEqual(60, minutes_clock.current_tick) self.assertEqual(1, hours_clock.current_tick)
class ClockTestCase(unittest.TestCase): def test_synchronization(self): self.clock = SynchronizingClock(max_ticks=2) self.tick_listener = Mock() self.clock.add_tick_listener(self.tick_listener) self.clock.tick() self.tick_listener.wait_for_tick.assert_called_once_with() self.tick_listener.notify_new_tick.assert_called_once_with() def test_ticks_until_stopped(self): self.clock = SynchronizingClock() self.clock.tick() self.clock.tick() self.clock.issue_ticks = False self.clock.tick() self.assertEqual(self.clock.current_tick, 2)
class ActorTestCase(TestCase): def setUp(self): self.clock = SynchronizingClock(max_ticks=4) self.actor = TaskQueueActor(0, self.clock) self.idling = Idling() self.example_workflow = ExampleWorkflow(self.idling) def run_clock(self): self.actor.start() self.clock.start() self.clock.wait_for_last_tick() def test_explicit_idle(self): self.actor.allocate_task(self.idling.idle, self.idling) self.actor.initiate_shutdown() self.run_clock() self.assertEqual(1, self.actor.last_task.finish_tick) def test_idling_when_nothing_to_do(self): self.run_clock() self.assertEqual(0, len(self.actor.task_history)) def test_finish_tasks_before_shutdown(self): self.actor.allocate_task(self.idling.idle, self.idling) self.actor.allocate_task(self.idling.idle, self.idling) self.actor.allocate_task(self.idling.idle, self.idling) self.actor.initiate_shutdown() self.run_clock() self.assertEqual(3, self.actor.last_task.finish_tick) def test_idling_when_nothing_to_do_after_completed_task(self): self.actor.allocate_task(self.idling.idle, self.idling) self.run_clock() self.assertEqual(1, self.actor.last_task.finish_tick) def test_nested_task(self): self.actor.allocate_task(self.example_workflow.task_a, self.example_workflow) self.actor.initiate_shutdown() self.run_clock() self.assertEqual(self.actor.last_task.finish_tick, 3) def test_encounter_exception_shutdown_cleanly(self): self.actor.allocate_task(self.example_workflow.task_c, self.example_workflow) self.run_clock() self.assertEqual('task_c()[0->1]', str(self.actor.last_task)) def test_insufficient_time_shutdown_cleanly(self): """ Demonstrate that actors can shutdown cleanly if their allocated tasks proceed beyond the maximum clock time. """ self.actor.allocate_task(self.idling.idle_for, self.idling, [5]) self.run_clock() self.actor.shutdown() self.assertEqual(5, len(self.actor._task_history[0].sub_tasks)) self.assertEqual(None, self.actor._task_history[0].finish_tick) def test_stateless_task_allocation(self): @default_cost(1) def example_task(): pass self.actor.allocate_task(example_task) self.run_clock() self.assertEqual('example_task()[0->1]', str(self.actor.last_task)) def test_task_with_implicit_state_allocation(self): self.actor.allocate_task(self.example_workflow.task_b) self.run_clock() self.assertEqual('task_b()[0->2]', str(self.actor.last_task)) def test_start_twice(self): self.actor.start() self.actor.start() self.actor.initiate_shutdown() self.clock.tick() self.actor.wait_for_shutdown()