1515 sys .platform .startswith ("win" ), reason = "Cothread doesn't work on windows"
1616)
1717
18+ # Default length used to initialise Waveform and longString records.
19+ # Length picked to match string record length, so we can re-use test strings.
20+ WAVEFORM_LENGTH = 40
21+
22+ # Default timeout for many operations across testing
23+ TIMEOUT = 10 # Seconds
24+
25+ def create_random_prefix ():
26+ """Create 12-character random string, for generating unique Device Names"""
27+ return "" .join (random .choice (string .ascii_uppercase ) for _ in range (12 ))
28+
1829class SubprocessIOC :
1930 def __init__ (self , ioc_py ):
20- self .pv_prefix = "" .join (
21- random .choice (string .ascii_uppercase ) for _ in range (12 )
22- )
31+ self .pv_prefix = create_random_prefix ()
2332 sim_ioc = os .path .join (os .path .dirname (__file__ ), ioc_py )
2433 cmd = [sys .executable , sim_ioc , self .pv_prefix ]
2534 self .proc = subprocess .Popen (
@@ -76,9 +85,9 @@ def _clear_records():
7685 # https://github.com/dls-controls/pythonSoftIOC/issues/56
7786 RecordLookup ._RecordDirectory .clear ()
7887
79- @pytest .fixture
88+ @pytest .fixture ( autouse = True )
8089def clear_records ():
81- """Fixture to delete all records before and after a test. """
90+ """Deletes all records before and after every test"""
8291 _clear_records ()
8392 yield
8493 _clear_records ()
@@ -93,3 +102,21 @@ def enable_code_coverage():
93102 pass
94103 else :
95104 cleanup_on_sigterm ()
105+
106+ def select_and_recv (conn , expected_char = None ):
107+ """Wait for the given Connection to have data to receive, and return it.
108+ If a character is provided check its correct before returning it.
109+ This function imports Cothread, and so must NOT be called before any
110+ multiprocessing sub-processes are spawned."""
111+ from cothread import select
112+ rrdy , _ , _ = select ([conn ], [], [], TIMEOUT )
113+ if rrdy :
114+ val = conn .recv ()
115+ else :
116+ pytest .fail ("Did not receive expected char before TIMEOUT expired" )
117+
118+ if expected_char :
119+ assert val == expected_char , \
120+ "Expected character did not match"
121+
122+ return val
0 commit comments