Example #1
0
def test_measurement_persistence(measurement_workbench, measurement, tmpdir,
                                 monkeypatch):
    """Test saving and reloading a measurement.

    """
    measurement_workbench.register(TasksManagerManifest())
    plugin = measurement_workbench.get_plugin('exopy.measurement')

    for m_e in ('meas_name', 'meas_id', 'meas_date', 'meas_time'):
        assert m_e in measurement.root_task.database_entries
    measurement.add_tool('pre-hook', 'dummy')
    measurement.root_task.default_path = 'test'
    measurement.pre_hooks['dummy'].fail_check = True

    path = str(tmpdir.join('test.meas.ini'))
    measurement.save(path)
    assert measurement.path == path

    loaded, errors = Measurement.load(plugin, path)
    assert loaded.root_task.default_path == 'test'
    assert loaded.pre_hooks['dummy'].fail_check
    assert loaded.path == path
    assert not errors

    # Test handling errors : root_task rebuilding and tool rebuilding.
    class CommandError(Exception):
        pass

    def generate_err(self, cmd, infos, u=None):
        raise CommandError()

    from enaml.workbench.core.core_plugin import CorePlugin
    old = CorePlugin.invoke_command
    monkeypatch.setattr(CorePlugin, 'invoke_command', generate_err)

    loaded, errors = Measurement.load(plugin, path)
    assert loaded is None
    assert 'main task' in errors and 'CommandError' in errors['main task']

    CorePlugin.invoke_command = old

    class CreationError(Exception):
        pass

    class Fail(object):
        def new(self, workbench, default=True):
            raise CreationError()

    plugin._pre_hooks.contributions['dummy'] = Fail()

    loaded, errors = Measurement.load(plugin, path)
    assert loaded is None
    assert 'pre-hook' in errors and 'dummy' in errors['pre-hook']
    assert 'CreationError' in errors['pre-hook']['dummy']
Example #2
0
def test_measurement_persistence(measurement_workbench, measurement, tmpdir,
                                 monkeypatch):
    """Test saving and reloading a measurement.

    """
    measurement_workbench.register(TasksManagerManifest())
    plugin = measurement_workbench.get_plugin('exopy.measurement')

    for m_e in ('meas_name', 'meas_id', 'meas_date', 'meas_time'):
        assert m_e in measurement.root_task.database_entries
    measurement.add_tool('pre-hook', 'dummy')
    measurement.root_task.default_path = 'test'
    measurement.pre_hooks['dummy'].fail_check = True

    path = str(tmpdir.join('test.meas.ini'))
    measurement.save(path)
    assert measurement.path == path

    loaded, errors = Measurement.load(plugin, path)
    assert loaded.root_task.default_path == 'test'
    assert loaded.pre_hooks['dummy'].fail_check
    assert loaded.path == path
    assert not errors

    # Test handling errors : root_task rebuilding and tool rebuilding.
    class CommandError(Exception):
        pass

    def generate_err(self, cmd, infos, u=None):
        raise CommandError()

    from enaml.workbench.core.core_plugin import CorePlugin
    old = CorePlugin.invoke_command
    monkeypatch.setattr(CorePlugin, 'invoke_command', generate_err)

    loaded, errors = Measurement.load(plugin, path)
    assert loaded is None
    assert 'main task' in errors and 'CommandError' in errors['main task']

    CorePlugin.invoke_command = old

    class CreationError(Exception):
        pass

    class Fail(object):
        def new(self, workbench, default=True):
            raise CreationError()

    plugin._pre_hooks.contributions['dummy'] = Fail()

    loaded, errors = Measurement.load(plugin, path)
    assert loaded is None
    assert 'pre-hook' in errors and 'dummy' in errors['pre-hook']
    assert 'CreationError' in errors['pre-hook']['dummy']
Example #3
0
def execution_view(measurement_workbench, workspace, exopy_qtbot):
    """Start plugins and add measurements before creating the execution view.

    """
    pl = measurement_workbench.get_plugin('exopy.measurement')
    pl.enqueued_measurements.add(
        Measurement(plugin=pl, root_task=RootTask(), name='Dummy', id='001'))
    pl.enqueued_measurements.add(
        Measurement(plugin=pl, root_task=RootTask(), name='Dummy', id='002'))
    pl.enqueued_measurements.measurements[1].name = 'dummy_test'
    pl.selected_engine = 'dummy'
    engine = pl.create('engine', pl.selected_engine)
    pl.processor.engine = engine

    item = ExecutionDockItem(workspace=workspace)
    return DockItemTestingWindow(widget=item)
def test_converting_a_measurement(measurement_workbench, meas_file, tmpdir,
                                  monkeypatch):
    """Test converting a measurement created using HQCMeas to make it run on
    Exopy.

    """
    import enaml
    from exopy.measurement.monitors.text_monitor import monitor
    monkeypatch.setattr(monitor, 'information', lambda *args, **kwargs: 1)
    measurement_workbench.register(TasksManagerManifest())
    measurement_workbench.register(HqcLegacyManifest())
    try:
        with enaml.imports():
            from exopy_pulses.pulses.manifest import PulsesManagerManifest
            measurement_workbench.register(PulsesManagerManifest())
            from exopy_pulses.tasks.manifest import PulsesTasksManifest
            measurement_workbench.register(PulsesTasksManifest())
            from exopy_hqc_legacy.pulses.manifest\
                import HqcLegacyPulsesManifest
            measurement_workbench.register(HqcLegacyPulsesManifest())
    except ImportError:
        print('Exopy pulses is not installed')
        print_exc()

    plugin = measurement_workbench.get_plugin('exopy.measurement')

    path = convert_measure(os.path.join(MEASURE_DIRECTORY, meas_file),
                           dest_folder=str(tmpdir))

    res, errors = Measurement.load(plugin, path)
    with open(path) as f:
        print(errors.get('main task'), f.read())
    assert res
Example #5
0
def test_running_full_measurement(exopy_qtbot, processor,
                                  measurement_with_tools, dialog_sleep,
                                  tmpdir):
    """Test running a complete measurement with pre/post-hooks and monitor.

    """
    plugin = processor.plugin.workbench.get_plugin('exopy.measurement')
    measure2 = Measurement(plugin=plugin,
                           root_task=RootTask(),
                           name='Dummy',
                           id='002')
    processor.plugin.enqueued_measurements.add(measure2)

    measurement = measurement_with_tools
    processor.continuous_processing = False
    processor.start_measurement(measurement)

    def assert_active():
        assert processor.active

    exopy_qtbot.wait_until(assert_active)

    pre_hook = measurement.pre_hooks['dummy']

    def assert_wait():
        assert pre_hook.waiting.wait(5)

    exopy_qtbot.wait_until(assert_wait, timeout=50e3)
    assert measurement is processor.running_measurement
    assert measurement.status == 'RUNNING'
    assert tmpdir.listdir()

    pre_hook.go_on.set()

    exopy_qtbot.wait_until(lambda: processor.engine.waiting.wait(0.04),
                           timeout=40e3)

    assert processor.monitors_window
    assert processor.monitors_window.measurement is measurement
    assert measurement.monitors['dummy'].running
    exopy_qtbot.wait(dialog_sleep)
    processor.engine.go_on.set()

    post_hook = measurement.post_hooks['dummy']
    exopy_qtbot.wait_until(lambda: post_hook.waiting.wait(0.04), timeout=40e3)

    assert measurement.task_execution_result
    assert not measurement.monitors['dummy'].running
    assert measurement.monitors['dummy'].received_news

    post_hook.go_on.set()

    process_and_join_thread(exopy_qtbot, processor._thread)
    assert measurement.status == 'COMPLETED'
    m = processor.plugin.workbench.get_manifest('test.measurement')
    assert not m.find('runtime_dummy1').collected
    assert not m.find('runtime_dummy2').collected

    assert measure2.status == 'READY'
Example #6
0
def measurement(measurement_workbench):
    """Register the dummy testing tools and create an empty measurement.

    """
    try:
        measurement_workbench.register(MeasureTestManifest())
    except ValueError:
        pass
    plugin = measurement_workbench.get_plugin('exopy.measurement')
    measurement = Measurement(plugin=plugin, root_task=RootTask(),
                              name='Dummy', id='001')
    return measurement
Example #7
0
def test_find_next_measurement(measurement_workbench, measurement):
    """Test finding the next valid measurement in the queue.

    """
    plugin = measurement_workbench.get_plugin('exopy.measurement')
    m1 = measurement
    m2 = Measurement(plugin=plugin,
                     root_task=RootTask(),
                     name='Dummy',
                     id='002')
    m3 = Measurement(plugin=plugin,
                     root_task=RootTask(),
                     name='Dummy',
                     id='003')
    plugin = measurement_workbench.get_plugin('exopy.measurement')
    plugin.enqueued_measurements.add(m1)
    plugin.enqueued_measurements.add(m2)
    plugin.enqueued_measurements.add(m3)

    m1.status = 'COMPLETED'
    assert plugin.find_next_measurement() is m2
Example #8
0
def test_stopping_processing_while_in_pause(exopy_qtbot, processor,
                                            measurement_with_tools):
    """Test stopping processing while in pause before starting main.

    """
    plugin = processor.plugin.workbench.get_plugin('exopy.measurement')
    measure2 = Measurement(plugin=plugin,
                           root_task=RootTask(),
                           name='Dummy',
                           id='002')
    processor.plugin.enqueued_measurements.add(measure2)

    def wait_on_state_paused(timeout):
        assert processor._state.wait(timeout, 'paused')

    measurement = measurement_with_tools
    processor.start_measurement(measurement)

    def assert_active():
        assert processor.active

    exopy_qtbot.wait_until(assert_active)

    pre_hook = measurement.pre_hooks['dummy']

    def assert_wait():
        assert pre_hook.waiting.wait(5)

    exopy_qtbot.wait_until(assert_wait, timeout=50e3)

    processor.pause_measurement()
    pre_hook.accept_pause = False
    pre_hook.go_on.set()

    exopy_qtbot.wait_until(lambda: wait_on_state_paused(0.04), timeout=40e3)

    processor.stop_processing(no_post_exec=True)
    exopy_qtbot.wait(0.2)

    def wait(timeout):
        processor._thread.join(timeout)
        assert not processor._thread.is_alive()

    exopy_qtbot.wait_until(lambda: wait(0.04), timeout=40e3)
    assert measurement.status == 'INTERRUPTED'
    m = processor.plugin.workbench.get_manifest('test.measurement')
    assert not m.find('runtime_dummy1').collected
    assert not m.find('runtime_dummy2').collected

    assert measure2.status == 'READY'
def test_measurement_tracker(measurement_workbench, measurement):
    """Test the measurement tracker.

    """
    plugin = measurement_workbench.get_plugin('exopy.measurement')
    tracker = MeasurementTracker()
    meas1 = measurement
    meas2 = Measurement(plugin=plugin,
                        root_task=RootTask(),
                        name='Dummy',
                        id='002')

    ev1 = Event()
    ev2 = Event()
    ev3 = Event()

    w1 = create_false_widget(meas1, ev1)
    w2 = create_false_widget(meas2, ev2)
    w3 = create_false_widget(None, ev3)

    tracker.start(meas1)

    assert tracker.get_selected_measurement() is meas1

    tracker.enqueue(w2)
    sleep(0.01)
    ev2.set()
    assert tracker.get_selected_measurement() is meas2
    ev2.clear()

    tracker._selected = meas1

    # Test discarding the buffer when new widgets are enqueued while processing
    tracker.enqueue(w3)
    tracker.enqueue(w1)
    while tracker._buffer_empty.is_set():
        sleep(0.01)
    assert not tracker._queue_not_empty.is_set()
    tracker.enqueue(w2)
    ev3.set()
    ev2.set()
    assert tracker.get_selected_measurement() is meas2

    ev2.clear()
    ev3.clear()

    # Test getting the selected when the buffer is empty but not the queue.
    tracker.enqueue(w1)
    sleep(0.01)
    assert not tracker._queue_not_empty.is_set()
    tracker.enqueue(w3)
    ev1.set()
    ev3.set()
    assert tracker.get_selected_measurement() is meas1

    # Test stopping while processing.
    ev3.clear()
    tracker.enqueue(w3)
    sleep(0.01)
    tracker._should_stop.set()
    ev3.set()
    tracker.stop()