pytest-dev / pytest-dev/pytest-asyncio
Running concurrent event loops
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.7k
- Forks
- 207
- Avg merge
- 5h 35m
- Merged PRs (30d)
- 9
Description
To test the basic usage of websockets, I want to start a server and test the return string from client, I think server and client should be in separate thread.
The below code passed, but I just want to know if there's a proper/generalized way to do it?
import pytest
import asyncio
import threading
import websockets
async def server(stop_sig):
async def echo(ws):
async for msg in ws:
await ws.send(msg)
async with websockets.serve(echo, "localhost", 8000):
await stop_sig
@pytest.fixture
def threaded_server():
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
sig = asyncio.Future(loop=loop)
def run_loop(loop, coro):
loop.run_until_complete(coro)
loop.close()
thread = threading.Thread(target=run_loop, args=(loop, server(sig)))
thread.start()
yield
loop.call_soon_threadsafe(sig.set_result, None)
thread.join()
async def test_client_echo(threaded_server):
async with websockets.connect('ws://localhost:8000') as ws:
await ws.send("hello")
assert await ws.recv() == 'hello'
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the threaded_server fixture and test_client_echo example from the issue, including the separate event loop and thread. Review how pytest-asyncio currently handles event loops and determine whether a documented generalized approach is possible; done means the recommended setup and its limitations are clearly explained.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100