예제 #1
0
 def trigger(self):
     # There is nothing to do. Just report that we are done.
     # Note: This really should not necessary to do --
     # future changes to PVPositioner may obviate this code.
     status = StatusBase()
     status._finished()
     return status
예제 #2
0
def test_status_legacy_finished_cb():
    st = StatusBase()
    state1, cb1 = _setup_state_and_cb()
    state2, cb2 = _setup_state_and_cb()

    # The old setter works for adding one callback.
    with pytest.warns(UserWarning):
        st.finished_cb = cb1
    # The new getter works.
    st.callbacks == set([cb1])
    # And the old getter works so long as there is just one callback set.
    with pytest.warns(UserWarning):
        assert st.finished_cb is cb1
    # As before, the old setter cannot be updated once set.
    with pytest.raises(UseNewProperty):
        st.finished_cb = cb2
    # But, using the new method, we can add another callback.
    st.add_callback(cb2)
    # Once we have two callbacks, the getter does not work.
    with pytest.raises(UseNewProperty):
        st.finished_cb
    # But the new getter does.
    st.callbacks == set([cb1, cb2])

    assert 'done' not in state1
    assert 'done' not in state2
    st.set_finished()
    st.wait(1)
    time.sleep(0.1)  # Wait for callbacks to run.
    assert 'done' in state1
    assert 'done' in state2
예제 #3
0
def test_status_timeout():
    """
    A StatusTimeoutError is raised when the timeout set in __init__ has
    expired.
    """
    st = StatusBase(timeout=0)
    with pytest.raises(StatusTimeoutError):
        st.wait(1)
    assert isinstance(st.exception(), StatusTimeoutError)
예제 #4
0
    def test_callback(self):
        st = StatusBase()
        cb = Mock()

        st.finished_cb = cb
        self.assertIs(st.finished_cb, cb)
        self.assertRaises(RuntimeError, setattr, st, 'finished_cb', None)

        st._finished()
        cb.assert_called_once_with()
예제 #5
0
    def test_callback(self):
        st = StatusBase()
        cb = Mock()

        st.finished_cb = cb
        self.assertIs(st.finished_cb, cb)
        self.assertRaises(RuntimeError, setattr, st, 'finished_cb', None)

        st._finished()
        cb.assert_called_once_with()
예제 #6
0
def test_status_callback():
    st = StatusBase()
    cb = Mock()

    st.finished_cb = cb
    assert st.finished_cb is cb
    with pytest.raises(RuntimeError):
        st.finished_cb = None

    st._finished()
    cb.assert_called_once_with()
예제 #7
0
def test_old_signature_on_finished_status():
    st = StatusBase()
    state, cb = _setup_state_and_cb(new_signature=False)
    st.set_finished()
    st.wait(1)
    with pytest.warns(DeprecationWarning, match="signature"):
        st.add_callback(cb)
    assert state
예제 #8
0
def test_set_exception_special_banned_exceptions():
    """
    Exceptions with special significant to StatusBase are banned. See comments
    in set_exception.
    """
    st = StatusBase()
    # Test the class and the instance of each.
    with pytest.raises(ValueError):
        st.set_exception(StatusTimeoutError)
    with pytest.raises(ValueError):
        st.set_exception(StatusTimeoutError())
    with pytest.raises(ValueError):
        st.set_exception(WaitTimeoutError)
    with pytest.raises(ValueError):
        st.set_exception(WaitTimeoutError())
    def trigger(self):
        ''' custom trigger for Eiger Manual'''
        if self._staged != Staged.yes:
            raise RuntimeError("This detector is not ready to trigger."
                               "Call the stage() method before triggering.")

        if self._set_st is not None:
            raise RuntimeError(f'trying to set {self.name}'
                               ' while a set is in progress')

        st = StatusBase()
        # idea : look at the array counter and the trigger value
        # the logic here is that I want to check when the status is back to zero
        def counter_cb(value, timestamp, **kwargs):
            # whenevr it counts just move on
            #print("changed : {}".format(value))
            self._set_st = None
            self.cam.array_counter.clear_sub(counter_cb)
            st._finished()


        # first subscribe a callback
        self.cam.array_counter.subscribe(counter_cb, run=False)

        # then call trigger on the PV
        self.special_trigger_button.put(1, wait=False)
        self.dispatch(self._image_name, ttime.time())
        self.file.frame_num += 1

        return st
예제 #10
0
def test_direct_done_setting():
    st = StatusBase()
    state, cb = _setup_state_and_cb()

    with pytest.raises(RuntimeError):
        st.done = True  # changing isn't allowed
    with pytest.warns(UserWarning):
        st.done = False  # but for now no-ops warn

    st.set_finished()
    st.wait(1)
    time.sleep(0.1)  # Wait for callbacks to run.

    with pytest.raises(RuntimeError):
        st.done = False  # changing isn't allowed
    with pytest.warns(UserWarning):
        st.done = True  # but for now no-ops warn
예제 #11
0
def test_old_signature():
    st = StatusBase()
    state, cb = _setup_state_and_cb(new_signature=False)
    with pytest.warns(DeprecationWarning, match="signature"):
        st.add_callback(cb)
    assert not state
    st.set_finished()
    st.wait(1)
    time.sleep(0.1)  # Wait for callbacks to run.
    assert state
예제 #12
0
파일: test_status.py 프로젝트: klauer/ophyd
def _setup_st():
    st = StatusBase()
    state = {}

    def cb():
        state['done'] = True

    repr(st)
    return st, state, cb
예제 #13
0
def test_old_finished_method_success():
    st = StatusBase()
    state, cb = _setup_state_and_cb()
    st.add_callback(cb)
    assert not state
    st._finished()
    st.wait(1)
    time.sleep(0.1)  # Wait for callbacks to run.
    assert state
    assert st.done
    assert st.success
예제 #14
0
def test_old_finished_method_failure():
    st = StatusBase()
    state, cb = _setup_state_and_cb()
    st.add_callback(cb)
    assert not state
    st._finished(success=False)
    with pytest.raises(UnknownStatusFailure):
        st.wait(1)
    time.sleep(0.1)  # Wait for callbacks to run.
    assert state
    assert st.done
    assert not st.success
예제 #15
0
def test_status_pre():
    st = StatusBase()
    state, cb = _setup_state_and_cb()

    st.set_finished()
    st.wait(1)
    time.sleep(0.1)  # Wait for callbacks to run.

    assert 'done' not in state
    st.add_callback(cb)
    assert 'done' in state
    assert state['done']
예제 #16
0
def test_status_callback():
    "The new way, with add_callback and the callbacks property"
    st = StatusBase()
    cb = Mock()

    st.add_callback(cb)
    assert st.callbacks[0] is cb

    st.set_finished()
    st.wait(1)
    time.sleep(0.1)  # Wait for callbacks to run.
    cb.assert_called_once_with(st)
예제 #17
0
def test_race_settle_time_and_timeout():
    """
    A StatusTimeoutError should NOT occur here because that is only invoked
    after (timeout + settle_time) has elapsed.
    """
    st = StatusBase(timeout=1, settle_time=3)
    st.set_finished()  # starts a threading.Timer with the settle_time
    time.sleep(1.5)
    # We should still be settling....
    with pytest.raises(WaitTimeoutError):
        st.wait(1)
    # Now we should be done successfully.
    st.wait(3)
예제 #18
0
def test_exception_fail_path():
    st = StatusBase()

    class LocalException(Exception):
        ...

    exc = LocalException()
    st.set_exception(exc)
    assert exc is st.exception()
    with pytest.raises(LocalException):
        st.wait(1)
예제 #19
0
def test_direct_done_setting():
    st = StatusBase()
    state, cb = _setup_state_and_cb()

    with pytest.raises(RuntimeError):
        st.done = True  # changing isn't allowed
    with pytest.warns(UserWarning):
        st.done = False  # but for now no-ops warn

    st._finished()

    with pytest.raises(RuntimeError):
        st.done = False  # changing isn't allowed
    with pytest.warns(UserWarning):
        st.done = True  # but for now no-ops warn
예제 #20
0
def test_status_callback_deprecated():
    "The old way, with finished_cb"
    st = StatusBase()
    cb = Mock()

    with pytest.warns(UserWarning):
        st.finished_cb = cb
    with pytest.warns(UserWarning):
        assert st.finished_cb is cb
    with pytest.raises(RuntimeError):
        st.finished_cb = None

    st.set_finished()
    st.wait(1)
    time.sleep(0.1)  # Wait for callbacks to run.
    cb.assert_called_once_with(st)
예제 #21
0
def test_exception_fail_path_with_class():
    """
    Python allows `raise Exception` and `raise Exception()` so we do as well.
    """
    st = StatusBase()

    class LocalException(Exception):
        ...

    st.set_exception(LocalException)
    assert LocalException is st.exception()
    with pytest.raises(LocalException):
        st.wait(1)
예제 #22
0
def test_status_callback():
    "The new way, with add_callback and the callbacks property"
    st = StatusBase()
    cb = Mock()

    st.add_callback(cb)
    assert st.callbacks[0] is cb

    st._finished()
    cb.assert_called_once_with()
예제 #23
0
def test_wait_timeout():
    """
    A WaitTimeoutError is raised when we block on wait(TIMEOUT) or
    exception(TIMEOUT) and the Status has not finished.
    """
    st = StatusBase()
    with pytest.raises(WaitTimeoutError):
        st.wait(0.01)
    with pytest.raises(WaitTimeoutError):
        st.exception(0.01)
예제 #24
0
def test_status_pre():
    st = StatusBase()
    state, cb = _setup_state_and_cb()

    st._finished()

    assert 'done' not in state
    st.add_callback(cb)
    assert 'done' in state
    assert state['done']
예제 #25
0
def test_set_finished_after_timeout():
    """
    If an external callback (e.g. pyepics) calls set_finished after the status
    has timed out, ignore it.
    """
    st = StatusBase(timeout=0)
    time.sleep(0.1)
    assert isinstance(st.exception(), StatusTimeoutError)
    # External callback fires, too late.
    st.set_finished()
    assert isinstance(st.exception(), StatusTimeoutError)
예제 #26
0
def test_external_timeout():
    """
    A TimeoutError is raised, not StatusTimeoutError or WaitTimeoutError,
    when set_exception(TimeoutError) has been set.
    """
    st = StatusBase(timeout=1)
    st.set_exception(TimeoutError())
    with pytest.raises(TimeoutError) as exc:
        st.wait(1)
    assert not isinstance(exc, WaitTimeoutError)
    assert not isinstance(exc, StatusTimeoutError)
예제 #27
0
def test_status_callback():
    st = StatusBase()
    cb = Mock()

    st.finished_cb = cb
    assert st.finished_cb is cb
    with pytest.raises(RuntimeError):
        st.finished_cb = None

    st._finished()
    cb.assert_called_once_with()
예제 #28
0
def test_status_timeout_with_settle_time():
    """
    A StatusTimeoutError is raised when the timeout set in __init__ plus the
    settle_time has expired.
    """
    st = StatusBase(timeout=0, settle_time=1)
    # Not dead yet.
    with pytest.raises(WaitTimeoutError):
        st.exception(0.01)
    # But now we are.
    with pytest.raises(StatusTimeoutError):
        st.wait(2)
예제 #29
0
def test_deprecated_init():
    with pytest.warns(DeprecationWarning, match="set_finished"):
        StatusBase(success=True, done=True)
    with pytest.warns(DeprecationWarning, match="set_finished"):
        StatusBase(success=False, done=True)
    with pytest.warns(DeprecationWarning, match="set_finished"):
        StatusBase(success=False, done=False)
    with pytest.warns(DeprecationWarning, match="set_finished"):
        StatusBase(success=False, done=None)
    with pytest.warns(DeprecationWarning, match="set_finished"):
        StatusBase(success=None, done=True)
    with pytest.warns(DeprecationWarning, match="set_finished"):
        StatusBase(success=None, done=False)
예제 #30
0
def test_status_callback_deprecated():
    "The old way, with finished_cb"
    st = StatusBase()
    cb = Mock()

    with pytest.warns(UserWarning):
        st.finished_cb = cb
    with pytest.warns(UserWarning):
        assert st.finished_cb is cb
    with pytest.raises(RuntimeError):
        st.finished_cb = None

    st._finished()
    cb.assert_called_once_with()
예제 #31
0
def test_set_exception_after_timeout():
    """
    If an external callback (e.g. pyepics) calls set_exception after the status
    has timed out, ignore it.
    """
    st = StatusBase(timeout=0)
    time.sleep(0.1)
    assert isinstance(st.exception(), StatusTimeoutError)

    class LocalException(Exception):
        ...

    # External callback reports failure, too late.
    st.set_exception(LocalException())
    assert isinstance(st.exception(), StatusTimeoutError)
예제 #32
0
 def test_basic(self):
     st = StatusBase()
     st._finished()
예제 #33
0
def test_status_wait():
    st = StatusBase()
    st._finished()
    wait(st)
예제 #34
0
 def test_wait(self):
     st = StatusBase()
     st._finished()
     wait(st)
예제 #35
0
def test_and():
    st1 = StatusBase()
    st2 = StatusBase()
    st3 = st1 & st2
    # make sure deep recursion works
    st4 = st1 & st3
    st5 = st3 & st4
    state1, cb1 = _setup_state_and_cb()
    state2, cb2 = _setup_state_and_cb()
    state3, cb3 = _setup_state_and_cb()
    state4, cb4 = _setup_state_and_cb()
    state5, cb5 = _setup_state_and_cb()
    st1.add_callback(cb1)
    st2.add_callback(cb2)
    st3.add_callback(cb3)
    st4.add_callback(cb4)
    st5.add_callback(cb5)
    st1._finished()
    assert 'done' in state1
    assert 'done' not in state2
    assert 'done' not in state3
    assert 'done' not in state4
    assert 'done' not in state5
    st2._finished()
    assert 'done' in state3
    assert 'done' in state4
    assert 'done' in state5
    assert st3.left is st1
    assert st3.right is st2
    assert st4.left is st1
    assert st4.right is st3
    assert st5.left is st3
    assert st5.right is st4
예제 #36
0
def test_status_basic():
    st = StatusBase()
    st._finished()