aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/QtAsyncio/qasyncio_test.py
blob: cf3f77113d0541b0d8ae78624f5dc9f9b5129ccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Copyright (C) 2023 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 os
import sys
import unittest

from pathlib import Path
sys.path.append(os.fspath(Path(__file__).resolve().parents[1]))
from init_paths import init_test_paths
init_test_paths(False)

import asyncio
from PySide6.QtAsyncio import QAsyncioEventLoopPolicy


class QAsyncioTestCase(unittest.TestCase):
    async def sleep(self, output):
        output += "Hello"
        await asyncio.sleep(0.2)
        output += "World"

    async def gather(self, output):
        await asyncio.gather(self.sleep(output), self.sleep(output), self.sleep(output))

    def test_sleep(self):
        outputs_expected = []
        outputs_real = []

        # Run the code without QAsyncioEventLoopPolicy
        asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
        asyncio.run(self.sleep(outputs_expected))

        # Run the code with QAsyncioEventLoopPolicy and QtEventLoop
        asyncio.set_event_loop_policy(QAsyncioEventLoopPolicy())
        asyncio.run(self.sleep(outputs_real))

        self.assertEqual(outputs_expected, outputs_real)

    def test_gather(self):
        outputs_expected = []
        outputs_real = []

        # Run the code without QAsyncioEventLoopPolicy
        asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
        asyncio.run(self.gather(outputs_expected))

        # Run the code with QAsyncioEventLoopPolicy and QtEventLoop
        asyncio.set_event_loop_policy(QAsyncioEventLoopPolicy())
        asyncio.run(self.gather(outputs_real))

        self.assertEqual(outputs_expected, outputs_real)


if __name__ == '__main__':
    unittest.main()