From a56806d30027f81b8f11355d77cbe443fea04313 Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Tue, 12 Jan 2021 18:26:17 +0100 Subject: [PATCH] Fix test script --- graphit_controlpi/websocket.py | 5 +++-- test.py | 17 +++++------------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/graphit_controlpi/websocket.py b/graphit_controlpi/websocket.py index 7065f85..440994f 100644 --- a/graphit_controlpi/websocket.py +++ b/graphit_controlpi/websocket.py @@ -29,7 +29,7 @@ async def command_handler(websocket, path, pins, out_queue): await process_command(command, pins, out_queue) -async def event_handler(websocket, path): +async def event_handler(websocket, path, out_queue): while True: event = await out_queue.get() message = json.dumps(event) @@ -40,7 +40,8 @@ async def event_handler(websocket, path): async def handler(pins, out_queue, websocket, path): command_task = asyncio.create_task(command_handler(websocket, path, pins, out_queue)) - event_task = asyncio.create_task(event_handler(websocket, path)) + event_task = asyncio.create_task(event_handler(websocket, path, + out_queue)) done, pending = await asyncio.wait( [command_task, event_task], return_when=asyncio.FIRST_COMPLETED, diff --git a/test.py b/test.py index f68f638..6810969 100644 --- a/test.py +++ b/test.py @@ -8,12 +8,14 @@ async def test_commands(websocket): commands = [{'command': 'getallpins'}, {'command': 'setpin', 'pin': 'A2-8', 'value': True}, {'command': 'setpin', 'pin': 'A2-8', 'value': True}, + {'command': 'getpin', 'pin': 'T2-16'}, {'command': 'setpin', 'pin': 'A2-8', 'value': False}, {'command': 'getpin', 'pin': 'T2-16'}] for command in commands: message = json.dumps(command) await websocket.send(message) print(f"Sent Command: {message}") + await asyncio.sleep(0.1) async def receive_events(websocket): @@ -22,19 +24,10 @@ async def receive_events(websocket): async def main(hostname): - async with websockets.connect(f"ws://{hostname}:80") as websocket: + async with websockets.connect(f"ws://{hostname}") as websocket: command_task = asyncio.create_task(test_commands(websocket)) event_task = asyncio.create_task(receive_events(websocket)) - done, pending = await asyncio.wait( - [command_task, event_task], - return_when=asyncio.BOTH_COMPLETED, - ) - for task in pending: - task.cancel() - + await command_task if __name__ == '__main__': - try: - asyncio.get_event_loop().run_until_complete(main(sys.argv[1])) - except KeyboardInterrupt: - pass + asyncio.run(main(sys.argv[1])) -- 2.34.1