aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/QtAsyncio/bug_2790.py
diff options
context:
space:
mode:
authorAdrian Herrmann <adrian.herrmann@qt.io>2024-07-03 18:36:20 +0200
committerAdrian Herrmann <adrian.herrmann@qt.io>2024-07-16 06:32:02 +0200
commit526bc12e42db7c6305bcc28ad8f6b7554597d725 (patch)
treea98f909fdf5c893bfe5d2937e0246acdae688dea /sources/pyside6/tests/QtAsyncio/bug_2790.py
parent32c36073e212e7ca17b1f7578d61195aefd9dbca (diff)
QtAsyncio: Add cancel count and uncancel
Implement the QAsyncioTask.uncancel() function and the associated cancel count. Note to reader: Unlike what the name suggests, the uncancel() function on its own does not undo a task cancellation. This must be performed by consuming the CancelledError exception, at which point uncancel() serves to remove the cancellation state. Pick-to: 6.7 Task-number: PYSIDE-769 Fixes: PYSIDE-2790 Change-Id: I4e817e1dd3f49179855432d20ed2f043090fd8f1 Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/pyside6/tests/QtAsyncio/bug_2790.py')
-rw-r--r--sources/pyside6/tests/QtAsyncio/bug_2790.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/sources/pyside6/tests/QtAsyncio/bug_2790.py b/sources/pyside6/tests/QtAsyncio/bug_2790.py
new file mode 100644
index 000000000..9fd152b15
--- /dev/null
+++ b/sources/pyside6/tests/QtAsyncio/bug_2790.py
@@ -0,0 +1,47 @@
+# Copyright (C) 2024 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+from __future__ import annotations
+
+'''Test cases for QtAsyncio'''
+
+import unittest
+import asyncio
+
+import PySide6.QtAsyncio as QtAsyncio
+
+
+class QAsyncioTestCaseBug2790(unittest.TestCase):
+
+ async def producer(self, products: list[str]):
+ while True:
+ products.append("product")
+ await asyncio.sleep(2)
+
+ async def task(self, outputs: list[str]):
+ products = []
+ asyncio.ensure_future(self.producer(products))
+ for _ in range(6):
+ try:
+ async with asyncio.timeout(0.5):
+ while len(products) == 0:
+ await asyncio.sleep(0)
+ outputs.append(products.pop(0))
+ except TimeoutError:
+ outputs.append("Timeout")
+
+ def test_timeout(self):
+ # The Qt event loop (and thus QtAsyncio) does not guarantee that events
+ # will be processed in the order they were posted, so there is two
+ # possible outputs for this test.
+ outputs_expected_1 = ["product", "Timeout", "Timeout", "Timeout", "Timeout", "product"]
+ outputs_expected_2 = ["product", "Timeout", "Timeout", "Timeout", "product", "Timeout"]
+
+ outputs_real = []
+
+ QtAsyncio.run(self.task(outputs_real), keep_running=False)
+
+ self.assertTrue(outputs_real in [outputs_expected_1, outputs_expected_2])
+
+
+if __name__ == '__main__':
+ unittest.main()