Example #1
0
def test_interface_basic():
    one = Signal(name='one', kind=Kind.config)
    two = Signal(name='two')

    assert one.kind == Kind.config
    two_sig = TwoSignal('prefix', name='name', sig_one=one, sig_two=two)
    assert one.kind == Kind.hinted
    two_sig.sig_one.put(1)
    two_sig.sig_two.put(2)

    assert one.get() == 1
    assert two.get() == 2
Example #2
0
def test_to_interface():
    SomeDeviceInterface = to_interface(SomeDevice)
    some = Signal(name='some')
    where = Signal(name='where')

    sdi = SomeDeviceInterface('', name='sdi', some=some, where=where)
    sd = SomeDevice('', name='sd')

    sdi.some.put(1)
    sdi.where.put(2)
    sd.some.put(3)
    sd.where.put(4)

    assert some.get() == 1
    assert where.get() == 2
Example #3
0
def test_soft_derived():
    timestamp = 1.0
    value = 'q'
    original = Signal(name='original', timestamp=timestamp, value=value)

    cb_values = []

    def callback(value=None, **kwargs):
        nonlocal cb_values
        cb_values.append(value)

    derived = DerivedSignal(derived_from=original, name='derived')
    derived.subscribe(callback, event_type=derived.SUB_VALUE)

    assert derived.timestamp == timestamp
    assert derived.get() == value
    assert derived.timestamp == timestamp
    assert derived.describe()[derived.name]['derived_from'] == original.name

    new_value = 'r'
    derived.put(new_value)
    assert original.get() == new_value
    assert derived.get() == new_value
    assert derived.timestamp == original.timestamp
    assert derived.limits == original.limits

    copied = copy.copy(derived)
    assert copied.derived_from.value == original.value
    assert copied.derived_from.timestamp == original.timestamp
    assert copied.derived_from.name == original.name

    derived.put('s')
    assert cb_values == ['r', 's']
Example #4
0
def test_soft_derived():
    timestamp = 1.0
    value = 'q'
    original = Signal(name='original', timestamp=timestamp, value=value)

    cb_values = []

    def callback(value=None, **kwargs):
        nonlocal cb_values
        cb_values.append(value)

    derived = DerivedSignal(derived_from=original, name='derived')
    derived.subscribe(callback, event_type=derived.SUB_VALUE)

    assert derived.timestamp == timestamp
    assert derived.get() == value
    assert derived.timestamp == timestamp
    assert derived.describe()[derived.name]['derived_from'] == original.name

    new_value = 'r'
    derived.put(new_value)
    assert original.get() == new_value
    assert derived.get() == new_value
    assert derived.timestamp == original.timestamp
    assert derived.limits == original.limits

    copied = copy.copy(derived)
    assert copied.derived_from.value == original.value
    assert copied.derived_from.timestamp == original.timestamp
    assert copied.derived_from.name == original.name

    derived.put('s')
    assert cb_values == ['r', 's']
Example #5
0
def test_ocpt():
    sig_one = Signal(name='one', kind=Kind.config)
    sig_two = Signal(name='two')

    class ObjectDevice(Device):
        one = OCpt(sig_one, kind=Kind.hinted)
        two = OCpt(sig_two)

    assert sig_one.kind == Kind.config
    obj = ObjectDevice('prefix', name='name')
    assert 'one' in obj.component_names
    assert obj.one.kind == Kind.hinted
    obj.one.put(5)
    assert sig_one.get() == 5
    obj.two.put(3)
    assert sig_two.get() == 3
Example #6
0
def test_set_method():
    sig = Signal(name='sig')

    st = sig.set(28)
    wait(st)
    assert st.done
    assert st.success
    assert sig.get() == 28
Example #7
0
def test_set_method():
    sig = Signal(name='sig')

    st = sig.set(28)
    wait(st)
    assert st.done
    assert st.success
    assert sig.get() == 28
Example #8
0
def test_ndderivedsignal_with_scalars():
    sig = Signal(value=np.zeros(12), name='zeros')
    shaped = NDDerivedSignal(sig, shape=(4, 3), num_dimensions=2, name='shaped')
    shaped.derived_shape == (4, 3)
    shaped.derived_ndims == 2
    assert shaped.get().shape == (4, 3)
    # Describe returns list
    assert shaped.describe()[shaped.name]['shape'] == [4, 3]
    shaped.put(np.ones((4, 3)))
    assert all(sig.get() == np.ones(12))
Example #9
0
def test_ndderivedsignal_with_scalars():
    sig = Signal(value=np.zeros(12), name='zeros')
    shaped = NDDerivedSignal(sig, shape=(4, 3), num_dimensions=2, name='shaped')
    shaped.derived_shape == (4, 3)
    shaped.derived_ndims == 2
    assert shaped.get().shape == (4, 3)
    # Describe returns list
    assert shaped.describe()[shaped.name]['shape'] == [4, 3]
    shaped.put(np.ones((4, 3)))
    assert all(sig.get() == np.ones(12))
Example #10
0
    def test_signal_copy(self):
        start_t = time.time()

        name = 'test'
        value = 10.0
        signal = Signal(name=name, value=value, timestamp=start_t)
        sig_copy = copy.copy(signal)

        self.assertEquals(signal.name, sig_copy.name)
        self.assertEquals(signal.value, sig_copy.value)
        self.assertEquals(signal.get(), sig_copy.get())
        self.assertEquals(signal.timestamp, sig_copy.timestamp)
Example #11
0
    def test_signal_copy(self):
        start_t = time.time()

        name = 'test'
        value = 10.0
        signal = Signal(name=name, value=value, timestamp=start_t)
        sig_copy = copy.copy(signal)

        self.assertEquals(signal.name, sig_copy.name)
        self.assertEquals(signal.value, sig_copy.value)
        self.assertEquals(signal.get(), sig_copy.get())
        self.assertEquals(signal.timestamp, sig_copy.timestamp)
Example #12
0
def test_signal_copy():
    start_t = time.time()

    name = 'test'
    value = 10.0
    signal = Signal(name=name, value=value, timestamp=start_t)
    sig_copy = copy.copy(signal)

    assert signal.name == sig_copy.name
    assert signal.value == sig_copy.value
    assert signal.get() == sig_copy.get()
    assert signal.timestamp == sig_copy.timestamp
Example #13
0
def test_signal_copy():
    start_t = time.time()

    name = 'test'
    value = 10.0
    signal = Signal(name=name, value=value, timestamp=start_t)
    sig_copy = copy.copy(signal)

    assert signal.name == sig_copy.name
    assert signal.value == sig_copy.value
    assert signal.get() == sig_copy.get()
    assert signal.timestamp == sig_copy.timestamp
Example #14
0
def test_soft_derived():
    timestamp = 1.0
    value = 'q'
    original = Signal(name='original', timestamp=timestamp, value=value)

    cb_values = []

    def callback(value=None, **kwargs):
        cb_values.append(value)

    derived = DerivedSignal(derived_from=original, name='derived')
    derived.subscribe(callback, event_type=derived.SUB_VALUE)

    assert derived.timestamp == timestamp
    assert derived.get() == value
    assert derived.timestamp == timestamp
    assert derived.describe()[derived.name]['derived_from'] == original.name
    assert derived.write_access == original.write_access
    assert derived.read_access == original.read_access

    new_value = 'r'
    derived.put(new_value)
    assert original.get() == new_value
    assert derived.get() == new_value
    assert derived.timestamp == original.timestamp
    assert derived.limits == original.limits

    copied = copy.copy(derived)
    with pytest.warns(UserWarning):
        assert copied.derived_from.value == original.value
    assert copied.derived_from.timestamp == original.timestamp
    assert copied.derived_from.name == original.name

    derived.put('s')
    assert cb_values == ['r', 's']

    called = []

    event = threading.Event()

    def meta_callback(*, connected, read_access, write_access, **kw):
        called.append(('meta', connected, read_access, write_access))
        event.set()

    derived.subscribe(meta_callback, event_type=derived.SUB_META, run=False)

    original._metadata['write_access'] = False
    original._run_subs(sub_type='meta', **original._metadata)

    event.wait(1)

    assert called == [('meta', True, True, False)]
Example #15
0
def test_soft_derived():
    timestamp = 1.0
    value = 'q'
    original = Signal(name='original', timestamp=timestamp, value=value)

    cb_values = []

    def callback(value=None, **kwargs):
        cb_values.append(value)

    derived = DerivedSignal(derived_from=original, name='derived')
    derived.subscribe(callback, event_type=derived.SUB_VALUE)

    assert derived.timestamp == timestamp
    assert derived.get() == value
    assert derived.timestamp == timestamp
    assert derived.describe()[derived.name]['derived_from'] == original.name
    assert derived.write_access == original.write_access
    assert derived.read_access == original.read_access

    new_value = 'r'
    derived.put(new_value)
    assert original.get() == new_value
    assert derived.get() == new_value
    assert derived.timestamp == original.timestamp
    assert derived.limits == original.limits

    copied = copy.copy(derived)
    assert copied.derived_from.value == original.value
    assert copied.derived_from.timestamp == original.timestamp
    assert copied.derived_from.name == original.name

    derived.put('s')
    assert cb_values == ['r', 's']

    called = []

    event = threading.Event()

    def meta_callback(*, connected, read_access, write_access, **kw):
        called.append(('meta', connected, read_access, write_access))
        event.set()

    derived.subscribe(meta_callback, event_type=derived.SUB_META, run=False)

    original._metadata['write_access'] = False
    original._run_subs(sub_type='meta', **original._metadata)

    event.wait(1)

    assert called == [('meta', True, True, False)]
Example #16
0
    def test_soft_derived(self):
        timestamp = 1.0
        value = 'q'
        original = Signal(name='original', timestamp=timestamp, value=value)

        derived = DerivedSignal(derived_from=original, name='derived')
        self.assertEqual(derived.timestamp, timestamp)
        self.assertEqual(derived.get(), value)
        self.assertEqual(derived.timestamp, timestamp)
        self.assertEqual(derived.describe()[derived.name]['derived_from'],
                         original.name)

        new_value = 'r'
        derived.put(new_value)
        self.assertEqual(original.get(), new_value)
        self.assertEqual(derived.get(), new_value)
        self.assertEqual(derived.timestamp, original.timestamp)
        self.assertEqual(derived.limits, original.limits)

        copied = copy.copy(derived)
        self.assertEqual(copied.derived_from.value, original.value)
        self.assertEqual(copied.derived_from.timestamp, original.timestamp)
        self.assertEqual(copied.derived_from.name, original.name)
Example #17
0
    def test_soft_derived(self):
        timestamp = 1.0
        value = 'q'
        original = Signal(name='original', timestamp=timestamp, value=value)

        derived = DerivedSignal(derived_from=original, name='derived')
        self.assertEqual(derived.timestamp, timestamp)
        self.assertEqual(derived.get(), value)
        self.assertEqual(derived.timestamp, timestamp)
        self.assertEqual(derived.describe()[derived.name]['derived_from'],
                         original.name)

        new_value = 'r'
        derived.put(new_value)
        self.assertEqual(original.get(), new_value)
        self.assertEqual(derived.get(), new_value)
        self.assertEqual(derived.timestamp, original.timestamp)
        self.assertEqual(derived.limits, original.limits)

        copied = copy.copy(derived)
        self.assertEqual(copied.derived_from.value, original.value)
        self.assertEqual(copied.derived_from.timestamp, original.timestamp)
        self.assertEqual(copied.derived_from.name, original.name)
Example #18
0
    def test_signal_base(self):
        start_t = time.time()

        name = 'test'
        value = 10.0
        signal = Signal(name=name, value=value, timestamp=start_t)
        signal.wait_for_connection()

        self.assertTrue(signal.connected)
        self.assertEquals(signal.name, name)
        self.assertEquals(signal.value, value)
        self.assertEquals(signal.get(), value)
        self.assertEquals(signal.timestamp, start_t)

        info = dict(called=False)

        def _sub_test(**kwargs):
            info['called'] = True
            info['kw'] = kwargs

        signal.subscribe(_sub_test, run=False)
        self.assertFalse(info['called'])

        signal.value = value
        signal.clear_sub(_sub_test)

        signal.subscribe(_sub_test, run=False)
        signal.clear_sub(_sub_test, event_type=signal.SUB_VALUE)

        kw = info['kw']
        self.assertIn('value', kw)
        self.assertIn('timestamp', kw)
        self.assertIn('old_value', kw)

        self.assertEquals(kw['value'], value)
        self.assertEquals(kw['old_value'], value)
        self.assertEquals(kw['timestamp'], signal.timestamp)

        # readback callback for soft signal
        info = dict(called=False)
        signal.subscribe(_sub_test, event_type=Signal.SUB_VALUE,
                         run=False)
        self.assertFalse(info['called'])
        signal.put(value + 1)
        self.assertTrue(info['called'])

        signal.clear_sub(_sub_test)
        kw = info['kw']

        self.assertIn('value', kw)
        self.assertIn('timestamp', kw)
        self.assertIn('old_value', kw)

        self.assertEquals(kw['value'], value + 1)
        self.assertEquals(kw['old_value'], value)
        self.assertEquals(kw['timestamp'], signal.timestamp)

        signal.trigger()
        signal.read()
        signal.describe()
        signal.read_configuration()
        signal.describe_configuration()

        eval(repr(signal))
Example #19
0
def test_signal_base():
    start_t = time.time()

    name = 'test'
    value = 10.0
    signal = Signal(name=name, value=value, timestamp=start_t)
    signal.wait_for_connection()

    assert signal.connected
    assert signal.name == name
    assert signal.value == value
    assert signal.get() == value
    assert signal.timestamp == start_t

    info = dict(called=False)

    def _sub_test(**kwargs):
        info['called'] = True
        info['kw'] = kwargs

    signal.subscribe(_sub_test, run=False,
                     event_type=signal.SUB_VALUE)
    assert not info['called']

    signal.value = value
    signal.clear_sub(_sub_test)

    signal.subscribe(_sub_test, run=False,
                     event_type=signal.SUB_VALUE)
    signal.clear_sub(_sub_test, event_type=signal.SUB_VALUE)

    kw = info['kw']
    assert 'value' in kw
    assert 'timestamp' in kw
    assert 'old_value' in kw

    assert kw['value'] == value
    assert kw['old_value'] == value
    assert kw['timestamp'] == signal.timestamp

    # readback callback for soft signal
    info = dict(called=False)
    signal.subscribe(_sub_test, event_type=Signal.SUB_VALUE,
                     run=False)
    assert not info['called']
    signal.put(value + 1)
    assert info['called']

    signal.clear_sub(_sub_test)
    kw = info['kw']

    assert 'value' in kw
    assert 'timestamp' in kw
    assert 'old_value' in kw

    assert kw['value'] == value + 1
    assert kw['old_value'] == value
    assert kw['timestamp'] == signal.timestamp

    signal.trigger()
    signal.read()
    signal.describe()
    signal.read_configuration()
    signal.describe_configuration()

    eval(repr(signal))
Example #20
0
    def test_signal_base(self):
        start_t = time.time()

        name = 'test'
        value = 10.0
        signal = Signal(name=name, value=value, timestamp=start_t)
        signal.wait_for_connection()

        self.assertTrue(signal.connected)
        self.assertEquals(signal.name, name)
        self.assertEquals(signal.value, value)
        self.assertEquals(signal.get(), value)
        self.assertEquals(signal.timestamp, start_t)

        info = dict(called=False)

        def _sub_test(**kwargs):
            info['called'] = True
            info['kw'] = kwargs

        signal.subscribe(_sub_test, run=False)
        self.assertFalse(info['called'])

        signal.value = value
        signal.clear_sub(_sub_test)

        signal.subscribe(_sub_test, run=False)
        signal.clear_sub(_sub_test, event_type=signal.SUB_VALUE)

        kw = info['kw']
        self.assertIn('value', kw)
        self.assertIn('timestamp', kw)
        self.assertIn('old_value', kw)

        self.assertEquals(kw['value'], value)
        self.assertEquals(kw['old_value'], value)
        self.assertEquals(kw['timestamp'], signal.timestamp)

        # readback callback for soft signal
        info = dict(called=False)
        signal.subscribe(_sub_test, event_type=Signal.SUB_VALUE, run=False)
        self.assertFalse(info['called'])
        signal.put(value + 1)
        self.assertTrue(info['called'])

        signal.clear_sub(_sub_test)
        kw = info['kw']

        self.assertIn('value', kw)
        self.assertIn('timestamp', kw)
        self.assertIn('old_value', kw)

        self.assertEquals(kw['value'], value + 1)
        self.assertEquals(kw['old_value'], value)
        self.assertEquals(kw['timestamp'], signal.timestamp)

        signal.trigger()
        signal.read()
        signal.describe()
        signal.read_configuration()
        signal.describe_configuration()

        eval(repr(signal))
Example #21
0
def test_signal_base():
    start_t = time.time()

    name = 'test'
    value = 10.0
    signal = Signal(name=name, value=value, timestamp=start_t)
    signal.wait_for_connection()

    assert signal.connected
    assert signal.name == name
    assert signal.value == value
    assert signal.get() == value
    assert signal.timestamp == start_t

    info = dict(called=False)

    def _sub_test(**kwargs):
        info['called'] = True
        info['kw'] = kwargs

    signal.subscribe(_sub_test, run=False,
                     event_type=signal.SUB_VALUE)
    assert not info['called']

    signal.value = value
    signal.clear_sub(_sub_test)

    signal.subscribe(_sub_test, run=False,
                     event_type=signal.SUB_VALUE)
    signal.clear_sub(_sub_test, event_type=signal.SUB_VALUE)

    kw = info['kw']
    assert 'value' in kw
    assert 'timestamp' in kw
    assert 'old_value' in kw

    assert kw['value'] == value
    assert kw['old_value'] == value
    assert kw['timestamp'] == signal.timestamp

    # readback callback for soft signal
    info = dict(called=False)
    signal.subscribe(_sub_test, event_type=Signal.SUB_VALUE,
                     run=False)
    assert not info['called']
    signal.put(value + 1)
    assert info['called']

    signal.clear_sub(_sub_test)
    kw = info['kw']

    assert 'value' in kw
    assert 'timestamp' in kw
    assert 'old_value' in kw

    assert kw['value'] == value + 1
    assert kw['old_value'] == value
    assert kw['timestamp'] == signal.timestamp

    signal.trigger()
    signal.read()
    signal.describe()
    signal.read_configuration()
    signal.describe_configuration()

    eval(repr(signal))