summaryrefslogtreecommitdiff
path: root/tests/test_events.py
blob: fd0678752196cffd7a0bf1e085bda7bd8699a5c1 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import unittest

try:
    from test.test_asyncio.test_events import UnixEventLoopTestsMixin
    from test.test_asyncio.test_subprocess import SubprocessMixin
    from test.test_asyncio.utils import TestCase
except:
    class UnixEventLoopTestsMixin():
        def test_unix_event_loop_tests_missing(self):
            import warnings
            warnings.warn('UnixEventLoopTestsMixin is unavailable, not running tests!')
            self.skipTest('UnixEventLoopTestsMixin is unavailable, not running tests!')

    class SubprocessMixin():
        def test_subprocess_mixin_tests_missing(self):
            import warnings
            warnings.warn('SubprocessMixin is unavailable, not running tests!')
            self.skipTest('SubprocessMixin is unavailable, not running tests!')

    from unittest import TestCase

import sys
import gi
import gi.events
import asyncio
import threading
from gi.repository import GLib


# None of this currently works on Windows
if sys.platform != 'win32':

    class GLibEventLoopTests(UnixEventLoopTestsMixin, TestCase):

        def __init__(self, *args):
            super().__init__(*args)
            self.loop = None

        def create_event_loop(self):
            return gi.events.GLibEventLoop(GLib.MainContext())

    class SubprocessWatcherTests(SubprocessMixin, TestCase):

        def setUp(self):
            super().setUp()
            policy = gi.events.GLibEventLoopPolicy()
            asyncio.set_event_loop_policy(policy)
            self.loop = policy.get_event_loop()

        def tearDown(self):
            asyncio.set_event_loop_policy(None)
            self.loop.close()
            super().tearDown()

    class GLibEventLoopPolicyTests(unittest.TestCase):

        def create_policy(self):
            return gi.events.GLibEventLoopPolicy()

        def test_get_event_loop(self):
            policy = self.create_policy()
            loop = policy.get_event_loop()
            self.assertIsInstance(loop, gi.events.GLibEventLoop)
            self.assertIs(loop, policy.get_event_loop())
            loop.close()

        def test_new_event_loop(self):
            policy = self.create_policy()
            loop = policy.new_event_loop()
            self.assertIsInstance(loop, gi.events.GLibEventLoop)
            loop.close()

            # Attaching a loop to the main thread fails
            with self.assertRaises(RuntimeError):
                policy.set_event_loop(loop)

        def test_nested_context_iteration(self):
            policy = self.create_policy()
            loop = policy.new_event_loop()

            called = False

            def cb():
                nonlocal called
                called = True

            async def run():
                nonlocal loop, called

                loop.call_soon(cb)
                self.assertEqual(called, False)

                # Iterating the main context does not cause cb to be called
                while loop._context.iteration(False):
                    pass
                self.assertEqual(called, False)

                # Awaiting on anything *does* cause the cb to fire
                await asyncio.sleep(0)
                self.assertEqual(called, True)

            loop.run_until_complete(run())
            loop.close()

        def test_thread_event_loop(self):
            policy = self.create_policy()
            loop = policy.new_event_loop()

            res = []

            def thread_func(res):
                try:
                    # We cannot get an event loop for the current thread
                    with self.assertRaises(RuntimeError):
                        policy.get_event_loop()

                    # We can attach our loop
                    policy.set_event_loop(loop)
                    # Now we can get it, and it is the same
                    self.assertIs(policy.get_event_loop(), loop)

                    # Simple call_soon test
                    results = []

                    def callback(arg1, arg2):
                        results.append((arg1, arg2))
                        loop.stop()

                    loop.call_soon(callback, 'hello', 'world')
                    loop.run_forever()
                    self.assertEqual(results, [('hello', 'world')])

                    # We can detach it again
                    policy.set_event_loop(None)

                    # Which means we have none and get a runtime error
                    with self.assertRaises(RuntimeError):
                        policy.get_event_loop()
                except:
                    res += sys.exc_info()

            # Initially, the thread has no event loop
            thread = threading.Thread(target=lambda: thread_func(res))
            thread.start()
            thread.join()

            if res:
                t, v, tb = res
                raise t(v).with_traceback(tb)

            loop.close()