|
5 | 5 |
|
6 | 6 | import asyncio |
7 | 7 | import ctypes |
8 | | -import threading |
9 | 8 | import logging |
10 | 9 | import multiprocessing |
11 | 10 | import os |
12 | 11 | import socket |
13 | 12 | import subprocess |
14 | 13 | import sys |
| 14 | +import threading |
15 | 15 | import time |
16 | 16 | from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor |
17 | 17 |
|
@@ -809,6 +809,61 @@ async def mycoro(): |
809 | 809 | assert "seconds" in msg |
810 | 810 |
|
811 | 811 |
|
| 812 | +def test_run_until_complete_returns_future_result(loop): |
| 813 | + async def coro(): |
| 814 | + await asyncio.sleep(0) |
| 815 | + return 42 |
| 816 | + |
| 817 | + assert loop.run_until_complete(asyncio.wait_for(coro(), timeout=1)) == 42 |
| 818 | + |
| 819 | + |
| 820 | +def test_run_forever_custom_exit_code(loop, application): |
| 821 | + if hasattr(application, "exec"): |
| 822 | + orig_exec = application.exec |
| 823 | + application.exec = lambda: 42 |
| 824 | + try: |
| 825 | + assert loop.run_forever() == 42 |
| 826 | + finally: |
| 827 | + application.exec = orig_exec |
| 828 | + else: |
| 829 | + orig_exec = application.exec_ |
| 830 | + application.exec_ = lambda: 42 |
| 831 | + try: |
| 832 | + assert loop.run_forever() == 42 |
| 833 | + finally: |
| 834 | + application.exec_ = orig_exec |
| 835 | + |
| 836 | + |
| 837 | +def test_qeventloop_in_qthread(): |
| 838 | + class CoroutineExecutorThread(qasync.QtCore.QThread): |
| 839 | + def __init__(self, coro): |
| 840 | + super().__init__() |
| 841 | + self.coro = coro |
| 842 | + self.loop = None |
| 843 | + |
| 844 | + def run(self): |
| 845 | + self.loop = qasync.QEventLoop(self) |
| 846 | + asyncio.set_event_loop(self.loop) |
| 847 | + asyncio.run(self.coro) |
| 848 | + |
| 849 | + def join(self): |
| 850 | + self.loop.stop() |
| 851 | + self.loop.close() |
| 852 | + self.wait() |
| 853 | + |
| 854 | + event = threading.Event() |
| 855 | + |
| 856 | + async def coro(): |
| 857 | + await asyncio.sleep(0.1) |
| 858 | + event.set() |
| 859 | + |
| 860 | + thread = CoroutineExecutorThread(coro()) |
| 861 | + thread.start() |
| 862 | + |
| 863 | + assert event.wait(timeout=1), "Coroutine did not execute successfully" |
| 864 | + |
| 865 | + thread.join() # Ensure thread cleanup |
| 866 | + |
812 | 867 | def teardown_module(module): |
813 | 868 | """ |
814 | 869 | Remove handlers from all loggers |
|
0 commit comments