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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
import pytest
import platform
import unittest
import asyncio
from gi.repository import GLib, Gio
from gi.events import GLibEventLoopPolicy
class TestAsync(unittest.TestCase):
def setUp(self):
policy = GLibEventLoopPolicy()
asyncio.set_event_loop_policy(policy)
self.addCleanup(asyncio.set_event_loop_policy, None)
self.loop = policy.get_event_loop()
self.addCleanup(self.loop.close)
def test_async_enumerate(self):
f = Gio.file_new_for_path("./")
called = False
def cb():
nonlocal called
called = True
async def run():
nonlocal called, self
self.loop.call_soon(cb)
iter_info = [
info.get_name()
for info in await f.enumerate_children_async(
"standard::*", 0, GLib.PRIORITY_DEFAULT
)
]
# The await runs the mainloop and cb is called.
self.assertEqual(called, True)
next_info = []
enumerator = f.enumerate_children("standard::*", 0, None)
while True:
info = enumerator.next_file(None)
if info is None:
break
next_info.append(info.get_name())
self.assertEqual(iter_info, next_info)
self.loop.run_until_complete(run())
def test_async_cancellation(self):
"""Cancellation raises G_IO_ERROR_CANCELLED."""
f = Gio.file_new_for_path("./")
async def run():
nonlocal self
# cancellable created implicitly
res = f.enumerate_children_async("standard::*", 0, GLib.PRIORITY_DEFAULT)
res.cancel()
with self.assertRaisesRegex(GLib.GError, "Operation was cancelled"):
await res
# cancellable passed explicitly
cancel = Gio.Cancellable()
res = f.enumerate_children_async(
"standard::*", 0, GLib.PRIORITY_DEFAULT, cancel
)
self.assertEqual(res.cancellable, cancel)
cancel.cancel()
with self.assertRaisesRegex(GLib.GError, "Operation was cancelled"):
await res
self.loop.run_until_complete(run())
def test_not_completed(self):
"""Querying an uncompleted task raises exceptions."""
f = Gio.file_new_for_path("./")
async def run():
nonlocal self
# cancellable created implicitly
res = f.enumerate_children_async("standard::*", 0, GLib.PRIORITY_DEFAULT)
with self.assertRaises(asyncio.InvalidStateError):
res.result()
with self.assertRaises(asyncio.InvalidStateError):
res.exception()
# And, await it
await res
self.loop.run_until_complete(run())
def test_async_cancel_completed(self):
"""Cancelling a completed task just cancels the cancellable."""
f = Gio.file_new_for_path("./")
async def run():
nonlocal self
res = f.enumerate_children_async("standard::*", 0, GLib.PRIORITY_DEFAULT)
await res
assert not res.cancellable.is_cancelled()
res.cancel()
assert res.cancellable.is_cancelled()
self.loop.run_until_complete(run())
def test_async_completed_add_cb(self):
"""Adding a done cb to a completed future queues it with call_soon."""
f = Gio.file_new_for_path("./")
called = False
def cb():
nonlocal called
called = True
async def run():
nonlocal called, self
res = f.enumerate_children_async("standard::*", 0, GLib.PRIORITY_DEFAULT)
await res
self.loop.call_soon(cb)
# Python await is smart and does not iterate the EventLoop
await res
assert not called
# So create a new future and wait on that
fut = asyncio.Future()
def done_cb(res):
nonlocal fut
fut.set_result(res.result())
res.add_done_callback(done_cb)
await fut
assert called
self.loop.run_until_complete(run())
@pytest.mark.xfail(
platform.python_implementation() == "PyPy",
reason="Exception reporting does not work in pypy",
)
def test_deleting_failed_logs(self):
f = Gio.file_new_for_path("./")
async def run():
nonlocal self
res = f.enumerate_children_async("standard::*", 0, GLib.PRIORITY_DEFAULT)
res.cancel()
# Cancellation in Gio is not immediate, so sleep for a bit
await asyncio.sleep(0.5)
exc = None
msg = None
def handler(loop, context):
nonlocal exc, msg
msg = context["message"]
exc = context["exception"]
self.loop.set_exception_handler(handler)
self.loop.run_until_complete(run())
self.assertRegex(msg, ".*exception was never retrieved")
self.assertIsInstance(exc, GLib.GError)
assert exc.matches(Gio.io_error_quark(), Gio.IOErrorEnum.CANCELLED)
def test_no_running_loop(self):
f = Gio.file_new_for_path("./")
res = f.enumerate_children_async("standard::*", 0, GLib.PRIORITY_DEFAULT)
self.assertIsNone(res)
def test_wrong_default_context(self):
f = Gio.file_new_for_path("./")
async def run(): # noqa: RUF029
nonlocal self
ctx = GLib.MainContext.new()
GLib.MainContext.push_thread_default(ctx)
self.addCleanup(GLib.MainContext.pop_thread_default, ctx)
res = f.enumerate_children_async("standard::*", 0, GLib.PRIORITY_DEFAULT)
self.assertIsNone(res)
self.loop.run_until_complete(run())
|