Skip to content

Commit

Permalink
test: use wait_until() instead of sleep()
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphlange committed Dec 15, 2021
1 parent 2bede6f commit 0ce28c9
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 23 deletions.
18 changes: 6 additions & 12 deletions testTop/pyTestsApp/TestCSStudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
import sys
import os
import unittest
import epics
from epics import ca, dbr
import IOCControl
import GatewayControl
import gwtests
import time
import subprocess

class TestCSStudio(unittest.TestCase):
'''Test CS-Studio workflow through the Gateway
Expand Down Expand Up @@ -65,8 +62,6 @@ def testCSStudio_ValueAndPropMonitor(self):
'''Monitor PV (imitating CS-Studio) through GW - change value and properties directly - check CTRL structure consistency'''
diffs = []

if gwtests.verbose:
print()
# gwcachetest is an ai record with full set of alarm limits: -100 -10 10 100
gw = ca.create_channel("gateway:gwcachetest")
connected = ca.connect_channel(gw, timeout=.5)
Expand All @@ -79,15 +74,15 @@ def testCSStudio_ValueAndPropMonitor(self):
(ioc_cbref, ioc_uaref, ioc_eventid) = ca.create_subscription(ioc, mask=dbr.DBE_VALUE | dbr.DBE_ALARM, use_time=True, callback=self.onChangeIOC)
(ioc_cbref2, ioc_uaref2, ioc_eventid2) = ca.create_subscription(ioc, mask=dbr.DBE_PROPERTY, use_ctrl=True, callback=self.onChangeIOC)

time.sleep(.1)
gwtests.wait_until(lambda: self.eventsReceivedIOC == 2 and self.eventsReceivedGW == 2, 5.0)

# set value on IOC
ioc_value = ca.create_channel("ioc:gwcachetest")
ca.put(ioc_value, 10.0, wait=True)
time.sleep(.1)
if gwtests.verbose:
print()
print("Wrote value 10.0 to ioc:gwcachetest")

gwtests.wait_until(lambda: self.eventsReceivedIOC == self.eventsReceivedGW, 5.0)
self.assertTrue(self.eventsReceivedIOC == self.eventsReceivedGW,
"After setting value, no. of received updates differ: GW {0}, IOC {1}"
.format(str(self.eventsReceivedGW), str(self.eventsReceivedIOC)))
Expand All @@ -100,14 +95,13 @@ def testCSStudio_ValueAndPropMonitor(self):
# set property on IOC
ioc_hihi = ca.create_channel("ioc:gwcachetest.HIHI")
ca.put(ioc_hihi, 123.0, wait=True)
time.sleep(.1)
if gwtests.verbose:
print()
print("Wrote value 123.0 to ioc:gwcachetest.HIHI")
ca.put(ioc_value, 11.0, wait=True)
time.sleep(.1)
if gwtests.verbose:
print()
print("Wrote value 11.0 to ioc:gwcachetest")

gwtests.wait_until(lambda: self.eventsReceivedIOC == self.eventsReceivedGW, 5.0)
self.assertTrue(self.eventsReceivedIOC == self.eventsReceivedGW,
"After setting property, no. of received updates differ: GW {0}, IOC {1}"
.format(str(self.eventsReceivedGW), str(self.eventsReceivedIOC)))
Expand Down
2 changes: 1 addition & 1 deletion testTop/pyTestsApp/TestDBEAlarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def testAlarmLevel(self):
gw.get()
for val in [0,1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1,0]:
ioc.put(val, wait=True)
time.sleep(.1)
gwtests.wait_until(lambda: self.eventsReceived == 6, 5.0)
# We get 6 events: at connection (INVALID), at first write (NO_ALARM),
# and at the level crossings MINOR-MAJOR-MINOR-NO_ALARM.
self.assertTrue(self.eventsReceived == 6, 'events expected: 6; events received: ' + str(self.eventsReceived))
Expand Down
2 changes: 1 addition & 1 deletion testTop/pyTestsApp/TestDBELog.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def testLogDeadband(self):
gw.get()
for val in range(35):
ioc.put(val, wait=True)
time.sleep(.1)
gwtests.wait_until(lambda: self.eventsReceived == 5, 5.0)
# We get 5 events: at connection, first put, then at 11 22 33
self.assertTrue(self.eventsReceived == 5, 'events expected: 5; events received: ' + str(self.eventsReceived))
# Any updates inside deadband are an error
Expand Down
2 changes: 1 addition & 1 deletion testTop/pyTestsApp/TestDBEProp.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def testPropAlarmLevels(self):

for val in range(10):
ioc.put(val, wait=True)
time.sleep(.1)
gwtests.wait_until(lambda: self.eventsReceivedGW == 1 and self.eventsReceivedIOC == 1, 5.0)
# We get 1 event: at connection
self.assertTrue(self.eventsReceivedGW == 1, 'GW events expected: 1; received: ' + str(self.eventsReceivedGW))
self.assertTrue(self.eventsReceivedIOC == 1, 'IOC events expected: 1; received: ' + str(self.eventsReceivedIOC))
Expand Down
2 changes: 1 addition & 1 deletion testTop/pyTestsApp/TestDBEValue.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def testValueNoDeadband(self):

for val in range(10):
ioc.put(val, wait=True)
time.sleep(.1)

gwtests.wait_until(lambda: self.eventsReceived == 11, 5.0)
# We get 11 events: at connection, then at 10 value changes (puts)
self.assertTrue(self.eventsReceived == 11, 'events expected: 11; events received: ' + str(self.eventsReceived))

Expand Down
13 changes: 8 additions & 5 deletions testTop/pyTestsApp/TestEnumPropertyCache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ class TestEnumPropertyCache(unittest.TestCase):

def connectGwStats(self):
self.gw_vctotal = ca.create_channel("gwtest:vctotal")
ca.connect_channel(self.gw_vctotal, timeout=.5)
self.gw_pvtotal = ca.create_channel("gwtest:pvtotal")
ca.connect_channel(self.gw_pvtotal, timeout=.5)
self.gw_connected = ca.create_channel("gwtest:connected")
ca.connect_channel(self.gw_connected, timeout=.5)
self.gw_active = ca.create_channel("gwtest:active")
ca.connect_channel(self.gw_active, timeout=.5)
self.gw_inactive = ca.create_channel("gwtest:inactive")
ca.connect_channel(self.gw_inactive, timeout=.5)

def updateGwStats(self):
self.vctotal = ca.get(self.gw_vctotal)
Expand All @@ -29,7 +34,6 @@ def updateGwStats(self):
self.active = ca.get(self.gw_active)
self.inactive = ca.get(self.gw_inactive)


def setUp(self):
gwtests.setup()
self.iocControl = IOCControl.IOCControl()
Expand All @@ -48,7 +52,7 @@ def setUp(self):
ioc.get()
pvhigh = epics.PV("ioc:passive0.HIGH", auto_monitor=None)
pvhigh.put(18.0, wait=True)
time.sleep(.05)
gwtests.wait_until(lambda: self.eventsReceivedIOC == 2, 2.0)
if self.eventsReceivedIOC == 2:
self.propSupported = True
ioc.disconnect()
Expand All @@ -63,8 +67,7 @@ def onChangeIOC(self, pvname=None, **kws):
self.eventsReceivedIOC += 1

def onChange(self, pvname=None, **kws):
a = 1

pass

@unittest.skip("detects CAS bug lp:1510955 which is not fixed yet")
def testEnumPropCache_ValueMonitorCTRLget(self):
Expand All @@ -83,7 +86,7 @@ def testEnumPropCache_ValueMonitorCTRLget(self):
self.assertTrue(connected, "Could not connect to gateway channel " + ca.name(gw))
(gw_cbref, gw_uaref, gw_eventid) = ca.create_subscription(gw, mask=dbr.DBE_VALUE, callback=self.onChange)
ioc = ca.create_channel("ioc:enumtest")
connected = ca.connect_channel(ioc, timeout=.5)
connected = ca.connect_channel(ioc)
self.assertTrue(connected, "Could not connect to ioc channel " + ca.name(gw))
(ioc_cbref, ioc_uaref, ioc_eventid) = ca.create_subscription(ioc, mask=dbr.DBE_VALUE, callback=self.onChange)

Expand Down
4 changes: 2 additions & 2 deletions testTop/pyTestsApp/TestStructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def testCtrlStruct_ValueMonitor(self):
# set value on IOC
ioc_value = ca.create_channel("ioc:gwcachetest")
ca.put(ioc_value, 10.0, wait=True)
time.sleep(.1)
gwtests.wait_until(lambda: self.eventsReceivedIOC == self.eventsReceivedGW, 5.0)

self.assertTrue(self.eventsReceivedIOC == self.eventsReceivedGW,
"After setting value, no. of received updates differ: GW {0}, IOC {1}"
Expand All @@ -92,7 +92,7 @@ def testCtrlStruct_ValueMonitor(self):
ioc_hihi = ca.create_channel("ioc:gwcachetest.HIHI")
ca.put(ioc_hihi, 123.0, wait=True)
ca.put(ioc_value, 11.0, wait=True) # trigger update
time.sleep(.1)
gwtests.wait_until(lambda: self.eventsReceivedIOC == self.eventsReceivedGW, 5.0)

self.assertTrue(self.eventsReceivedIOC == self.eventsReceivedGW,
"After setting property, no. of received updates differ: GW {0}, IOC {1}"
Expand Down
8 changes: 8 additions & 0 deletions testTop/pyTestsApp/gwtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import sys
import time

# Do we want verbose logging
verbose = False
Expand Down Expand Up @@ -70,3 +71,10 @@ def setup():
os.environ['PATH'] += os.pathsep + os.path.join(os.environ['EPICS_BASE'], 'bin', hostArch)
else:
print("Warning: EPICS_BASE not set. Will look for 'caRepeater' executable in PATH")

def wait_until(predicate, timeout, period=0.1, *args, **kwargs):
mustend = time.time() + timeout
while time.time() < mustend:
if predicate(*args, **kwargs): return True
time.sleep(period)
return False

0 comments on commit 0ce28c9

Please sign in to comment.