Skip to content

Commit df90f39

Browse files
committed
Move test fixtures around to avoid imports
1 parent c33d49f commit df90f39

File tree

3 files changed

+73
-69
lines changed

3 files changed

+73
-69
lines changed

tests/conftest.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1+
import atexit
12
import os
23
import random
34
import string
45
import subprocess
56
import sys
67

8+
import pytest
9+
710
if sys.version_info < (3,):
811
# Python2 has no asyncio, so ignore these tests
912
collect_ignore = [
10-
"test_asyncio.py", "sim_asyncio_ioc.py", "sim_asyncio_ioc_overide.py"
13+
"test_asyncio.py", "sim_asyncio_ioc.py", "sim_asyncio_ioc_override.py"
1114
]
1215

13-
PV_PREFIX = "".join(random.choice(string.ascii_uppercase) for _ in range(12))
14-
15-
1616
class SubprocessIOC:
1717
def __init__(self, ioc_py):
18+
self.pv_prefix = "".join(
19+
random.choice(string.ascii_uppercase) for _ in range(12)
20+
)
1821
sim_ioc = os.path.join(os.path.dirname(__file__), ioc_py)
19-
cmd = [sys.executable, sim_ioc, PV_PREFIX]
22+
cmd = [sys.executable, sim_ioc, self.pv_prefix]
2023
self.proc = subprocess.Popen(
2124
cmd, stdin=subprocess.PIPE,
2225
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -28,3 +31,37 @@ def kill(self):
2831
out, err = self.proc.communicate()
2932
print(out.decode())
3033
print(err.decode())
34+
35+
36+
@pytest.fixture
37+
def cothread_ioc():
38+
ioc = SubprocessIOC("sim_cothread_ioc.py")
39+
yield ioc
40+
ioc.kill()
41+
42+
43+
def aioca_cleanup():
44+
from aioca import purge_channel_caches, _catools
45+
# Unregister the aioca atexit handler as it conflicts with the one installed
46+
# by cothread. If we don't do this we get a seg fault. This is not a problem
47+
# in production as we won't mix aioca and cothread, but we do mix them in
48+
# the tests so need to do this.
49+
atexit.unregister(_catools._catools_atexit)
50+
# purge the channels before the event loop goes
51+
purge_channel_caches()
52+
53+
54+
@pytest.fixture
55+
def asyncio_ioc():
56+
ioc = SubprocessIOC("sim_asyncio_ioc.py")
57+
yield ioc
58+
ioc.kill()
59+
aioca_cleanup()
60+
61+
62+
@pytest.fixture
63+
def asyncio_ioc_override():
64+
ioc = SubprocessIOC("sim_asyncio_ioc_override.py")
65+
yield ioc
66+
ioc.kill()
67+
aioca_cleanup()

tests/test_asyncio.py

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,7 @@
11
# Will be ignored on Python2 by conftest.py settings
22

3-
import atexit
43
import signal
54
import pytest
6-
from tests.conftest import SubprocessIOC, PV_PREFIX
7-
8-
9-
def aioca_cleanup():
10-
from aioca import purge_channel_caches, _catools
11-
# Unregister the aioca atexit handler as it conflicts with the one installed
12-
# by cothread. If we don't do this we get a seg fault. This is not a problem
13-
# in production as we won't mix aioca and cothread, but we do mix them in
14-
# the tests so need to do this.
15-
atexit.unregister(_catools._catools_atexit)
16-
# purge the channels before the event loop goes
17-
purge_channel_caches()
18-
19-
20-
@pytest.fixture
21-
def asyncio_ioc():
22-
ioc = SubprocessIOC("sim_asyncio_ioc.py")
23-
yield ioc.proc
24-
ioc.kill()
25-
aioca_cleanup()
265

276

287
@pytest.mark.asyncio
@@ -31,73 +10,67 @@ async def test_asyncio_ioc(asyncio_ioc):
3110
from aioca import caget, caput, camonitor, CANothing, _catools, FORMAT_TIME
3211

3312
# Start
34-
assert (await caget(PV_PREFIX + ":UPTIME")).startswith("00:00:0")
13+
pre = asyncio_ioc.pv_prefix
14+
assert (await caget(pre + ":UPTIME")).startswith("00:00:0")
3515
# WAVEFORM
36-
await caput(PV_PREFIX + ":SINN", 4, wait=True)
16+
await caput(pre + ":SINN", 4, wait=True)
3717
q = asyncio.Queue()
38-
m = camonitor(PV_PREFIX + ":SIN", q.put, notify_disconnect=True)
18+
m = camonitor(pre + ":SIN", q.put, notify_disconnect=True)
3919
assert len(await asyncio.wait_for(q.get(), 1)) == 4
4020
# AO
41-
ao_val = await caget(PV_PREFIX + ":ALARM", format=FORMAT_TIME)
21+
ao_val = await caget(pre + ":ALARM", format=FORMAT_TIME)
4222
assert ao_val == 0
4323
assert ao_val.severity == 3 # INVALID
4424
assert ao_val.status == 17 # UDF
45-
await caput(PV_PREFIX + ":ALARM", 3, wait=True)
25+
await caput(pre + ":ALARM", 3, wait=True)
4626
await asyncio.sleep(0.1)
47-
ai_val = await caget(PV_PREFIX + ":AI", format=FORMAT_TIME)
27+
ai_val = await caget(pre + ":AI", format=FORMAT_TIME)
4828
assert ai_val == 23.45
4929
assert ai_val.severity == 0
5030
assert ai_val.status == 0
5131
await asyncio.sleep(0.8)
52-
ai_val = await caget(PV_PREFIX + ":AI", format=FORMAT_TIME)
32+
ai_val = await caget(pre + ":AI", format=FORMAT_TIME)
5333
assert ai_val == 23.45
5434
assert ai_val.severity == 3
5535
assert ai_val.status == 7 # STATE_ALARM
5636
# Check pvaccess works
5737
from p4p.client.asyncio import Context
5838
with Context("pva") as ctx:
59-
assert await ctx.get(PV_PREFIX + ":AI") == 23.45
39+
assert await ctx.get(pre + ":AI") == 23.45
6040
# Wait for a bit longer for the print output to flush
6141
await asyncio.sleep(2)
6242
# Stop
63-
out, err = asyncio_ioc.communicate(b"exit\n", timeout=5)
43+
out, err = asyncio_ioc.proc.communicate(b"exit\n", timeout=5)
6444
out = out.decode()
6545
err = err.decode()
6646
# Disconnect
6747
assert isinstance(await asyncio.wait_for(q.get(), 10), CANothing)
6848
m.close()
6949
# check closed and output
70-
assert "%s:SINN.VAL 1024 -> 4" % PV_PREFIX in out
50+
assert "%s:SINN.VAL 1024 -> 4" % pre in out
7151
assert 'update_sin_wf 4' in out
72-
assert "%s:ALARM.VAL 0 -> 3" % PV_PREFIX in out
73-
assert 'on_update %s:AO : 3.0' % PV_PREFIX in out
52+
assert "%s:ALARM.VAL 0 -> 3" % pre in out
53+
assert 'on_update %s:AO : 3.0' % pre in out
7454
assert 'async update 3.0 (23.45)' in out
7555
assert 'Starting iocInit' in err
7656
assert 'iocRun: All initialization complete' in err
7757
assert '(InteractiveConsole)' in err
7858

7959

80-
@pytest.fixture
81-
def asyncio_ioc_override():
82-
ioc = SubprocessIOC("sim_asyncio_ioc_override.py")
83-
yield ioc.proc
84-
ioc.kill()
85-
aioca_cleanup()
86-
87-
8860
@pytest.mark.asyncio
8961
async def test_asyncio_ioc_override(asyncio_ioc_override):
9062
from aioca import caget, caput
9163

9264
# Gain bo
93-
assert (await caget(PV_PREFIX + ":GAIN")) == 0
94-
await caput(PV_PREFIX + ":GAIN", "On", wait=True)
95-
assert (await caget(PV_PREFIX + ":GAIN")) == 1
65+
pre = asyncio_ioc_override.pv_prefix
66+
assert (await caget(pre + ":GAIN")) == 0
67+
await caput(pre + ":GAIN", "On", wait=True)
68+
assert (await caget(pre + ":GAIN")) == 1
9669

9770
# Stop
98-
asyncio_ioc_override.send_signal(signal.SIGINT)
71+
asyncio_ioc_override.proc.send_signal(signal.SIGINT)
9972
# check closed and output
100-
out, err = asyncio_ioc_override.communicate()
73+
out, err = asyncio_ioc_override.proc.communicate()
10174
out = out.decode()
10275
err = err.decode()
10376
# check closed and output

tests/test_cothread.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,48 @@
11
import sys
22
import signal
3-
from tests.conftest import SubprocessIOC, PV_PREFIX
43
import pytest
54

65

76
if sys.platform.startswith("win"):
87
pytest.skip("Cothread doesn't work on windows", allow_module_level=True)
98

109

11-
@pytest.fixture
12-
def cothread_ioc():
13-
ioc = SubprocessIOC("sim_cothread_ioc.py")
14-
yield ioc.proc
15-
ioc.kill()
16-
1710

1811
def test_cothread_ioc(cothread_ioc):
1912
import cothread
2013
from cothread.catools import ca_nothing, caget, caput, camonitor
2114

15+
pre = cothread_ioc.pv_prefix
2216
# Start
23-
assert caget(PV_PREFIX + ":UPTIME").startswith("00:00:0")
17+
assert caget(pre + ":UPTIME").startswith("00:00:0")
2418
# WAVEFORM
25-
caput(PV_PREFIX + ":SINN", 4, wait=True)
19+
caput(pre + ":SINN", 4, wait=True)
2620
q = cothread.EventQueue()
27-
m = camonitor(PV_PREFIX + ":SIN", q.Signal, notify_disconnect=True)
21+
m = camonitor(pre + ":SIN", q.Signal, notify_disconnect=True)
2822
assert len(q.Wait(1)) == 4
2923
# STRINGOUT
30-
assert caget(PV_PREFIX + ":STRINGOUT") == "watevah"
31-
caput(PV_PREFIX + ":STRINGOUT", "something", wait=True)
32-
assert caget(PV_PREFIX + ":STRINGOUT") == "something"
24+
assert caget(pre + ":STRINGOUT") == "watevah"
25+
caput(pre + ":STRINGOUT", "something", wait=True)
26+
assert caget(pre + ":STRINGOUT") == "something"
3327
# Check pvaccess works
3428
from p4p.client.cothread import Context
3529
with Context("pva") as ctx:
36-
assert ctx.get(PV_PREFIX + ":STRINGOUT") == "something"
30+
assert ctx.get(pre + ":STRINGOUT") == "something"
3731
# Wait for a bit longer for the print output to flush
3832
cothread.Sleep(2)
3933
# Stop
40-
cothread_ioc.send_signal(signal.SIGINT)
34+
cothread_ioc.proc.send_signal(signal.SIGINT)
4135
# Disconnect
4236
assert isinstance(q.Wait(10), ca_nothing)
4337
m.close()
4438
# check closed and output
45-
out, err = cothread_ioc.communicate()
39+
out, err = cothread_ioc.proc.communicate()
4640
out = out.decode()
4741
err = err.decode()
4842
# check closed and output
49-
assert "%s:SINN.VAL 1024 -> 4" % PV_PREFIX in out
43+
assert "%s:SINN.VAL 1024 -> 4" % pre in out
5044
assert 'update_sin_wf 4' in out
51-
assert "%s:STRINGOUT.VAL watevah -> something" % PV_PREFIX in out
45+
assert "%s:STRINGOUT.VAL watevah -> something" % pre in out
5246
assert 'on_update \'something\'' in out
5347
assert 'Starting iocInit' in err
5448
assert 'iocRun: All initialization complete' in err

0 commit comments

Comments
 (0)