aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/QtAsyncio/qasyncio_test.py
blob: f3c971285af56385359b971afe9ccff966c6d33d (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
# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

'''Test cases for QtAsyncio'''

import unittest
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()