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()
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.assertEquals(1, self.actor.last_task.finish_tick) def test_idling_when_nothing_to_do(self): self.run_clock() self.assertEquals(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.assertEquals(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.assertEquals(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.assertEquals(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.assertEquals('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.assertEquals(5, len(self.actor._task_history[0].sub_tasks)) self.assertEquals(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.assertEquals('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.assertEquals('task_b()[0->2]', str(self.actor.last_task))
name='trader', cash=1000, inventory={lemons: 100}, ) trader = TaskQueueActor('trader', clock) trader_workflow = RandomTraderWorkflow( trading_account, random, sell_ranges={lemons: TradeRange(1, 3, -3, 3, random)}, buy_ranges={lemons: TradeRange(1, 3, -1, 5, random)}) trader.allocate_task(trader_workflow.trade, args=[continuous_order_driven_market]) continuous_order_driven_market.record_limit_sell_order( LimitSellOrder(trading_account, lemons, 1, 100)) continuous_order_driven_market.record_limit_buy_order( LimitBuyOrder(trading_account, lemons, 1, 100)) order_book_clearing_workflow.clear() clock.start() trader.start() market_clearer.start() trader.wait_for_shutdown() market_clearer.wait_for_shutdown() print continuous_order_driven_market.average_trade_price