def test_get1(pvnames): print('Simple Test: test value and char_value on an integer\n') pv = PV(pvnames.int_pv) val = pv.get() cval = pv.get(as_string=True) assert int(cval) == val
def test_waveform_get_with_count_arg(pvnames): wf = PV(pvnames.char_arr_pv, count=32) val = wf.get() assert len(val) == 32 val = wf.get(count=wf.nelm) assert len(val) == wf.nelm
def test_put_string_waveform(pvnames): print('String Array: \n') with no_simulator_updates(pvnames): pv = PV(pvnames.string_arr_pv) put_value = ['a', 'b', 'c'] pv.put(put_value) get_value = pv.get(use_monitor=False, count=len(put_value)) numpy.testing.assert_array_equal(get_value, put_value)
def test_get_string_waveform(pvnames, simulator): print('String Array: \n') with no_simulator_updates(pvnames): pv = PV(pvnames.string_arr_pv) val = pv.get() assert len(val) > 10 assert isinstance(val[0], str) assert len(val[0]) > 1 assert isinstance(val[1], str) assert len(val[1]) > 1
def testA_CreatedWithConn(pvnames): print('Simple Test: create pv with conn callback\n') CONN_DAT = {} def onConnect(pvname=None, conn=None, chid=None, **kws): nonlocal CONN_DAT print(' :Connection status changed: %s connected=%s\n' % (pvname, conn)) CONN_DAT[pvname] = conn print(f'Connecting to {pvnames.int_pv}') pv = PV(pvnames.int_pv, connection_callback=onConnect) val = pv.get(timeout=5) conn = CONN_DAT.get(pvnames.int_pv, None) assert conn
def make_pvs(*args, **kwds): # print("Make PVS ' ", prefix, args) # print( [("%s%s" % (prefix, name)) for name in args]) pvlist = [PV("%s%s" % (prefix, name)) for name in args] for pv in pvlist: pv.connect() pv.connection_callbacks.append(onConnect) return pvlist
def test_DoubleVal(pvnames, simulator): pvn = pvnames.double_pv pv = PV(pvn) print('pv', pv) value = pv.get() print('pv get', value) assert pv.connected print('%s get value %s' % (pvn, value)) cdict = pv.get_ctrlvars() print('Testing CTRL Values for a Double (%s)\n' % (pvn)) assert 'severity' in cdict assert len(pv.host) > 1 assert pv.count == 1 assert pv.precision == pvnames.double_pv_prec assert pv.units == pvnames.double_pv_units assert pv.access.startswith('read')
def test_putcomplete(pvnames): print('Put with wait and put_complete (using real motor!) \n') vals = (1.35, 1.50, 1.44, 1.445, 1.45, 1.453, 1.446, 1.447, 1.450, 1.450, 1.490, 1.5, 1.500) p = PV(pvnames.motor1) if not p.wait_for_connection(): raise TimeoutError('simulated motor connection failed?') see_complete = [] for v in vals: t0 = time.time() p.put(v, use_complete=True) count = 0 for i in range(100000): time.sleep(0.001) count = count + 1 if p.put_complete: see_complete.append(True) print('See completion') break # print('made it to value= %.3f, elapsed time= %.4f sec (count=%i)' % (v, time.time()-t0, count)) assert len(see_complete) > (len(vals) - 5)
def testEnumPut(pvnames): pv = PV(pvnames.enum_pv) assert pv is not None pv.put('Stop') time.sleep(0.1) val = pv.get() assert val == 0 assert pv.get(as_string=True) == 'Stop'
def test_pv_access_event_callback(access_security_softioc): pvs = access_security_softioc.pvs # clear the run-permit pvs['permit'].put(0, wait=True) assert pvs['permit'].get(as_string=True, use_monitor=False) == 'DISABLED' def lcb(read_access, write_access, pv=None): assert pv.read_access == read_access assert pv.write_access == write_access pv.flag = True bo = PV(pvs['bo'].pvname, access_callback=lcb) bo.flag = False # set the run-permit to trigger an access rights event pvs['permit'].put(1, wait=True) assert pvs['permit'].get(as_string=True, use_monitor=False) == 'ENABLED' # give the callback a bit of time to run time.sleep(0.2) assert bo.flag is True
def test_subarray_1elem(pvnames): # pv = PV(pvnames.zero_len_subarr1) pv = PV(pvnames.double_arr_pv) pv.wait_for_connection() val = pv.get(count=1, use_monitor=False) print('val is', val, type(val)) assert isinstance(val, numpy.ndarray) assert len(val) == 1 val = pv.get(count=1, as_numpy=False, use_monitor=False) print('val is', val, type(val)) assert isinstance(val, list) assert len(val) == 1
def test_subarray_zerolen(pvnames): subarr1 = PV(pvnames.zero_len_subarr1) subarr1.wait_for_connection() val = subarr1.get(use_monitor=True, as_numpy=True) assert isinstance(val, numpy.ndarray), 'using monitor' assert len(val) == 0, 'using monitor' # caproto returns things in big endian, not native type # assert val.dtype == numpy.float64, 'using monitor' val = subarr1.get(use_monitor=False, as_numpy=True) assert isinstance(val, numpy.ndarray), 'no monitor' assert len(val) == 0, 'no monitor'
def test_get_callback(pvnames, simulator): print("Callback test: changing PV must be updated\n") mypv = PV(pvnames.updating_pv1) NEWVALS = [] def onChanges(pvname=None, value=None, char_value=None, **kw): nonlocal NEWVALS print('PV %s %s, %s Changed!\n' % (pvname, repr(value), char_value)) NEWVALS.append(repr(value)) mypv.add_callback(onChanges) print('Added a callback. Now wait for changes...\n') t0 = time.time() while time.time() - t0 < 3: time.sleep(1.e-4) print(' saw %i changes.\n' % len(NEWVALS)) assert len(NEWVALS) > 3 mypv.clear_callbacks()
def test_get_with_metadata(pvnames): with no_simulator_updates(pvnames): pv = PV(pvnames.int_pv, form='native') # Request time type md = pv.get_with_metadata(use_monitor=False, form='time') assert 'timestamp' in md assert 'lower_ctrl_limit' not in md # Request control type md = pv.get_with_metadata(use_monitor=False, form='ctrl') assert 'lower_ctrl_limit' in md assert 'timestamp' not in md # Use monitor: all metadata should come through md = pv.get_with_metadata(use_monitor=True) assert 'timestamp' in md assert 'lower_ctrl_limit' in md # Get a namespace ns = pv.get_with_metadata(use_monitor=True, as_namespace=True) assert hasattr(ns, 'timestamp') assert hasattr(ns, 'lower_ctrl_limit')
def test_waveform_callback_with_count_arg(pvnames, simulator): values = [] wf = PV(pvnames.char_arr_pv, count=32) def onChanges(pvname=None, value=None, char_value=None, **kw): print('PV %s %s, %s Changed!\n' % (pvname, repr(value), char_value)) values.append(value) wf.add_callback(onChanges) print('Added a callback. Now wait for changes...\n') t0 = time.time() while time.time() - t0 < 3: time.sleep(1.e-4) if len(values) > 0: break assert len(values) > 0 assert len(values[0]) == 32 wf.clear_callbacks()
def access_security_softioc(request, prefix, context): 'From pyepics test_cas.py' access_rights_db = { ('{}:ao'.format(prefix), 'ao'): { 'ASG': "rps_threshold", 'DRVH': "10", 'DRVL': "0", }, ('{}:bo'.format(prefix), 'bo'): { 'ASG': "rps_lock", 'ZNAM': "OUT", 'ONAM': "IN", }, ('{}:ao2'.format(prefix), 'ao'): { 'DRVH': "5", 'DRVL': "1", }, ('{}:permit'.format(prefix), 'bo'): { 'VAL': "0", 'PINI': "1", 'ZNAM': "DISABLED", 'ONAM': "ENABLED", }, } access_rights_asg_rules = ''' ASG(DEFAULT) { RULE(1,READ) RULE(1,WRITE,TRAPWRITE) } ASG(rps_threshold) { INPA("$(P):permit") RULE(1, READ) RULE(0, WRITE, TRAPWRITE) { CALC("A=1") } RULE(1, WRITE, TRAPWRITE) { CALC("A=0") } } ASG(rps_lock) { INPA("$(P):permit") RULE(1, READ) RULE(1, WRITE, TRAPWRITE) { CALC("A=0") } } ''' from .conftest import run_softioc, poll_readiness from ..benchmarking.util import has_softioc if not has_softioc(): pytest.skip("no softIoc") handler = run_softioc( request, db=access_rights_db, access_rules_text=access_rights_asg_rules, macros={'P': prefix}, ) PV._default_context = context process = handler.processes[-1] pvs = {pv[len(prefix) + 1:]: PV(pv) for pv, rtype in access_rights_db} pvs['ao.DRVH'] = PV(prefix + ':ao.DRVH') poll_readiness(pvs['ao'].pvname, process=process) for pv in pvs.values(): pv.wait_for_connection() def finalize_context(): print('Cleaning up PV context') broadcaster = PV._default_context.broadcaster broadcaster.disconnect() PV._default_context.disconnect() PV._default_context = None print('Done cleaning up PV context') request.addfinalizer(finalize_context) return SimpleNamespace(process=process, prefix=prefix, name='access_rights', pvs=pvs, type='epics-base')
def test_pyepics_pv(context): pv1 = "sim:mtr1" ctx = context # Some user function to call when subscriptions receive data. called = [] def user_callback(*, value, **kwargs): print() print('-- user callback', value) called.append(True) time_pv = PV(pv1, context=ctx, form='time') ctrl_pv = PV(pv1, context=ctx, form='ctrl') time_pv.wait_for_connection() time_pv.add_callback(user_callback) print('time read', time_pv.get()) print('ctrl read', ctrl_pv.get()) time_pv.put(3, wait=True) time_pv.put(6, wait=True) time.sleep(0.1) assert time_pv.get() == 6 assert called print('read', time_pv.get()) print('done') repr(time_pv) for k, v in PV.__dict__.items(): if isinstance(v, property): getattr(time_pv, k) getattr(ctrl_pv, k)
def test_waveform_get_1elem(pvnames): pv = PV(pvnames.double_arr_pv) val = pv.get(count=1, use_monitor=False) assert isinstance(val, numpy.ndarray) assert len(val) == 1
def test_emptyish_char_waveform_monitor(pvnames): '''a test of a char waveform of length 1 (NORD=1): value "\0" with using auto_monitor ''' zerostr = PV(pvnames.char_arr_pv, auto_monitor=True) zerostr.wait_for_connection() zerostr.put([0], wait=True) time.sleep(0.2) assert zerostr.get(as_string=True) == '' numpy.testing.assert_array_equal(zerostr.get(as_string=False), [0]) assert zerostr.get(as_string=True, as_numpy=False) == '' numpy.testing.assert_array_equal( zerostr.get(as_string=False, as_numpy=False), [0]) zerostr.put([0, 0], wait=True) time.sleep(0.2) assert zerostr.get(as_string=True) == '' numpy.testing.assert_array_equal(zerostr.get(as_string=False), [0, 0]) assert zerostr.get(as_string=True, as_numpy=False) == '' numpy.testing.assert_array_equal( zerostr.get(as_string=False, as_numpy=False), [0, 0]) zerostr.disconnect()
def test_emptyish_char_waveform_no_monitor(pvnames): '''a test of a char waveform of length 1 (NORD=1): value "\0" without using auto_monitor ''' zerostr = PV(pvnames.char_arr_pv, auto_monitor=False) zerostr.wait_for_connection() # elem_count = 128, requested count = None, libca returns count = 1 zerostr.put([0], wait=True) assert zerostr.get(as_string=True) == '' numpy.testing.assert_array_equal(zerostr.get(as_string=False), [0]) assert zerostr.get(as_string=True, as_numpy=False) == '' numpy.testing.assert_array_equal( zerostr.get(as_string=False, as_numpy=False), [0]) # elem_count = 128, requested count = None, libca returns count = 2 zerostr.put([0, 0], wait=True) assert zerostr.get(as_string=True) == '' numpy.testing.assert_array_equal(zerostr.get(as_string=False), [0, 0]) assert zerostr.get(as_string=True, as_numpy=False) == '' numpy.testing.assert_array_equal( zerostr.get(as_string=False, as_numpy=False), [0, 0])
def test_subarrays(pvnames): print("Subarray test: dynamic length arrays\n") driver = PV(pvnames.subarr_driver) subarr1 = PV(pvnames.subarr1) subarr1.connect() len_full = 64 len_sub1 = 16 full_data = numpy.arange(len_full) / 1.0 caput("%s.NELM" % pvnames.subarr1, len_sub1) caput("%s.INDX" % pvnames.subarr1, 0) driver.put(full_data) time.sleep(0.1) subval = subarr1.get() assert len(subval) == len_sub1 assert numpy.all(subval == full_data[:len_sub1]) print("Subarray test: C\n") caput("%s.NELM" % pvnames.subarr2, 19) caput("%s.INDX" % pvnames.subarr2, 3) subarr2 = PV(pvnames.subarr2) subarr2.get() driver.put(full_data) time.sleep(0.1) subval = subarr2.get() assert len(subval) == 19 assert numpy.all(subval == full_data[3:3 + 19]) caput("%s.NELM" % pvnames.subarr2, 5) caput("%s.INDX" % pvnames.subarr2, 13) driver.put(full_data) time.sleep(0.1) subval = subarr2.get() assert len(subval) == 5 assert numpy.all(subval == full_data[13:5 + 13])
def test_putwait(pvnames): print('Put with wait (using real motor!) \n') pv = PV(pvnames.motor1) if not pv.wait_for_connection(): raise TimeoutError('simulated motor connection failed?') val = pv.get() t0 = time.time() if val < 5: pv.put(val + 1.0, wait=True) else: pv.put(val - 1.0, wait=True) dt = time.time() - t0 print(' put took %s sec\n' % dt) assert dt > 0.1 # now with a callback! put_callback_called = False def onPutdone(pvname=None, **kws): print('put done ', pvname, kws) nonlocal put_callback_called put_callback_called = True val = pv.get() if val < 5: pv.put(val + 1.0, callback=onPutdone) else: pv.put(val - 1.0, callback=onPutdone) t0 = time.time() while time.time() - t0 < dt * 1.50: time.sleep(0.02) print(' put should be done by now? %s \n' % put_callback_called) assert put_callback_called # now using pv.put_complete val = pv.get() if val < 5: pv.put(val + 1.0, use_complete=True) else: pv.put(val - 1.0, use_complete=True) t0 = time.time() count = 0 while time.time() - t0 < dt * 1.50: if pv.put_complete: break count = count + 1 time.sleep(0.02) print( ' put_complete=%s (should be True), and count=%i (should be>3)\n' % (pv.put_complete, count)) assert pv.put_complete assert count > 3
def testA_CreatePV(pvnames): print('Simple Test: create pv\n') pv = PV(pvnames.double_pv) assert pv is not None