Debian Codegraph @

tests/test_source.py

raw
1 import sys
2 import gc
3 import unittest
4 import warnings
5
6 from gi.repository import GLib
7 from gi import PyGIDeprecationWarning
8
9 from .helper import capture_glib_warnings
10
11
12 class Idle(GLib.Idle):
13 def __init__(self, loop):
14 GLib.Idle.__init__(self)
15 self.count = 0
16 self.set_callback(self.callback, loop)
17
18 def callback(self, loop):
19 self.count += 1
20 return True
21
22
23 class MySource(GLib.Source):
24 def __init__(self):
25 GLib.Source.__init__(self)
26
27 def prepare(self):
28 return True, 0
29
30 def check(self):
31 return True
32
33 def dispatch(self, callback, args):
34 return callback(*args)
35
36
37 class TestSource(unittest.TestCase):
38 def timeout_callback(self, loop):
39 loop.quit()
40
41 def my_callback(self, loop):
42 self.pos += 1
43 return True
44
45 def setup_timeout(self, loop):
46 timeout = GLib.Timeout(500)
47 timeout.set_callback(self.timeout_callback, loop)
48 timeout.attach()
49
50 def test_sources(self):
51 loop = GLib.MainLoop()
52
53 self.setup_timeout(loop)
54
55 idle = Idle(loop)
56 self.assertEqual(idle.get_context(), None)
57 idle.attach()
58 self.assertEqual(idle.get_context(), GLib.main_context_default())
59
60 self.pos = 0
61
62 m = MySource()
63 self.assertEqual(m.get_context(), None)
64 m.set_callback(self.my_callback, loop)
65 m.attach()
66 self.assertEqual(m.get_context(), GLib.main_context_default())
67
68 loop.run()
69
70 m.destroy()
71 idle.destroy()
72
73 self.assertGreater(self.pos, 0)
74 self.assertGreaterEqual(idle.count, 0)
75 self.assertTrue(m.is_destroyed())
76 self.assertTrue(idle.is_destroyed())
77
78 def test_source_prepare(self):
79 # this test may not terminate if prepare() is wrapped incorrectly
80 dispatched = [False]
81 loop = GLib.MainLoop()
82
83 class CustomTimeout(GLib.Source):
84 def prepare(self):
85 return (False, 10)
86
87 def check(self):
88 return True
89
90 def dispatch(self, callback, args):
91 dispatched[0] = True
92
93 loop.quit()
94
95 return False
96
97 source = CustomTimeout()
98
99 source.attach()
100 source.set_callback(dir)
101
102 loop.run()
103
104 assert dispatched[0]
105
106 def test_is_destroyed_simple(self):
107 s = GLib.Source()
108
109 self.assertFalse(s.is_destroyed())
110 s.destroy()
111 self.assertTrue(s.is_destroyed())
112
113 c = GLib.MainContext()
114 s = GLib.Source()
115 s.attach(c)
116 self.assertFalse(s.is_destroyed())
117 s.destroy()
118 self.assertTrue(s.is_destroyed())
119
120 def test_is_destroyed_context(self):
121 def f():
122 c = GLib.MainContext()
123 s = GLib.Source()
124 s.attach(c)
125 return s
126
127 s = f()
128 gc.collect()
129 self.assertTrue(s.is_destroyed())
130
131 def test_remove(self):
132 s = GLib.idle_add(dir)
133 self.assertEqual(GLib.source_remove(s), True)
134
135 # Removing sources not found cause critical
136 with capture_glib_warnings(allow_criticals=True):
137 # s is now removed, should fail now
138 self.assertEqual(GLib.source_remove(s), False)
139
140 # accepts large source IDs (they are unsigned)
141 self.assertEqual(GLib.source_remove(GLib.MAXINT32), False)
142 self.assertEqual(GLib.source_remove(GLib.MAXINT32 + 1), False)
143 self.assertEqual(GLib.source_remove(GLib.MAXUINT32), False)
144
145 def test_recurse_property(self):
146 s = GLib.Idle()
147 self.assertTrue(s.can_recurse in [False, True])
148 s.can_recurse = False
149 self.assertFalse(s.can_recurse)
150
151 def test_priority(self):
152 s = GLib.Idle()
153 self.assertEqual(s.priority, GLib.PRIORITY_DEFAULT_IDLE)
154 s.priority = GLib.PRIORITY_HIGH
155 self.assertEqual(s.priority, GLib.PRIORITY_HIGH)
156
157 s = GLib.Idle(GLib.PRIORITY_LOW)
158 self.assertEqual(s.priority, GLib.PRIORITY_LOW)
159
160 s = GLib.Timeout(1, GLib.PRIORITY_LOW)
161 self.assertEqual(s.priority, GLib.PRIORITY_LOW)
162
163 s = GLib.Source()
164 self.assertEqual(s.priority, GLib.PRIORITY_DEFAULT)
165
166 def test_get_current_time(self):
167 # Note, deprecated API
168 s = GLib.Idle()
169 with warnings.catch_warnings(record=True) as w:
170 warnings.simplefilter("always")
171 time = s.get_current_time()
172 self.assertTrue(issubclass(w[0].category, PyGIDeprecationWarning))
173
174 self.assertTrue(isinstance(time, float))
175 # plausibility check, and check magnitude of result
176 self.assertGreater(time, 1300000000.0)
177 self.assertLess(time, 2000000000.0)
178
179 def test_add_remove_poll(self):
180 # FIXME: very shallow test, only verifies the API signature
181 pollfd = GLib.PollFD(99, GLib.IOCondition.IN | GLib.IOCondition.HUP)
182 self.assertEqual(pollfd.fd, 99)
183 source = GLib.Source()
184 source.add_poll(pollfd)
185 source.remove_poll(pollfd)
186
187 def test_out_of_scope_before_dispatch(self):
188 # https://bugzilla.gnome.org/show_bug.cgi?id=504337
189 GLib.Timeout(20)
190 GLib.Idle()
191
192 @unittest.skipIf(sys.platform == "darwin", "hangs")
193 def test_finalize(self):
194 self.dispatched = False
195 self.finalized = False
196
197 class S(GLib.Source):
198 def prepare(s):
199 return (True, 1)
200
201 def dispatch(s, callback, args):
202 self.dispatched = True
203 return False
204
205 def finalize(s):
206 self.finalized = True
207
208 source = S()
209 source.attach()
210 self.assertFalse(self.finalized)
211 self.assertFalse(source.is_destroyed())
212
213 while source.get_context().iteration(False):
214 pass
215
216 source.destroy()
217 self.assertTrue(self.dispatched)
218 self.assertFalse(self.finalized)
219 self.assertTrue(source.is_destroyed())
220 del source
221 gc.collect()
222 self.assertTrue(self.finalized)
223
224 def test_python_unref_with_active_source(self):
225 # Tests a Python derived Source which is free'd in the context of
226 # Python, but which was attached to a MainContext (via source.attach())
227 self.dispatched = False
228 self.finalized = False
229
230 class S(GLib.Source):
231 def prepare(s):
232 return (True, 1)
233
234 def check(s):
235 pass
236
237 def dispatch(s, callback, args):
238 self.dispatched = True
239 return False
240
241 def finalize(s):
242 self.finalized = True
243
244 context = GLib.MainContext.new()
245 source = S()
246 id_ = source.attach(context)
247 self.assertFalse(self.finalized)
248 self.assertFalse(source.is_destroyed())
249
250 # Delete the source from Python, it should detach
251 del source
252 gc.collect()
253
254 while context.iteration(may_block=False):
255 pass
256
257 assert self.finalized
258 assert not self.dispatched
259 assert context.find_source_by_id(id_) is None
260
261 @unittest.skipIf(
262 sys.implementation.name == "pypy", "PyPy doesn't __del__ immediately"
263 )
264 def test_python_unref_during_dispatch(self):
265 # Tests a Python derived Source which is free'd in the context of
266 # Python, while being dispatched
267 # NOTE: Finalize is explicitly called by __del__ for consistency
268 self.dispatched = False
269 self.finalized = False
270
271 class S(GLib.Source):
272 def __init__(s, func=None):
273 s.func = func
274
275 def prepare(s):
276 return (True, 1)
277
278 def check(s):
279 pass
280
281 def dispatch(s, callback, args):
282 self.dispatched = True
283 self.source = None
284 return False
285
286 def finalize(s):
287 self.finalized = True
288
289 context = GLib.MainContext.new()
290 self.source = S()
291 id_ = self.source.attach(context)
292
293 while context.iteration(may_block=False):
294 pass
295
296 assert self.source is None
297 assert context.find_source_by_id(id_) is None
298 assert self.finalized
299 assert self.dispatched
300
301 def test_extra_init_args(self):
302 class SourceWithInitArgs(GLib.Source):
303 def __init__(self, arg, kwarg=None):
304 super().__init__()
305 self.arg = arg
306 self.kwarg = kwarg
307
308 source = SourceWithInitArgs(1, kwarg=2)
309 self.assertEqual(source.arg, 1)
310 self.assertEqual(source.kwarg, 2)
311
312
313 @unittest.skipIf(sys.platform == "darwin", "hangs")
314 class TestUserData(unittest.TestCase):
315 def test_idle_no_data(self):
316 ml = GLib.MainLoop()
317
318 def cb():
319 ml.quit()
320
321 id = GLib.idle_add(cb)
322 self.assertEqual(
323 ml.get_context().find_source_by_id(id).priority, GLib.PRIORITY_DEFAULT_IDLE
324 )
325 ml.run()
326
327 def test_timeout_no_data(self):
328 ml = GLib.MainLoop()
329
330 def cb():
331 ml.quit()
332
333 id = GLib.timeout_add(1, cb)
334 self.assertEqual(
335 ml.get_context().find_source_by_id(id).priority, GLib.PRIORITY_DEFAULT
336 )
337 ml.run()
338
339 def test_idle_data(self):
340 ml = GLib.MainLoop()
341
342 def cb(data):
343 data["called"] = True
344 ml.quit()
345
346 data = {}
347 id = GLib.idle_add(cb, data)
348 self.assertEqual(
349 ml.get_context().find_source_by_id(id).priority, GLib.PRIORITY_DEFAULT_IDLE
350 )
351 ml.run()
352 self.assertTrue(data["called"])
353
354 def test_idle_multidata(self):
355 ml = GLib.MainLoop()
356
357 def cb(data, data2):
358 data["called"] = True
359 data["data2"] = data2
360 ml.quit()
361
362 data = {}
363 id = GLib.idle_add(cb, data, "hello")
364 self.assertEqual(
365 ml.get_context().find_source_by_id(id).priority, GLib.PRIORITY_DEFAULT_IDLE
366 )
367 ml.run()
368 self.assertTrue(data["called"])
369 self.assertEqual(data["data2"], "hello")
370
371 def test_timeout_data(self):
372 ml = GLib.MainLoop()
373
374 def cb(data):
375 data["called"] = True
376 ml.quit()
377
378 data = {}
379 id = GLib.timeout_add(1, cb, data)
380 self.assertEqual(
381 ml.get_context().find_source_by_id(id).priority, GLib.PRIORITY_DEFAULT
382 )
383 ml.run()
384 self.assertTrue(data["called"])
385
386 def test_timeout_multidata(self):
387 ml = GLib.MainLoop()
388
389 def cb(data, data2):
390 data["called"] = True
391 data["data2"] = data2
392 ml.quit()
393
394 data = {}
395 id = GLib.timeout_add(1, cb, data, "hello")
396 self.assertEqual(
397 ml.get_context().find_source_by_id(id).priority, GLib.PRIORITY_DEFAULT
398 )
399 ml.run()
400 self.assertTrue(data["called"])
401 self.assertEqual(data["data2"], "hello")
402
403 def test_idle_no_data_priority(self):
404 ml = GLib.MainLoop()
405
406 def cb():
407 ml.quit()
408
409 id = GLib.idle_add(cb, priority=GLib.PRIORITY_HIGH)
410 self.assertEqual(
411 ml.get_context().find_source_by_id(id).priority, GLib.PRIORITY_HIGH
412 )
413 ml.run()
414
415 def test_timeout_no_data_priority(self):
416 ml = GLib.MainLoop()
417
418 def cb():
419 ml.quit()
420
421 id = GLib.timeout_add(1, cb, priority=GLib.PRIORITY_HIGH)
422 self.assertEqual(
423 ml.get_context().find_source_by_id(id).priority, GLib.PRIORITY_HIGH
424 )
425 ml.run()
426
427 def test_idle_data_priority(self):
428 ml = GLib.MainLoop()
429
430 def cb(data):
431 data["called"] = True
432 ml.quit()
433
434 data = {}
435 id = GLib.idle_add(cb, data, priority=GLib.PRIORITY_HIGH)
436 self.assertEqual(
437 ml.get_context().find_source_by_id(id).priority, GLib.PRIORITY_HIGH
438 )
439 ml.run()
440 self.assertTrue(data["called"])
441
442 def test_timeout_data_priority(self):
443 ml = GLib.MainLoop()
444
445 def cb(data):
446 data["called"] = True
447 ml.quit()
448
449 data = {}
450 id = GLib.timeout_add(1, cb, data, priority=GLib.PRIORITY_HIGH)
451 self.assertEqual(
452 ml.get_context().find_source_by_id(id).priority, GLib.PRIORITY_HIGH
453 )
454 ml.run()
455 self.assertTrue(data["called"])
456
457 def cb_no_data(self):
458 self.loop.quit()
459
460 def test_idle_method_callback_no_data(self):
461 self.loop = GLib.MainLoop()
462 GLib.idle_add(self.cb_no_data)
463 self.loop.run()
464
465 def cb_with_data(self, data):
466 data["called"] = True
467 self.loop.quit()
468
469 def test_idle_method_callback_with_data(self):
470 self.loop = GLib.MainLoop()
471 data = {}
472 GLib.idle_add(self.cb_with_data, data)
473 self.loop.run()
474 self.assertTrue(data["called"])