Skip to content

Commit 1976893

Browse files
committed
Add Controller.disconnect
1 parent 9f2309f commit 1976893

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/fastcs/control_system.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ async def block_forever():
144144
finally:
145145
logger.info("Shutting down FastCS")
146146
self._stop_scan_tasks()
147+
await self._controller.disconnect()
147148

148149
async def _interactive_shell(self, context: dict[str, Any]):
149150
"""Spawn interactive shell in another thread and wait for it to complete."""

src/fastcs/controller.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,6 @@ def __init__(
195195

196196
async def connect(self) -> None:
197197
pass
198+
199+
async def disconnect(self) -> None:
200+
pass

tests/assertable_controller.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ async def initialise(self) -> None:
6161
async def connect(self) -> None:
6262
self.connected = True
6363

64+
async def disconnect(self) -> None:
65+
self.connected = False
66+
6467
@command()
6568
async def go(self):
6669
pass

tests/test_control_system.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,35 @@ async def raise_exception(self):
162162
# The task exception comes from scan method raise_exception
163163
assert isinstance(internal_exception, ValueError)
164164
assert "Scan Exception" == str(internal_exception)
165+
166+
167+
@pytest.mark.asyncio
168+
async def test_controller_connect_disconnect():
169+
class MyTestController(Controller):
170+
def __init__(self):
171+
super().__init__()
172+
173+
self.connected = False
174+
175+
async def connect(self):
176+
self.connected = True
177+
178+
async def disconnect(self):
179+
self.connected = False
180+
181+
controller = MyTestController()
182+
183+
loop = asyncio.get_event_loop()
184+
fastcs = FastCS(controller, [], loop)
185+
186+
task = asyncio.create_task(fastcs.serve(interactive=False))
187+
188+
# connect is called at the start of serve
189+
await asyncio.sleep(0.1)
190+
assert controller.connected
191+
192+
task.cancel()
193+
194+
# disconnect is called at the end of serve
195+
await asyncio.sleep(0.1)
196+
assert not controller.connected

0 commit comments

Comments
 (0)