def __init__(self, storage): self._storage = storage self._scheduler = Scheduler() self.environment = Environment() self.environment.define(Symbols.SIMULATION, self) self._next_request_id = 1 self.factory = Factory()
def test_scheduling_an_action_after_a_delay(self): schedule = Scheduler() action = DummyAction(schedule) schedule.after(5, action) schedule.simulate_until(20) self.assertEqual(5, schedule.time_now) self.verify_calls([5], action)
def test_scheduling_an_action_at_a_given_time(self): schedule = Scheduler() action = DummyAction(schedule) schedule.at(10, action) schedule.simulate_until(20) self.assertEqual(10, schedule.time_now) self.verify_calls([10], action)
def test_scheduling_an_action_with_a_given_period(self): schedule = Scheduler() action = DummyAction(schedule) schedule.every(5, action) schedule.simulate_until(20) self.assertLessEqual(20, schedule.time_now) self.verify_calls([5, 10, 15, 20], action)
def test_scheduling_twice_an_action_at_a_given_time(self): schedule = Scheduler() action = DummyAction(schedule) schedule.at(5, action) schedule.at(5, action) schedule.simulate_until(20) self.assertLessEqual(5, schedule.time_now) self.verify_calls([5, 5], action)
class Simulation: """ Represent the general simulation, including the current schedule and the associated trace """ # TODO: This should inherits from SimulatedEntity as well def __init__(self, storage): self._storage = storage self._scheduler = Scheduler() self.environment = Environment() self.environment.define(Symbols.SIMULATION, self) self._next_request_id = 1 self.factory = Factory() def run_until(self, end, display=None): self._scheduler.simulate_until(end, display) @property def log(self): return self._storage.log @property def schedule(self): return self._scheduler def evaluate(self, expression, continuation=lambda x: x): return Evaluation(self.environment, expression, self.factory, continuation).result def next_request_id(self): id = self._next_request_id self._next_request_id += 1 return id @property def services(self): return self._find_by_type(Service) @property def clients(self): return self._find_by_type(ClientStub) def _find_by_type(self, type): return [ each_value for each_value in list(self.environment.bindings.values()) if isinstance(each_value, type) ]
def test_simulation_orders_events(self): schedule = Scheduler() action = DummyAction(schedule) schedule.at(10, action) schedule.at(5, action) schedule.simulate_until(20) self.verify_calls([5, 10], action)
class Simulation: """ Represent the general simulation, including the current schedule and the associated trace """ # TODO: This should inherits from SimulatedEntity as well def __init__(self, storage): self._storage = storage self._scheduler = Scheduler() self.environment = Environment() self.environment.define(Symbols.SIMULATION, self) self._next_request_id = 1 self.factory = Factory() def run_until(self, end, display=None): self._scheduler.simulate_until(end, display) @property def log(self): return self._storage.log @property def schedule(self): return self._scheduler def evaluate(self, expression, continuation=lambda x: x): return Evaluation(self.environment, expression, self.factory, continuation).result def next_request_id(self): id = self._next_request_id self._next_request_id += 1 return id @property def services(self): return self._find_by_type(Service) @property def clients(self): return self._find_by_type(ClientStub) def _find_by_type(self, type): return [each_value for each_value in self.environment.bindings.values() if isinstance(each_value, type)]
def test_scheduling_an_object_is_forbidden(self): schedule = Scheduler() action = "This is not a callable!" with self.assertRaises(ValueError): schedule.at(10, action)
def test_scheduling_action_in_the_past_is_forbidden(self): schedule = Scheduler(20) action = DummyAction(schedule) with self.assertRaises(ValueError): schedule.at(10, action)
def test_scheduling_at_a_non_integer_time(self): schedule = Scheduler() action = DummyAction(schedule) with self.assertRaises(ValueError): schedule.at("now", action)