Skip to content

Commit 969663c

Browse files
committed
Add tests for using QEventLoop inside of QThread
1 parent 4d6b4ed commit 969663c

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

tests/test_qeventloop.py

Lines changed: 32 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,37 @@ 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+
self.loop = None
884+
885+
def run(self):
886+
self.loop = qasync.QEventLoop(self)
887+
asyncio.set_event_loop(self.loop)
888+
asyncio.run(self.coro)
889+
890+
def join(self):
891+
self.loop.stop()
892+
self.loop.close()
893+
self.wait()
894+
895+
event = threading.Event()
896+
897+
async def coro():
898+
await asyncio.sleep(0.1)
899+
event.set()
900+
901+
thread = CoroutineExecutorThread(coro())
902+
thread.start()
903+
904+
assert event.wait(timeout=1), "Coroutine did not execute successfully"
905+
906+
thread.join() # Ensure thread cleanup
907+
908+
877909
def teardown_module(module):
878910
"""
879911
Remove handlers from all loggers

0 commit comments

Comments
 (0)