Fix test script
authorBenjamin Braatz <bb@bbraatz.eu>
Tue, 12 Jan 2021 17:26:17 +0000 (18:26 +0100)
committerBenjamin Braatz <bb@bbraatz.eu>
Tue, 12 Jan 2021 17:26:17 +0000 (18:26 +0100)
graphit_controlpi/websocket.py
test.py

index 7065f856b1bf3d4c8a8f9655f4b9f90734a0ff78..440994f9663a5f2db44f970989fa4c5c4688f71e 100644 (file)
@@ -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 f68f6382c3fab9bd8757ab152bd2692e45a304a4..6810969cd1a740b96a2f05cb4c2473c6a12f0b15 100644 (file)
--- 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]))