Skip to content

Commit 32a4f05

Browse files
committed
Add a few more tests
1 parent 8e80c0e commit 32a4f05

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

tests/test_qthreadexec.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def safe_shutdown(executor):
5656
executor.shutdown()
5757
except Exception:
5858
pass
59+
if isinstance(executor, qasync.QThreadPoolExecutor):
60+
# empty the underlying QThreadPool object
61+
executor.pool.waitForDone()
5962

6063

6164
@pytest.fixture(params=[qasync.QThreadExecutor, qasync.QThreadPoolExecutor])
@@ -129,12 +132,14 @@ def test_no_stale_reference_as_result(executor, disable_executor_logging):
129132

130133

131134
def test_map(executor):
135+
"""Basic test of executor map functionality"""
132136
results = list(executor.map(lambda x: x + 1, range(10)))
133137
assert results == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
134138

135139

136140
@pytest.mark.parametrize("cancel", [True, False])
137141
def test_map_timeout(executor, cancel):
142+
"""Test that map with timeout raises TimeoutError and cancels futures"""
138143
results = []
139144

140145
def func(x):
@@ -157,5 +162,21 @@ def func(x):
157162
# only about half of the tasks should have completed
158163
# because the max number of workers is 5 and the rest of
159164
# the tasks were not started at the time of the cancel.
160-
assert results
161165
assert set(results) != {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
166+
167+
168+
def test_context(executor):
169+
"""Test that the context manager will shutdown executor"""
170+
with executor:
171+
f = executor.submit(lambda: 42)
172+
assert f.result() == 42
173+
174+
with pytest.raises(RuntimeError):
175+
executor.submit(lambda: 42)
176+
177+
178+
def test_default_pool_executor():
179+
"""Test that using the global instance of QThreadPool works"""
180+
with qasync.QThreadPoolExecutor() as executor:
181+
f = executor.submit(lambda: 42)
182+
assert f.result() == 42

0 commit comments

Comments
 (0)