From a544e6e2ce4aee68c37b957274e3310f2cb192c0 Mon Sep 17 00:00:00 2001 From: John Zhou Date: Tue, 22 Jul 2025 21:22:32 -0500 Subject: [PATCH 1/6] add a simple test to run --- tests/test_run.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/test_run.py diff --git a/tests/test_run.py b/tests/test_run.py new file mode 100644 index 0000000..b7ed047 --- /dev/null +++ b/tests/test_run.py @@ -0,0 +1,18 @@ +import qasync +import asyncio +import os +from qasync import QApplication + +def test_run_with_contextmanager(application): + async def coro(): + event_loop = asyncio.get_event_loop() + assert type(event_loop).__name__ == "QIOCPEventLoop" if os.name == 'nt' else "QSelectorEventLoop" + await asyncio.sleep(0) + + qasync.run(coro()) + + try: + event_loop = asyncio.get_event_loop() + except: + event_loop = None + assert type(event_loop).__name__ != "QIOCPEventLoop" if os.name == 'nt' else "QSelectorEventLoop" From d1b91454e0f23c91a874ef0897f439f932275e19 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 23 Jul 2025 10:38:03 -0500 Subject: [PATCH 2/6] Update test_run.py --- tests/test_run.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tests/test_run.py b/tests/test_run.py index b7ed047..ebfc896 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -1,18 +1,23 @@ -import qasync import asyncio import os -from qasync import QApplication + +import pytest + +import qasync + def test_run_with_contextmanager(application): async def coro(): event_loop = asyncio.get_event_loop() - assert type(event_loop).__name__ == "QIOCPEventLoop" if os.name == 'nt' else "QSelectorEventLoop" + assert ( + type(event_loop).__name__ == "QIOCPEventLoop" + if os.name == "nt" + else "QSelectorEventLoop" + ) await asyncio.sleep(0) + asyncio.set_event_loop(None) qasync.run(coro()) - try: - event_loop = asyncio.get_event_loop() - except: - event_loop = None - assert type(event_loop).__name__ != "QIOCPEventLoop" if os.name == 'nt' else "QSelectorEventLoop" + with pytest.raises(RuntimeError): + _ = asyncio.get_event_loop() From 69ca26aa83a4c8c947a5f2117d521d517b676eca Mon Sep 17 00:00:00 2001 From: John Date: Wed, 23 Jul 2025 10:50:01 -0500 Subject: [PATCH 3/6] Update test_run.py --- tests/test_run.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/test_run.py b/tests/test_run.py index ebfc896..4cddee5 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -5,19 +5,27 @@ import qasync - -def test_run_with_contextmanager(application): +@pytest.fixture +def get_event_loop_coro(expected_loop): async def coro(): event_loop = asyncio.get_event_loop() - assert ( - type(event_loop).__name__ == "QIOCPEventLoop" - if os.name == "nt" - else "QSelectorEventLoop" - ) + + assert type(event_loop).__name__ == expected_loop await asyncio.sleep(0) + return coro + +@pytest.fixture +def expected_loop(): + return "QIOCPEventLoop" if os.name == "nt" else "QSelectorEventLoop" +def test_run_with_contextmanager(get_event_loop_coro): asyncio.set_event_loop(None) - qasync.run(coro()) + qasync.run(get_event_loop_coro()) with pytest.raises(RuntimeError): _ = asyncio.get_event_loop() + +def test_run_with_existing_eventloop(get_event_loop_coro, expected_loop): + asyncio.set_event_loop(asyncio.new_event_loop()) + qasync.run(get_event_loop_coro()) + assert asyncio.get_event_loop() != expected_loop From ae703fc40da18e0b6f1d0eb1626674c96ab36085 Mon Sep 17 00:00:00 2001 From: John Zhou Date: Wed, 23 Jul 2025 10:54:46 -0500 Subject: [PATCH 4/6] better test --- tests/test_run.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_run.py b/tests/test_run.py index 4cddee5..9d2d82c 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -25,7 +25,8 @@ def test_run_with_contextmanager(get_event_loop_coro): with pytest.raises(RuntimeError): _ = asyncio.get_event_loop() -def test_run_with_existing_eventloop(get_event_loop_coro, expected_loop): - asyncio.set_event_loop(asyncio.new_event_loop()) +def test_run_reset_policy(get_event_loop_coro, expected_loop): + old_loop = asyncio.new_event_loop() qasync.run(get_event_loop_coro()) - assert asyncio.get_event_loop() != expected_loop + new_loop = asyncio.new_event_loop() + assert type(old_loop) == type(new_loop) From 4def8cc4e152eadbd8f2aa6156a6eb7223987487 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 23 Jul 2025 13:40:56 -0500 Subject: [PATCH 5/6] more tests! --- tests/test_run.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/test_run.py b/tests/test_run.py index 9d2d82c..5519ebd 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -1,5 +1,6 @@ import asyncio import os +from unittest.mock import ANY import pytest @@ -7,10 +8,11 @@ @pytest.fixture def get_event_loop_coro(expected_loop): - async def coro(): + async def coro(expected_debug): event_loop = asyncio.get_event_loop() assert type(event_loop).__name__ == expected_loop + assert event_loop.get_debug() == expected_debug await asyncio.sleep(0) return coro @@ -20,13 +22,17 @@ def expected_loop(): def test_run_with_contextmanager(get_event_loop_coro): asyncio.set_event_loop(None) - qasync.run(get_event_loop_coro()) + qasync.run(get_event_loop_coro(ANY)) with pytest.raises(RuntimeError): _ = asyncio.get_event_loop() -def test_run_reset_policy(get_event_loop_coro, expected_loop): +def test_run_reset_policy(get_event_loop_coro): old_loop = asyncio.new_event_loop() - qasync.run(get_event_loop_coro()) + qasync.run(get_event_loop_coro(ANY)) new_loop = asyncio.new_event_loop() assert type(old_loop) == type(new_loop) + +def test_run_debug(get_event_loop_coro): + qasync.run(get_event_loop_coro(True), debug=True) + qasync.run(get_event_loop_coro(False), debug=False) From 5376628d583559bafac43121c1cb500c23758308 Mon Sep 17 00:00:00 2001 From: Alex March Date: Thu, 24 Jul 2025 11:50:43 +0900 Subject: [PATCH 6/6] tidy up tests --- tests/test_run.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/test_run.py b/tests/test_run.py index 5519ebd..9c4a22a 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -1,38 +1,37 @@ import asyncio -import os from unittest.mock import ANY import pytest import qasync + @pytest.fixture -def get_event_loop_coro(expected_loop): +def get_event_loop_coro(): async def coro(expected_debug): event_loop = asyncio.get_event_loop() - - assert type(event_loop).__name__ == expected_loop + assert type(event_loop) is qasync.QEventLoop assert event_loop.get_debug() == expected_debug await asyncio.sleep(0) + return coro -@pytest.fixture -def expected_loop(): - return "QIOCPEventLoop" if os.name == "nt" else "QSelectorEventLoop" -def test_run_with_contextmanager(get_event_loop_coro): +def test_qasync_run_restores_loop(get_event_loop_coro): asyncio.set_event_loop(None) qasync.run(get_event_loop_coro(ANY)) with pytest.raises(RuntimeError): _ = asyncio.get_event_loop() -def test_run_reset_policy(get_event_loop_coro): - old_loop = asyncio.new_event_loop() + +def test_qasync_run_restores_policy(get_event_loop_coro): + old_policy = asyncio.get_event_loop_policy() qasync.run(get_event_loop_coro(ANY)) - new_loop = asyncio.new_event_loop() - assert type(old_loop) == type(new_loop) + new_policy = asyncio.get_event_loop_policy() + assert type(old_policy) is type(new_policy) + -def test_run_debug(get_event_loop_coro): +def test_qasync_run_with_debug_args(get_event_loop_coro): qasync.run(get_event_loop_coro(True), debug=True) qasync.run(get_event_loop_coro(False), debug=False)