Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# © 2018 Gerard Marull-Paretas <gerard@teslabs.com>
# © 2014 Mark Harviston <mark.harviston@gmail.com>
# © 2014 Arve Knudsen <arve.knudsen@gmail.com>
# BSD License
"""
Copyright (c) 2018 Gerard Marull-Paretas <gerard@teslabs.com>
Copyright (c) 2014 Mark Harviston <mark.harviston@gmail.com>
Copyright (c) 2014 Arve Knudsen <arve.knudsen@gmail.com>

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"
Expand Down
57 changes: 56 additions & 1 deletion tests/test_qeventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -680,7 +680,7 @@
await callback_invoked
loop._remove_writer(c_sock.fileno())

loop.run_until_complete(asyncio.wait_for(client_coro(), timeout=0.1))

Check failure on line 683 in tests/test_qeventloop.py

View workflow job for this annotation

GitHub Actions / macos-x86_64 / 3.8 / pyqt5

test_add_writer_replace asyncio.exceptions.TimeoutError
assert not called1
assert called2

Expand Down Expand Up @@ -809,6 +809,61 @@
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=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


def test_qeventloop_in_qthread():
class CoroutineExecutorThread(qasync.QtCore.QThread):
def __init__(self, coro):
super().__init__()
self.coro = coro
self.loop = None

def run(self):
self.loop = qasync.QEventLoop(self)
asyncio.set_event_loop(self.loop)
asyncio.run(self.coro)

def join(self):
self.loop.stop()
self.loop.close()
self.wait()

event = threading.Event()

async def coro():
await asyncio.sleep(0.1)
event.set()

thread = CoroutineExecutorThread(coro())
thread.start()

assert event.wait(timeout=1), "Coroutine did not execute successfully"

thread.join() # Ensure thread cleanup

def teardown_module(module):
"""
Remove handlers from all loggers
Expand Down
Loading