|
1 | 1 | # Will be ignored on Python2 by conftest.py settings |
2 | 2 |
|
3 | | -import random |
4 | | -import string |
5 | | -import subprocess |
6 | | -import sys |
7 | | -import os |
8 | 3 | import atexit |
| 4 | +import signal |
9 | 5 | import pytest |
10 | | -import time |
| 6 | +from tests.conftest import SubprocessIOC, PV_PREFIX |
11 | 7 |
|
12 | | -PV_PREFIX = "".join(random.choice(string.ascii_uppercase) for _ in range(12)) |
| 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() |
13 | 18 |
|
14 | 19 |
|
15 | 20 | @pytest.fixture |
16 | 21 | def asyncio_ioc(): |
17 | | - sim_ioc = os.path.join(os.path.dirname(__file__), "sim_asyncio_ioc.py") |
18 | | - cmd = [sys.executable, sim_ioc, PV_PREFIX] |
19 | | - proc = subprocess.Popen( |
20 | | - cmd, stdin=subprocess.PIPE, |
21 | | - stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
22 | | - yield proc |
23 | | - # purge the channels before the event loop goes |
24 | | - from aioca import purge_channel_caches |
25 | | - purge_channel_caches() |
26 | | - if proc.returncode is None: |
27 | | - # still running, kill it and print the output |
28 | | - proc.kill() |
29 | | - out, err = proc.communicate() |
30 | | - print(out.decode()) |
31 | | - print(err.decode(), file=sys.stderr) |
| 22 | + ioc = SubprocessIOC("sim_asyncio_ioc.py") |
| 23 | + yield ioc.proc |
| 24 | + ioc.kill() |
| 25 | + aioca_cleanup() |
32 | 26 |
|
33 | 27 |
|
34 | 28 | @pytest.mark.asyncio |
35 | 29 | async def test_asyncio_ioc(asyncio_ioc): |
36 | 30 | import asyncio |
37 | 31 | from aioca import caget, caput, camonitor, CANothing, _catools, FORMAT_TIME |
38 | | - # Unregister the aioca atexit handler as it conflicts with the one installed |
39 | | - # by cothread. If we don't do this we get a seg fault. This is not a problem |
40 | | - # in production as we won't mix aioca and cothread, but we do mix them in |
41 | | - # the tests so need to do this. |
42 | | - atexit.unregister(_catools._catools_atexit) |
43 | 32 |
|
44 | 33 | # Start |
45 | 34 | assert (await caget(PV_PREFIX + ":UPTIME")).startswith("00:00:0") |
@@ -86,3 +75,32 @@ async def test_asyncio_ioc(asyncio_ioc): |
86 | 75 | assert 'Starting iocInit' in err |
87 | 76 | assert 'iocRun: All initialization complete' in err |
88 | 77 | assert '(InteractiveConsole)' in err |
| 78 | + |
| 79 | + |
| 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 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_asyncio_ioc_override(asyncio_ioc_override): |
| 90 | + from aioca import caget, caput |
| 91 | + |
| 92 | + # 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 |
| 96 | + |
| 97 | + # Stop |
| 98 | + asyncio_ioc_override.send_signal(signal.SIGINT) |
| 99 | + # check closed and output |
| 100 | + out, err = asyncio_ioc_override.communicate() |
| 101 | + out = out.decode() |
| 102 | + err = err.decode() |
| 103 | + # check closed and output |
| 104 | + assert '1' in out |
| 105 | + assert 'Starting iocInit' in err |
| 106 | + assert 'iocRun: All initialization complete' in err |
0 commit comments