Skip to content

Commit

Permalink
Rerun tests mid-run on another thread for supporting retry libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriochaves authored and willemt committed Jul 4, 2024
1 parent 41e8665 commit 8181457
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pytest_asyncio_cooperative/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import threading
import asyncio
import collections.abc
import functools
Expand Down Expand Up @@ -290,7 +291,30 @@ async def run_tests(tasks, max_tasks: int):
item_by_coro[new_task] = item
continue

item.runtest = lambda: result.result()
# We need to change .runtest to a synchronous function for pytest
# however, if it is called again by retry libraries we need to rerun
# the test instead of retuning the previous result
def wrap_in_sync():
def sync_wrapper():
new_task = item_to_task(item)

# We use a new thread because we can't block for an async function
# in the same thread as the current running event loop, nor
# we can nest event loops
result = None
def run_in_thread():
nonlocal result
result = asyncio.run(new_task)
thread = threading.Thread(target=run_in_thread)
thread.start()
thread.join()
return result

item.runtest = sync_wrapper

return result.result()

item.runtest = wrap_in_sync

item.ihook.pytest_runtest_protocol(item=item, nextitem=None)

Expand Down

0 comments on commit 8181457

Please sign in to comment.