コード例 #1
0
    def test_mechanism_not_called_if_no_subject_behavior(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()
        global called
        called = 0

        class MySubjectMechanism(SubjectMechanism):
            def run(self):
                global called
                called += 1
                return {'foo': 42}

        class MySubject(Subject):
            behaviours_classes = []

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 0
コード例 #2
0
    def test_subject_behaviour_produce_data(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()

        class MySubject(Subject):
            behaviours_classes = [MySubjectBehaviour]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )
        results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert results_by_subjects
        assert id(my_subject) in results_by_subjects
        assert MySubjectBehaviour in results_by_subjects[id(my_subject)]
        assert 'bar' in results_by_subjects[id(my_subject)][MySubjectBehaviour]
        assert 142 == results_by_subjects[id(my_subject)][MySubjectBehaviour]['bar']
コード例 #3
0
    def test_subject_behavior_not_called_if_no_more_subjects(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()

        class MySubject(Subject):
            behaviours_classes = [MySubjectBehaviour]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )
        results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert results_by_subjects
        assert id(my_subject) in results_by_subjects
        assert results_by_subjects[id(my_subject)]

        # If we remove subject, no more data generated
        subjects.remove(my_subject)
        results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert not results_by_subjects
コード例 #4
0
    def test_subject_behaviour_cycle_frequency(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()

        class MySubject(Subject):
            behaviours_classes = [MyCycledSubjectBehaviour]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )

        # Cycle 0: behaviour IS executed
        cycle_manager.current_cycle = 0
        results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert results_by_subjects

        # Cycle 1: behaviour IS NOT executed
        cycle_manager.current_cycle = 1
        results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert not results_by_subjects
コード例 #5
0
ファイル: test_cycle.py プロジェクト: buxx/synergine2
    def test_new_subject(self):
        shared.reset()
        config = Config({'core': {'use_x_cores': 1}})

        simulation = Simulation(config)
        subjects = MySubjects(simulation=simulation)
        simulation.subjects = subjects

        for i in range(3):
            subjects.append(MySubject(config, simulation=simulation))

        cycle_manager = CycleManager(
            config=config,
            simulation=simulation,
        )

        events = cycle_manager.next()

        assert 3 == len(events)
        event_values = [e.value for e in events]
        assert all([s.id * 2 in event_values for s in subjects])

        subjects.append(MySubject(config, simulation=simulation))
        events = cycle_manager.next()
        cycle_manager.stop()

        assert 4 == len(events)
        event_values = [e.value for e in events]
        assert all([s.id * 2 in event_values for s in subjects])
コード例 #6
0
    def test_mechanism_not_called_if_subject_behavior_timebase_not_active_yet(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()
        global called
        called = 0

        class MySubjectMechanism(SubjectMechanism):
            def run(self):
                global called
                called += 1
                return {'foo': 42}

        class MySubjectBehaviour1(SubjectBehaviour):
            use = [MySubjectMechanism]

            @property
            def seconds_frequency(self):
                return 1.0

            def run(self, data):
                self.last_execution_time = time.time()
                return {'bar': data[MySubjectMechanism]['foo'] + 100}

        class MySubject(Subject):
            behaviours_classes = [MySubjectBehaviour1]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )

        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0)):
            cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert called == 1

        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 500000)):
            cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert called == 1

        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 700000)):
            cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert called == 1

        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 1, 500000)):
            cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert called == 2
コード例 #7
0
    def test_mechanism_not_called_if_subject_behavior_cycled_not_active_yet(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()
        global called
        called = 0

        class MySubjectMechanism(SubjectMechanism):
            def run(self):
                global called
                called += 1
                return {'foo': 42}

        class MySubjectBehaviour1(SubjectBehaviour):
            use = [MySubjectMechanism]

            @property
            def cycle_frequency(self):
                return 2

            def run(self, data):
                return {'bar': data[MySubjectMechanism]['foo'] + 100}

        class MySubject(Subject):
            behaviours_classes = [MySubjectBehaviour1]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )

        cycle_manager.current_cycle = 0
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 1

        cycle_manager.current_cycle = 1
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 1

        cycle_manager.current_cycle = 2
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 2

        cycle_manager.current_cycle = 3
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 2
コード例 #8
0
ファイル: test_terminals.py プロジェクト: buxx/synergine2
    def test_terminal_as_main_process(self):
        shared.reset()
        config = Config()
        simulation = Simulation(config)
        simulation.subjects = Subjects(simulation=simulation)
        cycle_manager = CycleManager(
            config=config,
            simulation=simulation,
        )

        global terminal_pid
        global core_pid
        terminal_pid = 0
        core_pid = 0

        class MyMainTerminal(Terminal):
            main_process = True

            def run(self):
                global terminal_pid
                terminal_pid = os.getpid()

        terminal = MyMainTerminal(config)

        class MyCore(Core):
            def _end_cycle(self):
                self._continue = False
                global core_pid
                core_pid = os.getpid()

        core = MyCore(
            config=config,
            simulation=simulation,
            cycle_manager=cycle_manager,
            terminal_manager=TerminalManager(
                config=config,
                terminals=[terminal],
            ),
        )
        core.run()
        core.main_process_terminal.core_process.terminate()
        cycle_manager.stop()

        assert terminal_pid == os.getpid()
        assert core_pid == 0  # because changed in other process
コード例 #9
0
    def test_mechanism_called_once_for_multiple_subject_behaviors(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()
        global called
        called = 0

        class MySubjectMechanism(SubjectMechanism):
            def run(self):
                global called
                called += 1
                return {'foo': 42}

        class MySubjectBehaviour1(SubjectBehaviour):
            use = [MySubjectMechanism]

            def run(self, data):
                return {'bar': data[MySubjectMechanism]['foo'] + 100}

        class MySubjectBehaviour2(SubjectBehaviour):
            use = [MySubjectMechanism]

            def run(self, data):
                return {'bar': data[MySubjectMechanism]['foo'] + 100}

        class MySubject(Subject):
            behaviours_classes = [MySubjectBehaviour1, MySubjectBehaviour2]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 1
コード例 #10
0
    def test_subject_behaviour_seconds_frequency(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()

        class MySubject(Subject):
            behaviours_classes = [MyTimedSubjectBehaviour]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )

        # Thirst time, behaviour IS executed
        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0)):
            data = cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert data
            assert id(my_subject) in data
            assert data[id(my_subject)]

        # Less second after: NOT executed
        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 500000)):
            data = cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert not data

        # Less second after: NOT executed
        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 700000)):
            data = cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert not data

        # Less second after: IS executed
        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 1, 500000)):
            data = cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert data