|
| 1 | +import asyncio |
| 2 | +from contextlib import nullcontext |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from conftest import ( |
| 7 | + aioca_cleanup, |
| 8 | + log, |
| 9 | + create_random_prefix, |
| 10 | + TIMEOUT, |
| 11 | + select_and_recv, |
| 12 | + get_multiprocessing_context, |
| 13 | +) |
| 14 | + |
| 15 | +from softioc import asyncio_dispatcher, builder, softioc |
| 16 | + |
| 17 | + |
| 18 | +class TestPVAccess: |
| 19 | + """Tests related to PVAccess""" |
| 20 | + |
| 21 | + record_name = "PVA_AOut" |
| 22 | + record_value = 10 |
| 23 | + |
| 24 | + def pva_test_func(self, device_name, conn, use_pva): |
| 25 | + builder.SetDeviceName(device_name) |
| 26 | + |
| 27 | + builder.aOut(self.record_name, initial_value=self.record_value) |
| 28 | + |
| 29 | + dispatcher = asyncio_dispatcher.AsyncioDispatcher() |
| 30 | + builder.LoadDatabase() |
| 31 | + softioc.iocInit(dispatcher=dispatcher, enable_pva=use_pva) |
| 32 | + |
| 33 | + conn.send("R") # "Ready" |
| 34 | + log("CHILD: Sent R over Connection to Parent") |
| 35 | + |
| 36 | + # Keep process alive while main thread works. |
| 37 | + while (True): |
| 38 | + if conn.poll(TIMEOUT): |
| 39 | + val = conn.recv() |
| 40 | + if val == "D": # "Done" |
| 41 | + break |
| 42 | + |
| 43 | + @pytest.mark.asyncio |
| 44 | + @pytest.mark.parametrize( |
| 45 | + "use_pva,expectation", |
| 46 | + [ |
| 47 | + (True, nullcontext()), |
| 48 | + (False, pytest.raises(asyncio.TimeoutError)) |
| 49 | + ] |
| 50 | + ) |
| 51 | + async def test_pva_enable_disable(self, use_pva, expectation): |
| 52 | + """Test that we can enable and disable PVA, perform PVAccess requests |
| 53 | + when enabled, and that we can always do Channel Access requests""" |
| 54 | + ctx = get_multiprocessing_context() |
| 55 | + parent_conn, child_conn = ctx.Pipe() |
| 56 | + |
| 57 | + device_name = create_random_prefix() |
| 58 | + |
| 59 | + process = ctx.Process( |
| 60 | + target=self.pva_test_func, |
| 61 | + args=(device_name, child_conn, use_pva), |
| 62 | + ) |
| 63 | + |
| 64 | + process.start() |
| 65 | + |
| 66 | + from aioca import caget |
| 67 | + from p4p.client.asyncio import Context |
| 68 | + try: |
| 69 | + # Wait for message that IOC has started |
| 70 | + select_and_recv(parent_conn, "R") |
| 71 | + |
| 72 | + record_full_name = device_name + ":" + self.record_name |
| 73 | + |
| 74 | + ret_val = await caget(record_full_name, timeout=TIMEOUT) |
| 75 | + |
| 76 | + assert ret_val == self.record_value |
| 77 | + |
| 78 | + with expectation as _: |
| 79 | + with Context("pva") as ctx: |
| 80 | + # Short timeout as, if the above CA connection has happened |
| 81 | + # there's no need to wait a very long time for the PVA |
| 82 | + # connection |
| 83 | + pva_val = await asyncio.wait_for( |
| 84 | + ctx.get(record_full_name), |
| 85 | + timeout=2 |
| 86 | + ) |
| 87 | + assert pva_val == self.record_value |
| 88 | + |
| 89 | + |
| 90 | + finally: |
| 91 | + # Clear the cache before stopping the IOC stops |
| 92 | + # "channel disconnected" error messages |
| 93 | + aioca_cleanup() |
| 94 | + parent_conn.send("D") # "Done" |
| 95 | + process.join(timeout=TIMEOUT) |
0 commit comments