Skip to content

Commit 2ae3fb7

Browse files
committed
Add tests for using QEventLoop inside of QThread
1 parent 2ee7397 commit 2ae3fb7

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

tests/test_qeventloop.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import asyncio
77
import ctypes
8+
import threading
89
import logging
910
import multiprocessing
1011
import os
@@ -874,6 +875,30 @@ async def main():
874875
assert not loop.is_running()
875876

876877

878+
def test_qeventloop_in_qthread():
879+
class CoroutineExecutorThread(qasync.QtCore.QThread):
880+
def __init__(self, coro):
881+
super().__init__()
882+
self.coro = coro
883+
884+
def run(self):
885+
asyncio.set_event_loop(qasync.QEventLoop(self))
886+
asyncio.run(self.coro)
887+
888+
event = threading.Event()
889+
890+
async def coro():
891+
await asyncio.sleep(0.1)
892+
event.set()
893+
894+
thread = CoroutineExecutorThread(coro())
895+
thread.start()
896+
897+
assert event.wait(timeout=1), "Coroutine did not execute successfully"
898+
899+
thread.wait() # Ensure thread cleanup
900+
901+
877902
def teardown_module(module):
878903
"""
879904
Remove handlers from all loggers

0 commit comments

Comments
 (0)