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 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_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_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 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_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