Exemple #1
0
 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()
Exemple #2
0
    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)
Exemple #3
0
    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)
Exemple #4
0
    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)
Exemple #5
0
    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)
Exemple #6
0
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)
        ]
Exemple #7
0
 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()
Exemple #8
0
    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)
Exemple #9
0
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)]
Exemple #10
0
    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)
Exemple #11
0
    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)
Exemple #12
0
    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)
Exemple #13
0
    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)
Exemple #14
0
    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)
Exemple #15
0
 def test_scheduling_an_object_is_forbidden(self):
     schedule = Scheduler()
     action = "This is not a callable!"
     with self.assertRaises(ValueError):
         schedule.at(10, action)
Exemple #16
0
    def test_scheduling_action_in_the_past_is_forbidden(self):
        schedule = Scheduler(20)
        action = DummyAction(schedule)

        with self.assertRaises(ValueError):
            schedule.at(10, action)
Exemple #17
0
 def test_scheduling_an_object_is_forbidden(self):
     schedule = Scheduler()
     action = "This is not a callable!"
     with self.assertRaises(ValueError):
         schedule.at(10, action)
Exemple #18
0
    def test_scheduling_at_a_non_integer_time(self):
        schedule = Scheduler()
        action = DummyAction(schedule)

        with self.assertRaises(ValueError):
            schedule.at("now", action)
Exemple #19
0
    def test_scheduling_action_in_the_past_is_forbidden(self):
        schedule = Scheduler(20)
        action = DummyAction(schedule)

        with self.assertRaises(ValueError):
            schedule.at(10, action)
Exemple #20
0
    def test_scheduling_at_a_non_integer_time(self):
        schedule = Scheduler()
        action = DummyAction(schedule)

        with self.assertRaises(ValueError):
            schedule.at("now", action)