Skip to content

Commit f7a1f53

Browse files
Add test for AsyncioDispatcher as ContextManager
1 parent 101192c commit f7a1f53

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

tests/test_asyncio.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
from multiprocessing.connection import Listener
66

7-
from conftest import requires_cothread, ADDRESS, select_and_recv
7+
from conftest import (
8+
ADDRESS, select_and_recv,
9+
log, get_multiprocessing_context, TIMEOUT,
10+
create_random_prefix
11+
)
812

913
from softioc.asyncio_dispatcher import AsyncioDispatcher
14+
from softioc import builder, softioc
1015

1116
@pytest.mark.asyncio
1217
async def test_asyncio_ioc(asyncio_ioc):
@@ -131,3 +136,73 @@ def test_asyncio_dispatcher_event_loop():
131136
event_loop = asyncio.get_event_loop()
132137
with pytest.raises(ValueError):
133138
AsyncioDispatcher(loop=event_loop)
139+
140+
def asyncio_dispatcher_test_func(device_name, child_conn):
141+
142+
log("CHILD: Child started")
143+
144+
builder.SetDeviceName(device_name)
145+
146+
147+
with AsyncioDispatcher() as dispatcher:
148+
# Create some records
149+
ai = builder.aIn('AI', initial_value=5)
150+
builder.aOut('AO', initial_value=12.45, always_update=True,
151+
on_update=lambda v: ai.set(v))
152+
153+
# Boilerplate get the IOC started
154+
builder.LoadDatabase()
155+
softioc.iocInit(dispatcher)
156+
157+
# Start processes required to be run after iocInit
158+
async def update():
159+
while True:
160+
ai.set(ai.get() + 1)
161+
await asyncio.sleep(0.01)
162+
163+
dispatcher(update)
164+
165+
log("CHILD: Sending Ready")
166+
child_conn.send("R")
167+
168+
# Keep process alive while main thread runs CAGET
169+
if child_conn.poll(TIMEOUT):
170+
val = child_conn.recv()
171+
assert val == "D", "Did not receive expected Done character"
172+
173+
174+
async def test_asyncio_dispatcher_as_context_manager():
175+
"""Test that the asyncio dispatcher can be used as a context manager"""
176+
ctx = get_multiprocessing_context()
177+
parent_conn, child_conn = ctx.Pipe()
178+
179+
device_name = create_random_prefix()
180+
181+
process = ctx.Process(
182+
target=asyncio_dispatcher_test_func,
183+
args=(device_name, child_conn),
184+
)
185+
186+
process.start()
187+
188+
log("PARENT: Child started, waiting for R command")
189+
190+
from aioca import caget
191+
try:
192+
# Wait for message that IOC has started
193+
select_and_recv(parent_conn, "R")
194+
195+
# ao_val = await caget(device_name + ":AO")
196+
ao_val = await caget(device_name + ":AO")
197+
assert ao_val == 12.45
198+
199+
# Confirm the value of the AI record is increasing
200+
ai_val_1 = await caget(device_name + ":AI")
201+
await asyncio.sleep(1)
202+
ai_val_2 = await caget(device_name + ":AI")
203+
assert ai_val_2 > ai_val_1
204+
205+
finally:
206+
parent_conn.send("D") # "Done"
207+
process.join(timeout=TIMEOUT)
208+
assert process.exitcode == 0 # clean exit

0 commit comments

Comments
 (0)