Skip to content

Commit

Permalink
speed up admin websocket test
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead committed Nov 21, 2019
1 parent 116ad93 commit b3f1b28
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions aries_cloudagent/admin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ async def websocket_handler(self, request):
await ws.prepare(request)
socket_id = str(uuid.uuid4())
queue = BasicMessageQueue()
loop = asyncio.get_event_loop()

try:
self.websocket_queues[socket_id] = queue
Expand All @@ -360,20 +361,40 @@ async def websocket_handler(self, request):
)

closed = False
receive = loop.create_task(ws.receive())
send = loop.create_task(queue.dequeue(timeout=5.0))

while not closed:
try:
msg = await queue.dequeue(timeout=5.0)
if msg is None:
# we send fake pings because the JS client
# can't detect real ones
msg = {"topic": "ping"}
await asyncio.wait(
(receive, send), return_when=asyncio.FIRST_COMPLETED
)
if ws.closed:
closed = True
if msg and not closed:
await ws.send_json(msg)

if receive.done():
# ignored
if not closed:
receive = loop.create_task(ws.receive())

if send.done():
msg = send.result()
if msg is None:
# we send fake pings because the JS client
# can't detect real ones
msg = {"topic": "ping"}
if not closed:
if msg:
await ws.send_json(msg)
send = loop.create_task(queue.dequeue(timeout=5.0))
except asyncio.CancelledError:
closed = True

if not receive.done():
receive.cancel()
if not send.done():
send.cancel()

finally:
del self.websocket_queues[socket_id]

Expand Down

0 comments on commit b3f1b28

Please sign in to comment.