Exemplo n.º 1
0
    def test_pause2(self, exopy_qtbot):
        """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)

        t = threading.Thread(target=root.perform)
        t.start()
        sleep(0.1)
        exopy_qtbot.wait(10)
        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
Exemplo n.º 2
0
    def test_pause2(self, exopy_qtbot):
        """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)

        t = threading.Thread(target=root.perform)
        t.start()
        sleep(0.1)
        exopy_qtbot.wait(10)
        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
Exemplo n.º 3
0
def test_moving_through_remove_add_child():
    """Test moving a child between different tasks through remove/add.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1', database_entries={'val1': 2.0})
    task2 = ComplexTask(name='task2', database_entries={'val2': 2.0})
    task3 = SimpleTask(name='task3',
                       database_entries={'val3': 1},
                       access_exs={'val3': 2})

    task2.add_child_task(0, task3)
    task1.add_child_task(1, task2)
    root.add_child_task(0, task1)

    task1.remove_child_task(0)

    assert task2.parent is None
    assert task2.root is None
    assert task2.database is None
    assert task3.parent is not None
    assert task3.root is None
    assert task3.database is None

    root.add_child_task(0, task2)

    assert len(root.children) == 2
Exemplo n.º 4
0
def test_task_renaming():
    """Test renaming simple and complex task.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1', database_entries={'val1': 2.0})
    task2 = ComplexTask(name='task2')
    task3 = SimpleTask(name='task3',
                       database_entries={'val2': 1},
                       access_exs={'val2': 2})

    task2.add_child_task(0, task3)
    task1.add_child_task(0, task2)
    root.add_child_task(0, task1)

    task3.name = 'worker3'
    with pytest.raises(KeyError):
        root.get_from_database('task3_val2')
    assert root.get_from_database('worker3_val2') == 1

    task1.name = 'worker1'
    with pytest.raises(KeyError):
        root.get_from_database('task1_val1')
    assert root.get_from_database('worker1_val1') == 2.0
    assert root.get_from_database('worker3_val2') == 1
Exemplo n.º 5
0
def test_database_update_with_exception():
    """Test that replacing the database_entries members refreshes the database.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1',
                        database_entries={'val1': 2.0})
    task2 = SimpleTask(name='task2',
                       database_entries={'val2': 1},
                       access_exs={'val2': 1})
    task3 = ComplexTask(name='task3')
    task1.add_child_task(0, task2)
    root.add_child_task(0, task1)
    root.add_child_task(1, task3)

    assert task3.get_from_database('task2_val2')

    entries = task2.database_entries.copy()
    del entries['val2']
    task2.database_entries = entries

    with pytest.raises(KeyError):
        task1.get_from_database('task2_val2')

    with pytest.raises(KeyError):
        task3.get_from_database('task2_val2')
Exemplo n.º 6
0
def test_moving_child():
    """Test moving a child.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1',
                        database_entries={'val1': 2.0})
    task2 = SimpleTask(name='task2',
                       database_entries={'val2': 1},
                       access_exs={'val2': 2})
    task3 = ComplexTask(name='task3')
    task4 = ComplexTask(name='task4')

    task1.add_child_task(0, task2)
    task1.add_child_task(1, task4)
    root.add_child_task(0, task1)
    root.add_child_task(1, task3)

    listener = SignalListener()
    task1.observe('children_changed', listener.listen)

    assert task1.preferences['children_0']['name'] == 'task2'
    assert task1.preferences['children_1']['name'] == 'task4'

    task1.move_child_task(0, 1)

    assert listener.counter == 1
    assert listener.signals[0].moved

    assert task1.preferences['children_0']['name'] == 'task4'
    assert task1.preferences['children_1']['name'] == 'task2'
    assert task3.get_from_database('task2_val2') == 1
Exemplo n.º 7
0
    def test_root_perform_complex(self):
        """Test running a simple task.

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

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert aux.perform_called == 1
Exemplo n.º 8
0
    def test_root_perform_complex(self):
        """Test running a simple task.

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

        assert not root.should_pause.is_set()
        assert not root.should_stop.is_set()
        assert aux.perform_called == 1
Exemplo n.º 9
0
def test_moving_through_remove_add_child():
    """Test moving a child between different tasks through remove/add.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1',
                        database_entries={'val1': 2.0})
    task2 = ComplexTask(name='task2',
                        database_entries={'val2': 2.0})
    task3 = SimpleTask(name='task3',
                       database_entries={'val3': 1},
                       access_exs={'val3': 2})

    task2.add_child_task(0, task3)
    task1.add_child_task(1, task2)
    root.add_child_task(0, task1)

    task1.remove_child_task(0)

    assert task2.parent is None
    assert task2.root is None
    assert task2.database is None
    assert task3.parent is not None
    assert task3.root is None
    assert task3.database is None

    root.add_child_task(0, task2)

    assert len(root.children) == 2
Exemplo n.º 10
0
def test_task_renaming():
    """Test renaming simple and complex task.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1',
                        database_entries={'val1': 2.0})
    task2 = ComplexTask(name='task2')
    task3 = SimpleTask(name='task3',
                       database_entries={'val2': 1},
                       access_exs={'val2': 2})

    task2.add_child_task(0, task3)
    task1.add_child_task(0, task2)
    root.add_child_task(0, task1)

    task3.name = 'worker3'
    with pytest.raises(KeyError):
        root.get_from_database('task3_val2')
    assert root.get_from_database('worker3_val2') == 1

    task1.name = 'worker1'
    with pytest.raises(KeyError):
        root.get_from_database('task1_val1')
    assert root.get_from_database('worker1_val1') == 2.0
    assert root.get_from_database('worker3_val2') == 1
Exemplo n.º 11
0
def test_traverse():
    """Test traversing a task hierarchy to collect infos.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1', database_entries={'val1': 2.0})
    task2 = SimpleTask(name='task2',
                       database_entries={'val2': 1},
                       access_exs={'val2': 2})
    task3 = ComplexTask(name='task3')
    task1.add_child_task(0, task2)
    root.add_child_task(0, task1)
    root.add_child_task(1, task3)

    flat = list(root.traverse())
    assert flat == [root, task1, task2, task3]

    flat = list(root.traverse(0))
    assert flat == [root, task1, task3]
Exemplo n.º 12
0
def test_traverse():
    """Test traversing a task hierarchy to collect infos.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1',
                        database_entries={'val1': 2.0})
    task2 = SimpleTask(name='task2',
                       database_entries={'val2': 1},
                       access_exs={'val2': 2})
    task3 = ComplexTask(name='task3')
    task1.add_child_task(0, task2)
    root.add_child_task(0, task1)
    root.add_child_task(1, task3)

    flat = list(root.traverse())
    assert flat == [root, task1, task2, task3]

    flat = list(root.traverse(0))
    assert flat == [root, task1, task3]
Exemplo n.º 13
0
def test_build_complex_from_config():
    """Test building a complex task from config.

    """
    config = {'name': 'test',
              'children_0': {'name': 'test_child',
                             'task_id': 'SimpleTask'}}
    task = ComplexTask.build_from_config(config,
                                         {'exopy.task':
                                             {'SimpleTask': SimpleTask}})
    assert task.name == 'test'
    assert len(task.children) == 1
    assert task.children[0].name == 'test_child'
    assert isinstance(task.children[0], SimpleTask)
Exemplo n.º 14
0
def test_deleting_child():
    """Test deleting a child.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1',
                        database_entries={'val1': 2.0})
    task2 = SimpleTask(name='task2',
                       database_entries={'val2': 1},
                       access_exs={'val2': 2})
    task3 = ComplexTask(name='task3')
    task4 = ComplexTask(name='task4')

    task1.add_child_task(0, task2)
    task1.add_child_task(1, task4)
    root.add_child_task(0, task1)
    root.add_child_task(1, task3)

    listener = SignalListener()
    task1.observe('children_changed', listener.listen)

    task1.remove_child_task(0)

    assert task2.parent is None
    assert task2.root is None
    assert task2.database is None
    assert listener.counter == 1
    assert listener.signals[0].removed

    assert task1.preferences['children_0']['name'] == 'task4'
    assert 'task2_val2' not in task3.list_accessible_database_entries()

    root.remove_child_task(0)

    assert task1.parent is None
    assert task1.root is None
    assert task1.database is None
    assert task4.root is None
    assert task4.database is None
    assert len(root.children) == 1
    with pytest.raises(KeyError):
        root.get_from_database('task1_val1')
Exemplo n.º 15
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']
Exemplo n.º 16
0
def test_swapping(exopy_qtbot, task_workbench, dialog_sleep):
    """Test moving a view between containers.

    """
    task = RootTask()
    view = RootTaskView(task=task,
                        core=task_workbench.get_plugin('enaml.workbench.core'))

    subtask = ComplexTask(name='Test')
    subview = view.view_for(subtask)

    task.add_child_task(0, subtask)

    cont = Container()

    show_widget(exopy_qtbot, cont)
    view.set_parent(cont)
    view.refresh()

    def assert_children():
        assert cont.children == [view]

    exopy_qtbot.wait_until(assert_children)
    exopy_qtbot.wait(dialog_sleep)

    view.set_parent(None)
    subview.set_parent(cont)
    subview.refresh()

    def assert_children():
        assert cont.children == [subview]

    exopy_qtbot.wait_until(assert_children)
    exopy_qtbot.wait(dialog_sleep)

    subview.set_parent(None)
    view.set_parent(cont)
    view.refresh()

    def assert_children():
        assert cont.children == [view]

    exopy_qtbot.wait_until(assert_children)
    assert subview.visible
    exopy_qtbot.wait(dialog_sleep)
Exemplo n.º 17
0
def test_build_complex_from_config():
    """Test building a complex task from config.

    """
    config = {
        'name': 'test',
        'children_0': {
            'name': 'test_child',
            'task_id': 'SimpleTask'
        }
    }
    task = ComplexTask.build_from_config(
        config, {'exopy.task': {
            'SimpleTask': SimpleTask
        }})
    assert task.name == 'test'
    assert len(task.children) == 1
    assert task.children[0].name == 'test_child'
    assert isinstance(task.children[0], SimpleTask)
Exemplo n.º 18
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)

        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']
Exemplo n.º 19
0
def test_adding_child():
    """Test adding children.

    This test adding a child with and without access_exs to a task which is not
    root and then to the root. This makes sure that giving the root afterwards
    does trigger the right updates.

    """
    root = RootTask()
    listener = SignalListener()
    root.observe('children_changed', listener.listen)
    task1 = ComplexTask(name='task1', database_entries={'val1': 2.0})
    task2 = SimpleTask(name='task2',
                       database_entries={'val2': 1},
                       access_exs={'val2': 2})
    task3 = ComplexTask(name='task3')
    task1.add_child_task(0, task2)
    root.add_child_task(0, task1)
    root.add_child_task(1, task3)

    assert task1.depth == 1
    assert task1.path == 'root'
    assert task1.database is root.database
    assert task1.root is root
    assert task1.parent is root

    assert task2.depth == 2
    assert task2.path == 'root/task1'
    assert task2.database is root.database
    assert task2.root is root
    assert task2.parent is task1

    assert task1.get_from_database('task1_val1') == 2.0
    assert root.get_from_database('task1_val1') == 2.0
    assert task3.get_from_database('task2_val2') == 1

    assert listener.counter == 2
    assert all([bool(c.added) for c in listener.signals])
Exemplo n.º 20
0
def test_adding_child():
    """Test adding children.

    This test adding a child with and without access_exs to a task which is not
    root and then to the root. This makes sure that giving the root afterwards
    does trigger the right updates.

    """
    root = RootTask()
    listener = SignalListener()
    root.observe('children_changed', listener.listen)
    task1 = ComplexTask(name='task1',
                        database_entries={'val1': 2.0})
    task2 = SimpleTask(name='task2',
                       database_entries={'val2': 1},
                       access_exs={'val2': 2})
    task3 = ComplexTask(name='task3')
    task1.add_child_task(0, task2)
    root.add_child_task(0, task1)
    root.add_child_task(1, task3)

    assert task1.depth == 1
    assert task1.path == 'root'
    assert task1.database is root.database
    assert task1.root is root
    assert task1.parent is root

    assert task2.depth == 2
    assert task2.path == 'root/task1'
    assert task2.database is root.database
    assert task2.root is root
    assert task2.parent is task1

    assert task1.get_from_database('task1_val1') == 2.0
    assert root.get_from_database('task1_val1') == 2.0
    assert task3.get_from_database('task2_val2') == 1

    assert listener.counter == 2
    assert all([bool(c.added) for c in listener.signals])
Exemplo n.º 21
0
def test_root_view(exopy_qtbot, task_workbench, dialog_sleep):
    """Test the behavior of the root task view.

    """
    task = RootTask()
    view = RootTaskView(task=task,
                        core=task_workbench.get_plugin('enaml.workbench.core'))
    editor = view.children[-1]

    win = show_widget(exopy_qtbot, view)
    exopy_qtbot.wait(dialog_sleep)
    assert editor.task is task
    assert editor.root is view

    TASK_NAME = 'Foo'

    def answer_dialog(bot, dial):
        selector = dial.selector
        selector.selected_task = 'exopy.ComplexTask'
        dial.config.task_name = TASK_NAME

    with handle_dialog(exopy_qtbot, 'accept', answer_dialog, cls=BuilderView):
        editor._empty_button.clicked = True

    def assert_task_children():
        assert task.children

    exopy_qtbot.wait_until(assert_task_children)
    assert type(task.children[0]) is ComplexTask
    assert len(editor._children_buttons) == 1
    exopy_qtbot.wait(dialog_sleep)

    TASK_NAME = 'Bar'
    with handle_dialog(exopy_qtbot, 'accept', answer_dialog, cls=BuilderView):
        editor.operations['add'](0, 'after')
    exopy_qtbot.wait(10)
    exopy_qtbot.wait(dialog_sleep)

    task.children[0].add_child_task(0, ComplexTask(name='Test'))
    get_window(exopy_qtbot).maximize()
    exopy_qtbot.wait(10)
    exopy_qtbot.wait(dialog_sleep)

    editor.operations['move'](0, 1)
    exopy_qtbot.wait(10)
    exopy_qtbot.wait(dialog_sleep)

    task.remove_child_task(1)
    exopy_qtbot.wait(10)
    exopy_qtbot.wait(dialog_sleep)
    assert len(view._cache) == 2

    # Test removing the last child and removing a view for an already removed
    # task
    child_task = task.children[0]
    editor.operations['remove'](0)
    exopy_qtbot.wait(10)
    exopy_qtbot.wait(dialog_sleep)
    assert len(view._cache) == 1

    view.discard_view(child_task)

    win.close()
Exemplo n.º 22
0
    def test_pause1(self, exopy_qtbot):
        """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

        class Starter(object):
            """False instrument starter.

            """
            def reset(self, driver):
                driver.clear_cache()
                driver.owner = ''

            def stop(self, driver):
                driver.stop()

        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, Starter()
        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)

        t = threading.Thread(target=root.perform)
        t.start()
        sleep(0.1)
        exopy_qtbot.wait(10)
        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 == ''
Exemplo n.º 23
0
def test_access_exceptions():
    """Test adding, modifying and removing an access exception after creation.

    """
    root = RootTask()
    listener = SignalListener()
    root.observe('children_changed', listener.listen)
    task1 = ComplexTask(name='task1',
                        database_entries={'val1': 2.0})
    task2 = ComplexTask(name='task2')
    task3 = SimpleTask(name='task3',
                       database_entries={'val2': 1, 'val3': 2},
                       )

    task2.add_child_task(0, task3)
    task1.add_child_task(0, task2)
    root.add_child_task(0, task1)

    with pytest.raises(KeyError):
        task2.get_from_database('task3_val2')

    task3.add_access_exception('val2', 1)
    task3.add_access_exception('val3', 1)

    assert task2.get_from_database('task3_val2') == 1
    assert task2.get_from_database('task3_val2') == 1
    with pytest.raises(KeyError):
        task1.get_from_database('task3_val2')

    task3.modify_access_exception('val2', 2)
    task3.modify_access_exception('val3', -1)
    assert task1.get_from_database('task3_val2') == 1
    with pytest.raises(KeyError):
        task2.get_from_database('task3_val3')

    task3.remove_access_exception('val2')
    with pytest.raises(KeyError):
        task2.get_from_database('task3_val2')
Exemplo n.º 24
0
def test_database_update_with_exception():
    """Test that replacing the database_entries members refreshes the database.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1', database_entries={'val1': 2.0})
    task2 = SimpleTask(name='task2',
                       database_entries={'val2': 1},
                       access_exs={'val2': 1})
    task3 = ComplexTask(name='task3')
    task1.add_child_task(0, task2)
    root.add_child_task(0, task1)
    root.add_child_task(1, task3)

    assert task3.get_from_database('task2_val2')

    entries = task2.database_entries.copy()
    del entries['val2']
    task2.database_entries = entries

    with pytest.raises(KeyError):
        task1.get_from_database('task2_val2')

    with pytest.raises(KeyError):
        task3.get_from_database('task2_val2')
Exemplo n.º 25
0
def test_access_exceptions():
    """Test adding, modifying and removing an access exception after creation.

    """
    root = RootTask()
    listener = SignalListener()
    root.observe('children_changed', listener.listen)
    task1 = ComplexTask(name='task1', database_entries={'val1': 2.0})
    task2 = ComplexTask(name='task2')
    task3 = SimpleTask(
        name='task3',
        database_entries={
            'val2': 1,
            'val3': 2
        },
    )

    task2.add_child_task(0, task3)
    task1.add_child_task(0, task2)
    root.add_child_task(0, task1)

    with pytest.raises(KeyError):
        task2.get_from_database('task3_val2')

    task3.add_access_exception('val2', 1)
    task3.add_access_exception('val3', 1)

    assert task2.get_from_database('task3_val2') == 1
    assert task2.get_from_database('task3_val2') == 1
    with pytest.raises(KeyError):
        task1.get_from_database('task3_val2')

    task3.modify_access_exception('val2', 2)
    task3.modify_access_exception('val3', -1)
    assert task1.get_from_database('task3_val2') == 1
    with pytest.raises(KeyError):
        task2.get_from_database('task3_val3')

    task3.remove_access_exception('val2')
    with pytest.raises(KeyError):
        task2.get_from_database('task3_val2')
Exemplo n.º 26
0
    def test_pause1(self, exopy_qtbot):
        """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

        class Starter(object):
            """False instrument starter.

            """
            def reset(self, driver):
                driver.clear_cache()
                driver.owner = ''

            def stop(self, driver):
                driver.stop()

        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, Starter()
        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)

        t = threading.Thread(target=root.perform)
        t.start()
        sleep(0.1)
        exopy_qtbot.wait(10)
        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 == ''
Exemplo n.º 27
0
def test_deleting_child():
    """Test deleting a child.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1', database_entries={'val1': 2.0})
    task2 = SimpleTask(name='task2',
                       database_entries={'val2': 1},
                       access_exs={'val2': 2})
    task3 = ComplexTask(name='task3')
    task4 = ComplexTask(name='task4')

    task1.add_child_task(0, task2)
    task1.add_child_task(1, task4)
    root.add_child_task(0, task1)
    root.add_child_task(1, task3)

    listener = SignalListener()
    task1.observe('children_changed', listener.listen)

    task1.remove_child_task(0)

    assert listener.counter == 1
    assert listener.signals[0].removed

    assert task1.preferences['children_0']['name'] == 'task4'
    assert 'task2_val2' not in task3.list_accessible_database_entries()

    root.remove_child_task(0)

    assert len(root.children) == 1
    with pytest.raises(KeyError):
        root.get_from_database('task1_val1')
Exemplo n.º 28
0
def test_moving_child():
    """Test moving a child.

    """
    root = RootTask()
    task1 = ComplexTask(name='task1', database_entries={'val1': 2.0})
    task2 = SimpleTask(name='task2',
                       database_entries={'val2': 1},
                       access_exs={'val2': 2})
    task3 = ComplexTask(name='task3')
    task4 = ComplexTask(name='task4')

    task1.add_child_task(0, task2)
    task1.add_child_task(1, task4)
    root.add_child_task(0, task1)
    root.add_child_task(1, task3)

    listener = SignalListener()
    task1.observe('children_changed', listener.listen)

    assert task1.preferences['children_0']['name'] == 'task2'
    assert task1.preferences['children_1']['name'] == 'task4'

    task1.move_child_task(0, 1)

    assert listener.counter == 1
    assert listener.signals[0].moved

    assert task1.preferences['children_0']['name'] == 'task4'
    assert task1.preferences['children_1']['name'] == 'task2'
    assert task3.get_from_database('task2_val2') == 1