From 130178d2eb588de0da887e4710cf1ac249cbfb91 Mon Sep 17 00:00:00 2001 From: Alex March Date: Fri, 25 Jul 2025 17:20:11 +0900 Subject: [PATCH] Test run_forever and run_until_complete return values --- tests/conftest.py | 15 +++++++++------ tests/test_qeventloop.py | 27 ++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index e52683a..acb8c38 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,12 +1,15 @@ -# © 2018 Gerard Marull-Paretas -# © 2014 Mark Harviston -# © 2014 Arve Knudsen -# BSD License +""" +Copyright (c) 2018 Gerard Marull-Paretas +Copyright (c) 2014 Mark Harviston +Copyright (c) 2014 Arve Knudsen + +BSD License +""" -import os import logging -from pytest import fixture +import os +from pytest import fixture logging.basicConfig( level=logging.DEBUG, format="%(levelname)s\t%(filename)s:%(lineno)s %(message)s" diff --git a/tests/test_qeventloop.py b/tests/test_qeventloop.py index 76d374d..48f25ab 100644 --- a/tests/test_qeventloop.py +++ b/tests/test_qeventloop.py @@ -5,13 +5,13 @@ import asyncio import ctypes -import threading import logging import multiprocessing import os import socket import subprocess import sys +import threading import time from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor @@ -809,6 +809,31 @@ async def mycoro(): assert "seconds" in msg +def test_run_until_complete_returns_future_result(loop): + async def coro(): + await asyncio.sleep(0) + return 42 + + assert loop.run_until_complete(asyncio.wait_for(coro(), timeout=0.1)) == 42 + + +def test_run_forever_custom_exit_code(loop, application): + if hasattr(application, "exec"): + orig_exec = application.exec + application.exec = lambda: 42 + try: + assert loop.run_forever() == 42 + finally: + application.exec = orig_exec + else: + orig_exec = application.exec_ + application.exec_ = lambda: 42 + try: + assert loop.run_forever() == 42 + finally: + application.exec_ = orig_exec + + @pytest.mark.skipif(sys.version_info < (3, 12), reason="Requires Python 3.12+") def test_asyncio_run(application): """Test that QEventLoop is compatible with asyncio.run()"""