Beispiel #1
0
    def test_root_perform_wait_all(self):
        """Test running a simple task waiting on all pools.

        """
        root = self.root

        event1 = threading.Event()
        event2 = threading.Event()

        par = CheckTask(name='test', custom=lambda t, x: event1.wait())
        par.parallel = {'activated': True, 'pool': 'test'}
        aux = CheckTask(name='signal', custom=lambda t, x: event2.set())
        wait = CheckTask(name='wait')
        wait.wait = {'activated': True}
        root.add_child_task(0, par)
        root.add_child_task(1, aux)
        root.add_child_task(2, wait)
        root.check()

        t = threading.Thread(target=root.perform)
        t.start()
        event2.wait()
        event1.set()
        t.join()

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert par.perform_called == 1
        assert aux.perform_called == 1
        assert wait.perform_called == 1
        assert not root.resources['threads']['test']
Beispiel #2
0
    def test_pause1(self, app):
        """Test pausing and resuming the execution. (add instrs)

        Tricky as only the main thread is allowed to resume.

        """
        class Dummy(object):
            """False instrument checking that restarting does its job.

            """
            owner = 'test'
            called = 0

            def finalize(self):
                pass

            def clear_cache(self):
                self.called += 1

        def pause(task, value):
            """Post a method restarting execution on event loop and pause.

            """
            deferred_call(lambda t: t.root.should_pause.clear(), task)
            task.root.should_pause.set()

        root = self.root
        dummy = Dummy()
        root.resources['instrs']['test'] = dummy
        par = CheckTask(name='test', custom=pause)
        comp = ComplexTask(name='comp',
                           stoppable=False,
                           parallel={
                               'activated': True,
                               'pool': 'test'
                           })
        par2 = CheckTask(name='test2')
        comp.add_child_task(0, par2)
        par3 = CheckTask(name='test3')
        for i, c in enumerate([par, comp, par3]):
            root.add_child_task(i, c)
        root.check()

        t = threading.Thread(target=root.perform)
        t.start()
        sleep(0.1)
        process_app_events()
        t.join()

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert par.perform_called == 1
        assert par2.perform_called == 1
        assert par3.perform_called == 1
        assert root.resumed.is_set()
        assert dummy.called == 1
        assert dummy.owner == ''
Beispiel #3
0
    def test_root_perform_no_wait_single(self):
        """Test running a simple task not waiting on a single pool.

        """
        root = self.root
        event1 = threading.Event()
        event2 = threading.Event()

        par = CheckTask(name='test', custom=lambda t, x: event1.wait())
        par.parallel = {'activated': True, 'pool': 'test'}
        aux = CheckTask(name='signal', custom=lambda t, x: event2.set())
        aux.parallel = {'activated': True, 'pool': 'aux'}
        wait = CheckTask(name='wait')
        wait.wait = {'activated': True, 'wait': ['test']}
        root.add_child_task(0, par)
        root.add_child_task(1, aux)
        root.add_child_task(2, wait)
        root.check()

        t = threading.Thread(target=root.perform)
        t.start()
        event2.wait()
        event1.set()
        t.join()

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert par.perform_called == 1
        assert aux.perform_called == 1
        assert wait.perform_called == 1
        assert not root.resources['threads']['test']
        assert root.resources['threads']['aux']
Beispiel #4
0
    def test_root_perform_wait_single(self):
        """Test running a simple task waiting on a single pool.

        Notes
        -----
        When starting par will be executed in its own thread, which will allow
        par2 to start (also in its own thread) as a consequence aux will be
        run. The test will wait for aux to set its flag. At this step wait
        should be waiting as one thread in test pool is active. After checking
        that, we set the flag on which par2 is waiting. This should allow wait
        to run. Once we have checked it is so, we let par complete.

        """
        root = self.root
        event1 = threading.Event()
        event2 = threading.Event()
        event3 = threading.Event()
        event4 = threading.Event()

        par = CheckTask(name='test', custom=lambda t, x: event1.wait())
        par.parallel = {'activated': True, 'pool': 'aux'}
        par2 = CheckTask(name='test', custom=lambda t, x: event2.wait())
        par2.parallel = {'activated': True, 'pool': 'test'}
        aux = CheckTask(name='signal', custom=lambda t, x: event3.set())
        wait = CheckTask(name='wait', custom=lambda t, x: event4.set())
        wait.wait = {'activated': True, 'wait': ['test']}
        root.add_child_task(0, par)
        root.add_child_task(1, par2)
        root.add_child_task(2, aux)
        root.add_child_task(3, wait)
        root.check()

        t = threading.Thread(target=root.perform)
        t.start()
        event3.wait()
        sleep(1)
        assert not wait.perform_called
        assert root.resources['active_threads']['test']
        assert root.resources['active_threads']['aux']
        event2.set()
        event4.wait()
        assert wait.perform_called
        assert root.resources['active_threads']['aux']
        assert not root.resources['active_threads']['test']
        event1.set()
        t.join()

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert par.perform_called == 1
        assert par2.perform_called == 1
        assert aux.perform_called == 1
        assert wait.perform_called == 1
        assert not root.resources['active_threads']['test']
        assert not root.resources['active_threads']['aux']
Beispiel #5
0
    def test_stop(self):
        """Test stopping the execution.

        """
        root = self.root
        par = CheckTask(name='test',
                        custom=lambda t, x: t.root.should_stop.set())
        par2 = CheckTask(name='test2')
        for i, c in enumerate([par, par2]):
            root.add_child_task(i, c)
        root.check()
        root.perform()

        assert par.perform_called == 1
        assert not par2.perform_called
Beispiel #6
0
    def test_stop_unstoppable(self):
        """Try stopping unstoppable task.

        """
        root = self.root
        par = CheckTask(name='test',
                        custom=lambda t, x: t.root.should_stop.set())
        par2 = CheckTask(name='test2', stoppable=False)
        for i, c in enumerate([par, par2]):
            root.add_child_task(i, c)
        root.check()
        root.perform()

        assert par.perform_called == 1
        assert par2.perform_called == 1
    def test_performing_stop3(self, iterable_interface):
        """Test handling stop in the middle of an iteration.

        Child, no timing

        """
        self.task.interface = iterable_interface
        self.task.task = CheckTask(name='check')
        stop = lambda t, v: t.root.should_stop.set()
        self.task.add_child_task(
            0, CheckTask(name='Stop', custom=stop, stoppable=False))
        self.task.prepare()

        self.task.perform()

        assert self.task.children[0].perform_called == 1
    def test_perform_task3(self, iterable_interface):
        """Test performing a loop with an embedded task no timing. Continue.

        """
        self.task.interface = iterable_interface
        self.task.task = CheckTask(name='check')
        self.task.add_child_task(
            0, ContinueTask(name='Continue', condition='True'))
        self.task.children.append(CheckTask(name='check'))
        self.root.prepare()

        self.task.perform()
        assert self.root.get_from_database('Test_index') == 11
        assert self.task.task.perform_called
        assert self.task.task.perform_value == 10
        assert not self.task.children[1].perform_called
    def test_traverse(self, linspace_interface):
        """Test traversing a with interfaces ComplexTask.

        """
        self.task.interface = linspace_interface
        self.task.add_child_task(0, CheckTask(name='check'))
        assert len(list(self.task.traverse())) == 3
Beispiel #10
0
    def test_handle_task_exception_in_thread(self):
        """Test handling an exception occuring in a thread (test smooth_crash).

        """
        def raiser(task, value):
            raise Exception

        root = self.root
        aux = CheckTask(name='test', custom=raiser)
        aux.parallel = {'activated': True, 'pool': 'test'}
        root.add_child_task(0, aux)
        root.add_child_task(1, CheckTask())
        root.check()
        root.perform()

        assert not root.should_pause.is_set()
        assert root.should_stop.is_set()
        assert aux.perform_called == 1
Beispiel #11
0
    def test_handle_task_exception_in_thread(self):
        """Test handling an exception occuring in a thread (test smooth_crash).

        """
        def raiser(task, value):
            raise Exception()

        root = self.root
        aux = CheckTask(name='test', custom=raiser)
        aux.parallel = {'activated': True, 'pool': 'test'}
        root.add_child_task(0, aux)
        root.add_child_task(1, CheckTask())
        root.check()
        root.perform()

        assert not root.should_pause.is_set()
        assert root.should_stop.is_set()
        assert aux.perform_called == 1
    def test_perform_task1(self, iterable_interface):
        """Test performing a loop with an embedded task no timing.

        """
        self.task.interface = iterable_interface
        self.task.task = CheckTask(name='check')
        self.root.prepare()

        self.task.perform()
        assert self.root.get_from_database('Test_index') == 11
        assert self.task.task.perform_called
        assert self.task.task.perform_value == 10
Beispiel #13
0
    def test_root_perform_parallel_in_finalization(self):
        """Ensure that the ThreadResources release does not prevent to start
        new threads.

        """
        root = self.root

        event1 = threading.Event()
        event2 = threading.Event()
        event3 = threading.Event()

        comp = ComplexTask(name='comp')
        comp.parallel = {'activated': True, 'pool': 'test'}
        aux = CheckTask(name='signal', custom=lambda t, x: event1.set())
        wait = CheckTask(name='test', custom=lambda t, x: event2.wait())
        par = CheckTask(name='signal', custom=lambda t, x: event3.set())
        # Test creating a new thread as by priority active_threads is released
        # later.
        par.parallel = {'activated': True, 'pool': 'test2'}
        comp.add_child_task(0, aux)
        comp.add_child_task(1, wait)
        comp.add_child_task(2, par)
        root.add_child_task(0, comp)
        root.check()

        t = threading.Thread(target=root.perform)
        t.start()
        event1.wait()
        assert root.resources['active_threads']['test']
        assert not root.resources['active_threads']['test2']
        event2.set()
        event3.wait()
        t.join()

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert par.perform_called == 1
        assert aux.perform_called == 1
        assert wait.perform_called == 1
        assert not root.resources['active_threads']['test']
Beispiel #14
0
    def test_perform4(self):
        """Test handling stopping while iterating.

        """
        self.task.condition = 'True'
        stop = lambda t, v: t.root.should_stop.set()
        self.task.add_child_task(
            0, CheckTask(name='Stop', custom=stop, stoppable=False))
        self.root.prepare()

        self.task.perform()

        assert self.task.children[0].perform_called == 1
Beispiel #15
0
    def test_root_perform_simple(self):
        """Test running a simple task.

        """
        root = self.root
        aux = CheckTask(name='test')
        root.add_child_task(0, aux)
        root.check()
        root.perform()

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert aux.perform_called == 1
Beispiel #16
0
    def test_pause2(self, app):
        """Test pausing and stopping the execution.

        """
        def pause_and_stop(task, value):
            """Post a method stopping execution on event loop and pause.

            """
            deferred_call(lambda t: t.root.should_stop.set(), task)
            task.root.should_pause.set()

        root = self.root
        par = CheckTask(name='test', custom=pause_and_stop)
        comp = ComplexTask(name='comp',
                           stoppable=False,
                           parallel={
                               'activated': True,
                               'pool': 'test'
                           })
        par2 = CheckTask(name='test2')
        comp.add_child_task(0, par2)
        par3 = CheckTask(name='test3')
        for i, c in enumerate([par, comp, par3]):
            root.add_child_task(i, c)
        root.check()

        t = threading.Thread(target=root.perform)
        t.start()
        sleep(0.1)
        process_app_events()
        t.join()

        assert root.should_pause.is_set()
        assert root.should_stop.is_set()
        assert par.perform_called
        assert not par2.perform_called
        assert not par3.perform_called
    def test_perform4(self, iterable_interface):
        """Test performing a simple loop no timing. Continue

        """
        self.task.interface = iterable_interface
        for i, t in enumerate([
                ContinueTask(name='break', condition='True'),
                CheckTask(name='check')
        ]):
            self.task.add_child_task(i, t)

        self.root.prepare()

        self.task.perform()
        assert not self.task.children[1].perform_called
    def test_perform_timing3(self, iterable_interface):
        """Test performing a simple loop timing. Continue.

        """
        self.task.interface = iterable_interface
        self.task.timing = True
        self.task.add_child_task(
            0, ContinueTask(name='Continue', condition='True'))
        self.task.add_child_task(1, CheckTask(name='check'))

        self.root.prepare()

        self.task.perform()
        assert not self.task.children[1].perform_called
        assert self.root.get_from_database('Test_elapsed_time') != 1.0
Beispiel #19
0
    def test_root_perform_parallel(self):
        """Test running a simple task in parallel.

        """
        main = threading.current_thread().name

        def thread_checker(task, value):
            """Check that this is not running in the main thread.

            """
            assert threading.current_thread().name != main

        root = self.root
        aux = CheckTask(name='test', custom=thread_checker)
        aux.parallel = {'activated': True, 'pool': 'test'}
        root.add_child_task(0, aux)
        root.add_child_task(1, CheckTask())
        root.check()
        root.perform()

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert aux.perform_called == 1
        assert root.resources['threads']['test']
Beispiel #20
0
    def test_root_perform_parallel(self):
        """Test running a simple task in parallel.

        """
        main = threading.current_thread().name

        def thread_checker(task, value):
            """Check that this is not running in the main thread.

            """
            assert task.root.resources['threads']['test']
            assert threading.current_thread().name != main

        root = self.root
        aux = CheckTask(name='test', custom=thread_checker)
        aux.parallel = {'activated': True, 'pool': 'test'}
        root.add_child_task(0, aux)
        root.add_child_task(1, CheckTask())
        root.check()
        root.perform()

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert aux.perform_called == 1
    def test_perform_task2(self):
        """Test performing a loop with an embedded task no timing. Break.

        """
        interface = IterableLoopInterface()
        interface.iterable = 'range(11)'
        self.task.interface = interface
        self.task.task = CheckTask(name='check')
        self.task.add_child_task(
            0, BreakTask(name='Break', condition='{Test_index} == 6'))
        self.root.prepare()

        self.task.perform()
        assert self.root.get_from_database('Test_index') == 6
        assert self.task.task.perform_called
        assert self.task.task.perform_value == 5
    def test_perform_timing_task2(self, iterable_interface):
        """Test performing a loop with an embedded task no timing. Break.

        """
        self.task.interface = iterable_interface
        self.task.timing = True
        self.task.task = CheckTask(name='check')
        self.task.add_child_task(
            0, BreakTask(name='break', condition='{Test_index} == 1'))

        self.root.prepare()

        self.task.perform()
        assert self.root.get_from_database('Test_index') == 1
        assert self.task.task.perform_called
        assert self.task.task.perform_value == 0
        assert self.root.get_from_database('Test_elapsed_time') != 1.0
Beispiel #23
0
    def test_root_perform_profile(self, tmpdir):
        """Test running a simple task.

        """
        self.root.default_path = str(tmpdir)
        root = self.root
        aux = CheckTask(name='test')
        root.add_child_task(0, aux)
        root.check()
        root.should_profile = True
        root.perform()

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert aux.perform_called == 1

        meas_name = self.root.get_from_database('meas_name')
        meas_id = self.root.get_from_database('meas_id')
        path = os.path.join(self.root.default_path,
                            meas_name + '_' + meas_id + '.prof')
        assert os.path.isfile(path)
    def test_subtask_handling(self):
        """Test adding, changing, removing the subtask.

        """
        subtask1 = CheckTask(name='check', database_entries={'val': 1})
        self.task.task = subtask1

        assert subtask1.root is self.root
        assert subtask1.database is self.root.database
        assert subtask1.parent is self.task
        assert subtask1.path and subtask1.depth
        assert 'value' not in self.task.database_entries
        assert subtask1.get_from_database('check_val')
        assert self.task.preferences['task']['name'] == 'check'

        subtask2 = CheckTask(name='rep', database_entries={'new': 1})
        self.task.task = subtask2

        assert not subtask1.root
        assert not subtask1.parent
        with pytest.raises(KeyError):
            assert subtask1.get_from_database('check_val')

        assert subtask2.root is self.root
        assert subtask2.database is self.root.database
        assert subtask2.parent is self.task
        assert subtask2.path and subtask1.depth
        assert 'value' not in self.task.database_entries
        assert subtask2.get_from_database('rep_new')
        assert self.task.preferences['task']['name'] == 'rep'

        self.task.task = None

        assert not subtask2.root
        assert not subtask2.parent
        with pytest.raises(KeyError):
            assert subtask2.get_from_database('rep_new')
        assert 'value' in self.task.database_entries
Beispiel #25
0
    def test_saving_building_from_config(self, iterable_interface):
        """Done here as the LoopTask is a viable case of a member tagged with
        child.

        """
        subtask1 = CheckTask(name='check', database_entries={'val': 1})
        self.task.task = subtask1

        self.root.update_preferences_from_members()

        deps = {
            'ecpy.task': {
                'ecpy.RootTask': RootTask,
                'ecpy.LoopTask': LoopTask,
                'ecpy.CheckTask': CheckTask
            }
        }
        new = RootTask.build_from_config(self.root.preferences, deps)

        assert new.children[0].task.name == 'check'

        self.task.interface = iterable_interface
        self.root.update_preferences_from_members()
        prefs = self.root.preferences
        assert prefs['children_0'].sections[1] == 'task'
        del prefs['children_0']['task']
        deps = {
            'ecpy.task': {
                'ecpy.RootTask': RootTask,
                'ecpy.LoopTask': LoopTask,
                'ecpy.CheckTask': CheckTask
            },
            'ecpy.tasks.interface': {
                'ecpy.LoopTask:ecpy.IterableLoopInterface':
                IterableLoopInterface
            }
        }
        new = RootTask.build_from_config(prefs, deps)

        assert not new.children[0].task
Beispiel #26
0
    def test_root_perform_wait_all(self):
        """Test running a simple task waiting on all pools.

        Notes
        -----
        When starting par will be executed in its own thread, which will allow
        aux to run. The test will wait for aux to set its flag. At this step
        wait should be waiting as one pool is active. After checking that we
        can set the flag on which par is waiting and let the execution
        complete.

        """
        root = self.root

        event1 = threading.Event()
        event2 = threading.Event()

        par = CheckTask(name='test', custom=lambda t, x: event1.wait())
        par.parallel = {'activated': True, 'pool': 'test'}
        aux = CheckTask(name='signal', custom=lambda t, x: event2.set())
        wait = CheckTask(name='wait')
        wait.wait = {'activated': True}
        root.add_child_task(0, par)
        root.add_child_task(1, aux)
        root.add_child_task(2, wait)
        root.check()

        t = threading.Thread(target=root.perform)
        t.start()
        event2.wait()
        sleep(1)
        assert not wait.perform_called
        assert root.resources['active_threads']['test']
        event1.set()
        t.join()

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert par.perform_called == 1
        assert aux.perform_called == 1
        assert wait.perform_called == 1
        assert not root.resources['active_threads']['test']
Beispiel #27
0
    def test_subtask_handling(self):
        """Test adding, changing, removing the subtask.

        """
        subtask1 = CheckTask(name='check', database_entries={'val': 1})
        self.task.task = subtask1

        assert subtask1.root is self.root
        assert subtask1.database is self.root.database
        assert subtask1.parent is self.task
        assert subtask1.path and subtask1.depth
        assert 'value' not in self.task.database_entries
        assert subtask1.get_from_database('check_val')
        assert self.task.preferences['task']['name'] == 'check'

        subtask2 = CheckTask(name='rep', database_entries={'new': 1})
        self.task.task = subtask2

        assert not subtask1.root
        assert not subtask1.parent
        with pytest.raises(KeyError):
            assert subtask1.get_from_database('check_val')

        assert subtask2.root is self.root
        assert subtask2.database is self.root.database
        assert subtask2.parent is self.task
        assert subtask2.path and subtask1.depth
        assert 'value' not in self.task.database_entries
        assert subtask2.get_from_database('rep_new')
        assert self.task.preferences['task']['name'] == 'rep'

        self.task.task = None

        assert not subtask2.root
        assert not subtask2.parent
        with pytest.raises(KeyError):
            assert subtask2.get_from_database('rep_new')
        assert 'value' in self.task.database_entries
Beispiel #28
0
 def setup(self):
     self.root = RootTask(should_stop=Event(), should_pause=Event())
     self.task = WhileTask(name='Test')
     self.root.add_child_task(0, self.task)
     self.check = CheckTask(name='check')
     self.task.add_child_task(0, self.check)