Esempio n. 1
0
    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)
Esempio n. 2
0
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))
Esempio n. 3
0
    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
Esempio n. 4
0
 def assign_task_to_new_actor(self, entry_method):
     eve = TaskQueueActor('eve', self.episode.clock)
     self.episode.cast.add_member(eve)
     eve.allocate_task(entry_method)
     eve.start()